预期结果:只获取引用文档的“content”属性。
function getAuthorPost(username){
return User.findOne({ username })
.populate('posts').exec((err, posts) => {
console.log("posts: ", posts)
})
}
它返回:
{ posts:
[ { author: [Array],
_id: 5c8c31e2e204566cd7ecef4a,
content: 'April' },
{ author: [Array],
_id: 5c8c31e2e204566cd7ecef4b,
content: 'Great spring' },
{ author: [Array],
_id: 5c8c31e2e204566cd7ecef4c,
content: 'Natural path' },
{ author: [Array],
_id: 5c8c31e2e204566cd7ecef4d,
content: 'Better cherry' },
{ author: [Array],
_id: 5c8c31e2e204566cd7ecef4e,
content: 'Water life' } ],
_id: 5c8c255fed727439d6d5b860,
username: 'Frank',
__v: 1
}
我只需要获取内容字段。
当我这么做的时候
.populate('posts', {posts:"content"}).exec((err, posts)
它返回的id如下:
{
posts:
[ { _id: 5c8c31e2e204566cd7ecef4a },
{ _id: 5c8c31e2e204566cd7ecef4b },
{ _id: 5c8c31e2e204566cd7ecef4c },
{ _id: 5c8c31e2e204566cd7ecef4d },
{ _id: 5c8c31e2e204566cd7ecef4e } ],
_id: 5c8c255fed727439d6d5b860,
username: 'youmbo',
__v: 1
}
谢谢你的提示