我在mongoDB数据库中有两个集合:书籍和作者。
在一本书的文档中,有作者收藏的一位作者的记录。
如何通过insertedId获得作者文档?
书籍文档示例:
_id: new ObjectId("6350f2eaf7dd9bfc16392a19"),
name: 'the teacher',
discreption: 'very good book',
published: '1.1.2022',
author: {
acknowledged: true,
insertedId: new ObjectId("6350f2e9f7dd9bfc16392a17")
},
pages: 500
},发布于 2022-10-20 07:34:09
您可以通过以下方式填充作者文档
book.find().populate({path: "author.insertedId"}),否则
db.collectionName.find().populate("author.insertedId"),否则
db.book.aggregate([
{
"$lookup": {
"from": "author",
"localField": "author.insertedId",
"foreignField": "_id",
"as": "author"
}
}
]https://stackoverflow.com/questions/74135810
复制相似问题