说明
likes
文档的blog
属性中。
我想编写一个查询来添加一个名为liked
的额外布尔属性,如果likes
数组包含输入ip,则为true。
问题
示例
[
{ _id:123123,
title: "title here",
desc: "some other text here",
likes: [
{ip: "10.0.0.1"},
{ip: "0.1.1.1"},
// ... a lot more {ip: "..."}
]
},
{
// the next blog document...
},
// all other blog documents...
]
我从查询输出中期望的是:
[
{ _id:123123,
title: "title here",
desc: "some other text here",
liked: true // if the array contains {ip: "10.0.0.1"}
]
},
{
_id:2,
title: "another"
desc: "some text",
liked: false
},
//...
]
注释
我正在使用他的官方MongoDB驱动程序用于NodeJS。 (see documentation)
到目前为止我想出了什么:
dbo.collection('blogs')
.aggregate([{ $project: {
title:1,
desc:1,
liked: {$cond: {if: {// TODO: not sure what to write here...}},
}}])