2016-01-14 79 views
4

我正在使用該項目的MongooseDeepPopulate包。我有SchemaA,SchemaB,SchemaC,SchemaD。我的SchemaD,SchemaC連接到SchemaB,SchemaB連接到SchemaA。貓鼬深填充限制中間模型

我已經這樣做了。

var deepPopulate = require('mongoose-deep-populate')(mongoose); 
AlbumSong.plugin(deepPopulate, { 
    populate: { 
     'song.category': {select: 'name status'}, 
     'song.poetId': {select: 'name status'} 
    } 
}); 

song與分類和poetId進一步聯繫。限制字段​​從categorypoetId我成功。但我希望限制中間模型song的字段。我的查詢查詢就像

AlbumSong.find(condition) 
    .deepPopulate('song.category song.poetId') 
// .deepPopulate('song.category song.poetId' , '_id category poetId name nameHindi invalid status') // I tried this as well to limit records from song model as well. 
    .exec(function(err, playlist) { 
     callback(err, playlist); 
    }); 

我在哪裏弄錯了。

回答

1

如果要限制爲AlbumSong領域,你可以只使用由mongoose itself提供的,像這樣的功能:

AlbumSong.find(condition) 
    .select('_id category poetId name nameHindi invalid status') 
    .deepPopulate(...) 

Here is一個簡單的應用程序驗證這個想法。 模式是這樣的:

var userSchema = new Schema({ 
    name: String, 
    email: String 
}); 

var CommentSchema = new Schema({ 
    author : {type: Schema.Types.ObjectId, ref: 'User'}, 
    title: String, 
    body: String 
}) 

var PostSchema = new Schema({ 
    title: String, 
    author: { type: Schema.Types.ObjectId, ref: 'User' }, 
    comments: [{type: Schema.Types.ObjectId, ref: 'Comment'}], 
    body: String 
}); 

PostSchema.plugin(deepPopulate, { 
    populate: { 
    'author': { select: 'name' }, 
    'comments': { select: 'title author' }, 
    'comments.author': { select: 'name' }, 
    } 
}); 

deepPopulate設置上述相關authorcommentscomments.author限制領域。 要獲得職位和限制領域的職位本身,我用這個:

Post.find().select('title author comments').deepPopulate('author comments.author').exec(function(err, data) { 
    // process the data 
}); 

的數據是這樣的:

[{ 
    "_id": "56b74c9c60b11e201fc8563f", 
    "author": { 
     "_id": "56b74c9b60b11e201fc8563d", 
     "name": "Tester" 
    }, 
    "title": "test", 
    "comments": [ 
     { 
      "_id": "56b74c9c60b11e201fc85640", 
      "title": "comment1", 
      "author": { 
       "_id": "56b74c9b60b11e201fc8563e", 
       "name": "Poster" 
      } 
     } 
    ] 
}] 

所以對於職位本身我們只titlebody不選擇有)。 對於填充的記錄,所選字段也受到限制。

+0

它沒有工作。 :( – Sankalp