2016-10-24 11 views
0

私は友人を保存し、この「ユーザー」モデルを持っているが、この友人はマングースの移入のネーミングと構造

var friendQuery = User.find({_id:req.params.id}); 
friendQuery.select('friends'); 
friendQuery.populate({ path: 'friends._id', model: 'user', select:'_id name' });   
friendQuery.exec(
    function(err, user) { 
    if (err) 
     res.send(err); 
     res.send(user); 
}); 

私の質問があり、ここで

// Prepare schema 
var schema = new db.Schema({ 
    friends: [{ 
     active : Boolean, 
     user : { type: db.Schema.ObjectId, ref: 'user' }, 
     options : [{ type: db.Schema.ObjectId, ref: 'friend_option' }], 
    }], 
    image: { type: db.Schema.ObjectId, ref: 'file' }, 
    password: { 
     type: String, 
     select: false 
    }, 
    email: String, 
    name: String, 
    udid: [{ 
     device: String, 
     udid: String 
    }], 
    created_on: Date 
}); 

は、私は彼の友人と一人のユーザーを取得し、いくつかのオプションがありますなぜ結果は私のモデルと違うのですか?どうして友だち - > _ id - > _ idが代わりにfriends-> user - > _ idになるのですか?

friends": [ 
     { 
     "_id": { 
      "_id": "58025664c154929d3207b232", 
      "name": "User 1" 
     }, 
     "options": [ 
     ], 
     "active": false 
     }, 
     { 
     "_id": { 
      "_id": "580257e1e3cd4fc7326fa97b", 
      "name": "User 2" 
     }, 
     "options": [ 
     ], 
     "active": false 
     } 
    ] 

別のオプションでは、このソリューションは仮想の大きなアプリに適していますか?

答えて

0

あなたは単に間違いのために働くのユーザにクエリから_idを置き換える配列の友人でユーザーの参照を取っているとして、ちょうどあなたのクエリ

var friendQuery = User.find({_id:req.params.id}); 
friendQuery.select('friends'); 
friendQuery.populate({ path: 'friends.user', model: 'user', select:'_id name' });   
friendQuery.exec(
    function(err, user) { 
    if (err) 
     res.send(err); 
     res.send(user); 
}); 

で簡単な変更を加える間違ったで、これを使用する方法あなた

関連する問題