我有以下文件:
{
_id : 21353456,
username : "xy",
text : "asdf",
comments : [
{
username : "User1",
text : "hi",
},
{
username : "User2",
text : "hi1",
},
{
username : "User3",
text : "hi2",
},
{
username : "User4",
text : "hi3",
}
]
}
现在,我想使用聚合和项目获取用户名、文本和评论。此外,我还想知道评论数组中是否包含一个带有“User1”用户名的布尔值。我有以下代码,但它不起作用。
db.posttest.aggregate(
[
{
$project:
{
username: 1,
text: 1,
comments : 1,
hasComment: { $eq: [ "comments.$.username", "User1" ] },
_id: 0
}
}
]
)