Mongodb 包含数组的Mongoose模型
首先,我对MongoDB、Mongoose和Express还很陌生。我试图创建一个Mongoose模型,该模型有两个数组,我想用多个对象填充它们,称为itemSchema,但我不确定除了使用findOneAndUpdate之外,应该如何更新数组,但由于我的数组最初为空,因此在创建文档之前没有初始ID。使用我在下面定义的方法-食物数组中的任何现有数据都将替换为新数组。以下是我的模型-Mongodb 包含数组的Mongoose模型,mongodb,mongoose,Mongodb,Mongoose,首先,我对MongoDB、Mongoose和Express还很陌生。我试图创建一个Mongoose模型,该模型有两个数组,我想用多个对象填充它们,称为itemSchema,但我不确定除了使用findOneAndUpdate之外,应该如何更新数组,但由于我的数组最初为空,因此在创建文档之前没有初始ID。使用我在下面定义的方法-食物数组中的任何现有数据都将替换为新数组。以下是我的模型- const mongoose = require("mongoose"); const itemSchema =
const mongoose = require("mongoose");
const itemSchema = new mongoose.Schema({
id: String,
drinks: [
{
id: String,
name: {
type: String,
required: true
},
price: {
type: String,
required: true
},
description: {
type: String
},
date: {
type: Date,
default: Date.now
}
}
],
food: [
{
name: {
type: String,
required: true
},
price: {
type: String,
required: true
},
description: {
type: String
},
date: {
type: Date,
default: Date.now
}
}
]
});
module.exports = Item = mongoose.model("item", itemSchema);
我不知道我是否正确定义了模式。我知道它不是很枯燥(因为两个数组都包含相同的类型),但因为我相信这是一个非常简单的用例,我不想为饮料和食物定义两个单独的模式,因为我可以只创建一个模式
router.post("/food", async (req, res) => {
try {
// Create an object from the request that includes the name, price and description
const newItem = {
name: req.body.name,
price: req.body.price,
description: req.body.description
};
// pass the object to the Items model
let item = new Items(newItem);
// add to the comments array
console.log("the new comment ", newItem);
item.food.unshift(newItem);
item.save();
// return the new item array to confirm adding the new item is working.
res.json(item);
} catch (error) {
// Display an error if there is one.
res.send(404).json(error);
}
});
上述方法的问题来自于我应该如何更新数组。例如,我定义了下面的函数来更新食物数组,但是每次都会创建一个新数组。我相信这与没有Id参数有关,我可以使用Id参数为模型提供findOneAndUpdate方法。任何帮助都将不胜感激。提前谢谢。根据我的意见,您可以使您的模式更简单,因为在食品和饮料数组中,所有字段都是相同的,因此您只需将另一个字段作为
项目类型
,然后您就不需要为食品和饮料使用两个单独的子文档
const mongoose = require("mongoose");
const itemSchema = new mongoose.Schema({
id: String,
itemType: { type: String }, // FOOD or DRINK
name: {
type: String,
required: true
},
price: {
type: String,
required: true
},
description: {
type: String
},
date: {
type: Date,
default: Date.now
}
});
如果您想了解有关使用findOneAndUpdate()
在数组中更新的更多信息,那么我将解释使用此函数执行的两个简单任务
案例:1如果子文档的数组为空,则可以按如下方式在子文档中推送新文档:
var updatedData = await Model.findOneAndUpdate(
{
_id: doc._id
},
{
$push: {
drinks: {
name: drink.name,
price: drink.price,
description: drink.description,
}
},
},{ new: true }
).lean().exec();
var updatedData = await Model.findOneAndUpdate(
{
'drink._id': drinkId
},
{
$set: {
'drink.$.name': drink.name,
'drink.$.price': drink.price,
'drink.$.description': drink.description,
},
},{ new: true }
).lean().exec();
案例:2如果要按子单据id更新现有子单据,则可以按如下方式更新:
var updatedData = await Model.findOneAndUpdate(
{
_id: doc._id
},
{
$push: {
drinks: {
name: drink.name,
price: drink.price,
description: drink.description,
}
},
},{ new: true }
).lean().exec();
var updatedData = await Model.findOneAndUpdate(
{
'drink._id': drinkId
},
{
$set: {
'drink.$.name': drink.name,
'drink.$.price': drink.price,
'drink.$.description': drink.description,
},
},{ new: true }
).lean().exec();
根据我的意见,你可以使你的模式更简单,因为在你的食物和饮料数组中,所有字段都是相同的,因此你可以简单地将一个字段作为
itemType
,然后你不需要为食物和饮料取两个单独的子文档
const mongoose = require("mongoose");
const itemSchema = new mongoose.Schema({
id: String,
itemType: { type: String }, // FOOD or DRINK
name: {
type: String,
required: true
},
price: {
type: String,
required: true
},
description: {
type: String
},
date: {
type: Date,
default: Date.now
}
});
如果您想了解有关使用findOneAndUpdate()
在数组中更新的更多信息,那么我将解释使用此函数执行的两个简单任务
案例:1如果子文档的数组为空,则可以按如下方式在子文档中推送新文档:
var updatedData = await Model.findOneAndUpdate(
{
_id: doc._id
},
{
$push: {
drinks: {
name: drink.name,
price: drink.price,
description: drink.description,
}
},
},{ new: true }
).lean().exec();
var updatedData = await Model.findOneAndUpdate(
{
'drink._id': drinkId
},
{
$set: {
'drink.$.name': drink.name,
'drink.$.price': drink.price,
'drink.$.description': drink.description,
},
},{ new: true }
).lean().exec();
案例:2如果要按子单据id更新现有子单据,则可以按如下方式更新:
var updatedData = await Model.findOneAndUpdate(
{
_id: doc._id
},
{
$push: {
drinks: {
name: drink.name,
price: drink.price,
description: drink.description,
}
},
},{ new: true }
).lean().exec();
var updatedData = await Model.findOneAndUpdate(
{
'drink._id': drinkId
},
{
$set: {
'drink.$.name': drink.name,
'drink.$.price': drink.price,
'drink.$.description': drink.description,
},
},{ new: true }
).lean().exec();
谢谢,我认为添加item_type字段是实现我想要实现的目标的方法。谢谢,我认为添加item_type字段是实现我想要实现的目标的方法。