これは私の最初の mongodb プロジェクトです。mongodb にこのドキュメント構造があります。特定のユーザー アカウント (各ユーザー アカウントには連絡先の配列があります) を取得しようとしています。このユーザー アカウントから、ID フィールドの配列を取得します。ユーザーの連絡先を取得し、この配列をパラメーターとして別のクエリに渡します。ID フィールドを取得するためにユーザーの連絡先配列をループする必要がないようにするためにこれを行っています。これがドキュメント構造です。私が試したクエリは以下のとおりです。それ
{
  name,
  id,
  contacts:[{
             contactId, //I need an array of this field
             dateAdded
             },
             contactId,
             dateAdded
             },
              {}..]
}
//
  var findByIdAll = function(accountId, callback) {
     var self=this;
          //Get the user account
      Account.findOne({_id:accountId}, function(err,doc) {
          /  After the user account has been obtained, the function below will
        // use an array of the users contactsId's to fetch the contact's accounts
          //please how do I obtain the array of contact Id's before reaching here                     
       self.Account.find({_id:{$in:[/array of contact Ids]}},function(err,results){
      callback(results);
    });
    });
  };
編集 //次のクエリを使用して contactID フィールドの配列を取得できるようになりました
 var r=db.accounts.aggregate({$match:{email:'m@live.com'}},{$unwind:"$contacts"},
       {$project:{_id:0,contacts:1}},{$group:{_id:'$_id',
       list:{$push:'$contacts.accountId'}}});
    The result I get from the query is 
r
{
        "result" : [
                {
                        "_id" : null,
                        "list" : [
                                ObjectId("51c59a31c398c40c22000004"),
                                ObjectId("51c59a31c398c40c22000004")
                        ]
                }
        ],
        "ok" : 1
}