[英]Applying $cond inside subset in Mongodb/Mongoose Aggregation
Lets say I have this document: 可以说我有这个文件:
{ "_id" : 1, "first_name" : "Matt", "roles": ['editor', 'publisher', 'siteAdmin'] }
{ "_id" : 1, "first_name" : "Harry", "roles": ['publisher', 'siteAdmin'] }
{ "_id" : 1, "first_name" : "Rob", "roles": ['editor'] }
Now I would like to use mongoose aggregation ( $cond
) to output this document. 现在,我想使用猫鼬聚合( $cond
)输出此文档。
How do I get this? 我怎么得到这个? I would appreciate your help in this. 谢谢您的帮助。
{ "first_name" : "Matt", "isSiteAdmin": true }
{ "first_name" : "Harry", "isSiteAdmin": true }
{ "first_name" : "Rob", "isSiteAdmin": false }
A pipeline like the following should work for you 如下所示的管道应该为您工作
YourModel.aggregate([
{
$project: {
_id: 0,
first_name: 1,
isSiteAdmin: {
$setIsSubset: [["siteAdmin"], "$roles"]
}
}
}
])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.