用一个示例描述场景:
从下列数据中找出每个人的第一(id是1)爱好

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
[
{
"_id": ObjectId("61975535a73483311a5670a7"),
"name": "章三",
"hobbies": [{ "id": 1, "name": "足球" }, { "id": 2, "name": "篮球" }]
},
{
"_id": ObjectId("61975549a73483311a5670a8"),
"name": "李四",
"hobbies": [{ "id": 1, "name": "篮球" },{ "id": 2, "name": "羽毛球" }]
},
{
"_id": ObjectId("61975564a73483311a5670a9"),
"name": "王五",
"hobbies": [{ "id": 1, "name": "书法" },{ "id": 2, "name": "篆刻" },{ "id": 3, "name": "围棋" }]
}
]

最后期望得到的数据是

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
[
{
"_id": ObjectId("61975535a73483311a5670a7"),
"name": "章三",
"hobbies": [{ "id": 1, "name": "足球" }]
},
{
"_id": ObjectId("61975549a73483311a5670a8"),
"name": "李四",
"hobbies": [{ "id": 1, "name": "篮球" }]
},
{
"_id": ObjectId("61975564a73483311a5670a9"),
"name": "王五",
"hobbies": [{ "id": 1, "name": "书法" }]
}
]

使用 projection 数组占位符$

根据find的第二个参数 projection 过滤掉数组中不符合条件的元素,示例如下:

阅读全文 »

这个小点每次都要搜索,记一下还是有必要的。

1
icanhazip.com  # 访问该网址查看IP