我正在使用mongodb的vscode扩展。我试图将值存储在变量中,并且它返回未定义的值。查询工作正常。
var obj = db.categories.find({_id : "Space Opera"}, {_id : 0, parent : 1}) // undefined--编辑--
类别是表示图形数据结构的文档。
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: [],
},
]);图书收集对象示例如下:
{
ISBN: "0006",
title: "Book6",
pages: NumberInt("135"),
price: NumberDecimal("170.5"),
copies: NumberInt("10"),
language: "russian",
author: ["Author1", "Author2", "Author3"],
category: ["Science Fiction & Fantasy"],
genre: ["Genre-6", "Genre-0"],
character: ["Character-4", "Character-3"],
}我想存储这样一个变量:
var category = ["Space Opera"] -另一个问题
由于它是一个图形数据结构和“科幻小说”类别有两个父母,我想执行以下操作。
下面的代码适用于只有一个类别的书籍,并且该类别只有一个父类。不适用于科幻小说,我认为我需要一些条件检查该类别是否有多个父母,并取决于我想要的类别,更新类别。
db.books.aggregate([
{ $match: { category: "Space Opera" } },
{
$lookup:
{
from: "categories",
pipeline: [
{ $match: { _id: "Space Opera" } },
{ $project: { _id: 0, parent: { $first: "$parent" } } }
],
as: "category"
}
},
{ $set: { category: { $first: "$category.parent" } } }
]).toArray().forEach(function (doc) {
db.books.updateOne({ _id: doc._id }, { $set: { category: [doc.category] } });
})发布于 2021-03-10 08:37:54
mongodb中的属性"_id“是一个敏感的问题.),这是数据库存储文档ID的地方。与正常的关系数据库相反,这些IDs不是内写整数,而是由mongodb本身创建的对象Ids。因此,该查询返回未定义的内容,因为"Space“一开始不可能是对象Id,0也不能。如果您提供了更多的代码,我很乐意提供更多的帮助。
https://stackoverflow.com/questions/66560985
复制相似问题