MongoDB 使用来自另一个 collection 的另一个字段更新字段
MongoDB update field with another field from another collection
我有两个 collection:书籍和类别。类别 collections 表示树结构,使用 parents 和 children.
使它们成为嵌套类别
这本书可以有多个类别,并将它们存储在一个数组中。
示例:图书有类别,我想停用它并将其设置为 parent 类别。
这就是类别 collection 的填充方式。
db.categories.insertMany([
{
_id: "Space Opera",
ancestors: ["Science Fiction", "Fiction", "Science Fiction & Fantasy"],
parent: ["Science Fiction"],
},
{
_id: "Dystopian",
ancestors: ["Science Fiction", "Fiction", "Science Fiction & Fantasy"],
parent: ["Science Fiction"],
},
{
_id: "Cyberpunk",
ancestors: ["Science Fiction", "Fiction", "Science Fiction & Fantasy"],
parent: ["Science Fiction"],
},
{
_id: "Science Fiction",
ancestors: ["Fiction", "Science Fiction & Fantasy"],
parent: ["Fiction", "Science Fiction & Fantasy"],
},
{
_id: "Fantasy",
ancestors: ["Science Fiction & Fantasy"],
parent: ["Science Fiction & Fantasy"],
},
{
_id: "Science Fiction & Fantasy",
ancestors: [],
parent: [],
},
{
_id: "Fiction",
ancestors: [],
parent: [],
},
]);
还有,如何查询这个,只得到值“Science Fiction”(注意是存储在数组中)?
db.categories.find({_id : "Space Opera"}, {_id : 0, parent : 1})[0].parent // Did not work
db.categories.find({_id : "Space Opera"}, {_id : 0, parent : 1}) // find parent
// result
[
{
"parent": [
"Science Fiction"
]
}
]
db.books.update(
{title : "Book1"},
{$set : {category : [**PARENT CATEGORY**]}}
)
我相信我可以在 books.update()
中使用上面的代码
我可以将它存储在一个单独的变量中,但在 vscode 中它给了我未定义的。
内部查询并没有像之前那样给我正确的值,但我想你明白了。
db.books.update(
{title : "Book1"},
{$set : {category : [db.categories.find({_id : "Space Opera"}, {_id : 0, parent : 1})]}}
)
您可以通过此聚合管道获得的父项:
db.categories.aggregate([
{ $match: { _id: "Space Opera" } },
{ $project: { _id: 0, parent: { $first: "$parent" } } }
])
甚至
db.categories.aggregate([
{ $match: { _id: "Space Opera" } },
{ $project: { _id: 0, parent: { $first: "$parent" } } }
]).toArray().shift().parent
要加入集合,您必须使用 $lookup 运算符。请记住,像 MongoDB 这样的 NoSQL 数据库并未针对 join/lookup 进行优化。在现实生活中你应该寻找更好的设计。
db.books.aggregate([
{ $match: { title: "Book1" } },
{
$lookup:
{
from: "categories",
pipeline: [
{ $match: { _id: "Space Opera" } },
{ $project: { _id: 0, parent: { $first: "$parent" } } }
],
as: "category"
}
},
{ $set: { category: { $first: "$category.parent" } } }
])
如果您想更新 现有集合,则必须为其创建一个循环:
db.books.aggregate([...]).forEach(function (doc) {
db.books.updateOne({ _id: doc._id }, { $set: { category: doc.category } });
})
我有两个 collection:书籍和类别。类别 collections 表示树结构,使用 parents 和 children.
使它们成为嵌套类别这本书可以有多个类别,并将它们存储在一个数组中。
示例:图书有类别,我想停用它并将其设置为 parent 类别。
这就是类别 collection 的填充方式。
db.categories.insertMany([
{
_id: "Space Opera",
ancestors: ["Science Fiction", "Fiction", "Science Fiction & Fantasy"],
parent: ["Science Fiction"],
},
{
_id: "Dystopian",
ancestors: ["Science Fiction", "Fiction", "Science Fiction & Fantasy"],
parent: ["Science Fiction"],
},
{
_id: "Cyberpunk",
ancestors: ["Science Fiction", "Fiction", "Science Fiction & Fantasy"],
parent: ["Science Fiction"],
},
{
_id: "Science Fiction",
ancestors: ["Fiction", "Science Fiction & Fantasy"],
parent: ["Fiction", "Science Fiction & Fantasy"],
},
{
_id: "Fantasy",
ancestors: ["Science Fiction & Fantasy"],
parent: ["Science Fiction & Fantasy"],
},
{
_id: "Science Fiction & Fantasy",
ancestors: [],
parent: [],
},
{
_id: "Fiction",
ancestors: [],
parent: [],
},
]);
还有,如何查询这个,只得到值“Science Fiction”(注意是存储在数组中)?
db.categories.find({_id : "Space Opera"}, {_id : 0, parent : 1})[0].parent // Did not work
db.categories.find({_id : "Space Opera"}, {_id : 0, parent : 1}) // find parent
// result
[
{
"parent": [
"Science Fiction"
]
}
]
db.books.update(
{title : "Book1"},
{$set : {category : [**PARENT CATEGORY**]}}
)
我相信我可以在 books.update()
中使用上面的代码我可以将它存储在一个单独的变量中,但在 vscode 中它给了我未定义的。 内部查询并没有像之前那样给我正确的值,但我想你明白了。
db.books.update(
{title : "Book1"},
{$set : {category : [db.categories.find({_id : "Space Opera"}, {_id : 0, parent : 1})]}}
)
您可以通过此聚合管道获得的父项:
db.categories.aggregate([
{ $match: { _id: "Space Opera" } },
{ $project: { _id: 0, parent: { $first: "$parent" } } }
])
甚至
db.categories.aggregate([
{ $match: { _id: "Space Opera" } },
{ $project: { _id: 0, parent: { $first: "$parent" } } }
]).toArray().shift().parent
要加入集合,您必须使用 $lookup 运算符。请记住,像 MongoDB 这样的 NoSQL 数据库并未针对 join/lookup 进行优化。在现实生活中你应该寻找更好的设计。
db.books.aggregate([
{ $match: { title: "Book1" } },
{
$lookup:
{
from: "categories",
pipeline: [
{ $match: { _id: "Space Opera" } },
{ $project: { _id: 0, parent: { $first: "$parent" } } }
],
as: "category"
}
},
{ $set: { category: { $first: "$category.parent" } } }
])
如果您想更新 现有集合,则必须为其创建一个循环:
db.books.aggregate([...]).forEach(function (doc) {
db.books.updateOne({ _id: doc._id }, { $set: { category: doc.category } });
})