node.js - 在 mongodb 数组中查找嵌套字段
问题描述
架构
[
{
"_id" : ObjectId("60e09c90402cbd625d7a8162"),
"title" : "...",
"author" : DBRef("users", ObjectId("60ce9146f41866120ee70c0d")),
"posts" : [ ]
}
{
"_id" : ObjectId("60e09c97402cbd625d7a8163"),
"title" : "...",
"author" : DBRef("users", ObjectId("60ce9146f41866120ee70c0d")),
"posts" : [ ]
}
{
"_id" : ObjectId("60e09d06402cbd625d7a8164"),
"title" : "...",
"author" : DBRef("users", ObjectId("60ce9146f41866120ee70c0d")),
"posts" : [ ]
}
{
"_id" : ObjectId("60e09d07402cbd625d7a8165"),
"title" : "...",
"author" : DBRef("users", ObjectId("60ce9146f41866120ee70c0d")),
"posts" : [
{
"_id" : ObjectId("60e12300e931cd14c03ecd89"),
"title" : "...",
"author" : DBRef("users", ObjectId("60ce9146f41866120ee70c0d"))
},
{
"_id" : ObjectId("60e12305e931cd14c03ecd8a"),
"title" : "...",
"author" : DBRef("users", ObjectId("60ce9146f41866120ee70c0d"))
}
]
}
]
每个文档都有一个title
,author
和posts
属性。author
属性是$ref ,posts
属性是文档数组。每个文件posts
也会有title, author
。
我想要的是
我想获取所有带有作者的文档,并且它是posts
(带有作者)。
询问
db.blogs.aggregate([
{ $project: {title:1,author:1,"posts.title":1,"posts._id":1,"posts.author":1} },
{ "$lookup": { "from": "users", "localField": "author.$id", "foreignField": "_id", "as": "author" } },
{ "$lookup": { "from": "users", "localField": "posts.author.$id", "foreignField": "_id", "as": "posts.author" } }
])
回复
[
{
"_id": '..',
"title": "..",
"author": [
{
// author details
}
],
"posts": {
"author": []
}
},
{
"_id": '..',
"title": "...",
"author": [
{
// author details
}
],
"posts": {
"author": []
}
},
{
"_id": '..',
"title": "..",
"author": [
{
// author details
}
],
"posts": {
"author": []
}
},
{
"_id": ...,
"title": "2 Type SR Blog, my first blog.",
"author": [
{
// author details
}
],
"posts": {
"author": [
{
// author details
}
]
}
}
]
用户集合
{
"_id" : ObjectId("60ce9146f41866120ee70c0d"),
"name" : "Rahul kumar",
"status" : "A Fake Developer",
"__v" : 0,
"pic" : "https://res.cloudinary.com/bdevg/image/upload/v1604287418/pic_xexz8o.jpg"
}
问题
您可以看到最后一个文档,它只有posts
属性。它还应该包含title
属性。
解决方案
$unwind
解构posts
数组$lookup
users
收集并作为posts.author
localField传递$unwind
解构author
数组$group
通过_id
并重建posts
数组并获取其他必填字段的第一个值$lookup
使用users
并author
作为 localField传递$unwind
解构author
数组
db.blogs.aggregate([
{
$unwind: {
path: "$posts.author",
preserveNullAndEmptyArrays: true
}
},
{
"$lookup": {
"from": "users",
"localField": "posts.author",
"foreignField": "_id",
"as": "posts.author"
}
},
{
$unwind: {
path: "$posts.author",
preserveNullAndEmptyArrays: true
}
},
{
$group: {
_id: "$_id",
title: { $first: "$title" },
posts: { $push: "$posts" },
author: { $first: "$author" }
}
},
{
"$lookup": {
"from": "users",
"localField": "author",
"foreignField": "_id",
"as": "author"
}
},
{
$unwind: {
path: "$author",
preserveNullAndEmptyArrays: true
}
}
])
推荐阅读
- visual-studio-2019 - devenv.exe 退出而不是构建(Visual Studio 社区 2019)
- sql - 列出所有演员的名字和姓氏及其以字母“P”开头的角色
- reactjs - 我应该如何将我的商店保存在我的 Java 实体模型的 react mobx 中?
- asp.net-core - 如何使用 NSwag 添加自定义操作
- ajaxcontroltoolkit - 弹出日历看起来不正确
- javascript - 量角器测试 - 无法将未定义或 null 转换为对象
- excel - 使用 .Formula 对不断变化的单元格范围求和
- python - 我对使用 Xgboost 的 hyperopt 包的 fmin() 函数有很大的帮助
- spring-boot - spring 如何使用第二个数据源?
- python - 如何根据 Python 3.x 中的输入类型查找用户输入的类型并打印不同的值