2016-08-05 13 views
0

私は、モバイルアプリのためのREST APIを開発しています。これは、モデルの一部である私はマングース - 外部キー参照の設定プレゼンテーション

var UserSchema = new Schema(
{ 
    email : 
    { 
     type : String, 
     unique : true 
    }, 
    login_type : String, 
    username : 
    { 
     type : String, 
     index : true 
    }, 
    password : String 
} 

を使用していると私は参照

としてスキーマを投稿するユーザーのOIDを追加しました
var PostSchema = new Schema(
{ 
    author : {type : Schema.Types.ObjectId, ref : "User"}, 
    board_id : {type : Schema.Types.ObjectId, ref : "Board"}, 
    regDate : Number, 
    lastModDate : Number, 
    title : String, 
    content : String, 
} 

したがって、投稿はユーザーコレクションを参照して投稿したユーザーのusernameになることがあります。だから私はユーザーのユーザー名を取得したい、他は必要ありません。 このため、私は以下を使用しました。

var query = Post.find({"regDate" :{$lte : before}}, 
{ 
    _id : 1, 
    title : 1, 
    "author.username" : 1, 
    regDate : 1 
}) 

しかし、マングースはそれを無視した"author.username"。どうすれば私はちょうどauthorのユーザー名を取得できますか?

答えて

1

あなたはpopulate機能を使用する必要がありますし、そこに選択したフィールドを列挙する:

Post 
    .find({ regDate :{ $lte : before }}, 'title reqDate') 
    .populate('author', 'username') 
    .exec(); 
関連する問題