猫鼬保存内部文档创建一个包含文档的文档
mongoose save internal documents creating a document that contains a document
我正在学习 mongoose,我对如何保存多个文档有疑问:
// Product.js
const categorySchema = mongoose.Schema(
{ name: String },
{ collection: "categories" }
);
const productSchema = mongoose.Schema(
{ name: String, category: categorySchema },
{ collection: "products" }
);
modules.exports = mongoose.model("Product", productSchema);
我的想法是,当我以这种方式创建产品时
const Product = require("./Product.js")
const product = new Product({name: 'Soccer Ball', category: {name: "Sports"})
await product.save()
我想获取 collection 产品中的文档以及 collection 类别中的文档
怎么可能
提前致谢
PD:我得到了这个,但类别没有保存在 collection
{
"msg": "Product created succesfully",
"ok": true,
"product": {
"name": "Soccer ball",
"category": {
"name": "Sports",
"_id": "6275df4c8149967bea21e7c0"
},
"_id": "6275df4c8149967bea21e7bf",
"__v": 0
}
}
您应该将 Product
的 category
定义为 ref
属性:
// Product.js
const categorySchema = mongoose.Schema(
{ name: String },
{ collection: 'categories' }
);
const productSchema = mongoose.Schema(
{
name: String,
category: { type: mongoose.Schema.Types.ObjectId, ref: 'categories' },
},
{ collection: 'products' }
);
modules.exports = {
Category: mongoose.model('Category', categorySchema),
Product: mongoose.model('Product', productSchema),
}
为此,您需要将类别的 _id
分配给新的 Product
:
const { Category } = require("./Product.js")
const { Product } = require("./Product.js")
// Create category (supposing it is not present)
const category = new Category({ name: "Sports" })
await category.save()
// Add category _id to product
const product = new Product({name: 'Soccer Ball', category: category._id})
await product.save()
最后,您将能够使用 populate
检索产品:
const product = await Product.findById(<product_id>).populate('categories').exec()
此查询应给出与之前相同的结果,但 Category
数据将从 Category
集合中的引用加载。
我正在学习 mongoose,我对如何保存多个文档有疑问:
// Product.js
const categorySchema = mongoose.Schema(
{ name: String },
{ collection: "categories" }
);
const productSchema = mongoose.Schema(
{ name: String, category: categorySchema },
{ collection: "products" }
);
modules.exports = mongoose.model("Product", productSchema);
我的想法是,当我以这种方式创建产品时
const Product = require("./Product.js")
const product = new Product({name: 'Soccer Ball', category: {name: "Sports"})
await product.save()
我想获取 collection 产品中的文档以及 collection 类别中的文档
怎么可能
提前致谢
PD:我得到了这个,但类别没有保存在 collection
{
"msg": "Product created succesfully",
"ok": true,
"product": {
"name": "Soccer ball",
"category": {
"name": "Sports",
"_id": "6275df4c8149967bea21e7c0"
},
"_id": "6275df4c8149967bea21e7bf",
"__v": 0
}
}
您应该将 Product
的 category
定义为 ref
属性:
// Product.js
const categorySchema = mongoose.Schema(
{ name: String },
{ collection: 'categories' }
);
const productSchema = mongoose.Schema(
{
name: String,
category: { type: mongoose.Schema.Types.ObjectId, ref: 'categories' },
},
{ collection: 'products' }
);
modules.exports = {
Category: mongoose.model('Category', categorySchema),
Product: mongoose.model('Product', productSchema),
}
为此,您需要将类别的 _id
分配给新的 Product
:
const { Category } = require("./Product.js")
const { Product } = require("./Product.js")
// Create category (supposing it is not present)
const category = new Category({ name: "Sports" })
await category.save()
// Add category _id to product
const product = new Product({name: 'Soccer Ball', category: category._id})
await product.save()
最后,您将能够使用 populate
检索产品:
const product = await Product.findById(<product_id>).populate('categories').exec()
此查询应给出与之前相同的结果,但 Category
数据将从 Category
集合中的引用加载。