gpt4 book ai didi

node.js - 在 mongodb 数组中查找嵌套字段

转载 作者:行者123 更新时间:2023-12-04 14:54:55 25 4
gpt4 key购买 nike

架构

[
{
"_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 , authorposts属性。 author属性是 $refposts属性是一个文档数组。 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阵列
  • $lookupusers收藏和传递posts.author作为 localField
  • $unwind解构author阵列
  • $group来自 _id并重建 posts数组并获取其他必填字段的第一个值
  • $lookupusers并通过 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
    }
    }
    ])
    Playground

    关于node.js - 在 mongodb 数组中查找嵌套字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68241530/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com