英文:
Modified model in node.js not reflecting on MongoDB
问题
我刚刚修改了我的模型 Order,因为我需要一个值来满足条件。但不幸的是,我的集合上的新属性集 (orderStatus) 并没有反映在我的 MongoDB 数据库上。
我不需要为属性 (orderStatus) 输入一个值,因为我设置了默认值("Pending")。我尝试删除整个集合 orders 来查看是否有任何更改,但没有变化。我需要在我的 MongoDB 上做其他操作吗,或者在 git 上执行任何命令吗?
这是我的模型代码:
const mongoose = require("mongoose");
const orderSchema = new mongoose.Schema({
userId: {
type: String,
required: [true, "User ID is required!"]
},
products: [
{
productId: {
type: String,
required: [true, "Product ID is required!"]
},
quantity: {
type: Number,
required: [true, "Quantity is required!"],
min: 1,
default: 1
},
subtotal: {
type: Number,
required: [true, "Subtotal is required"],
default: 0
}
}
],
totalAmount: {
type: Number,
required: [true, "Total Amount is required!"],
default: 0
},
orderStatus: {
type: String,
required: [true, "Order Status is required!"],
default: "Pending"
},
purchasedOn: {
type: Date,
default: new Date
}
});
module.exports = mongoose.model("Order", orderSchema);
以及我的数据库的截图
我会非常感激您的帮助!非常感谢!
英文:
I just modified my model Order for my API because I need a value to pass a condition. But unfortunately, my new set of properties (orderStatus) on my collection did not reflect on my MongoDB database.
I don't need to input a value for the property (orderStatus) since I set a default value ("Pending"). I tried to delete the entire collection orders to see any changes but there were none. Is there something else I need to do on my MongoDB or any command on the git?
Here is my code for my model
const mongoose = require("mongoose");
const orderSchema = new mongoose.Schema({
userId: {
type: String,
required: [true, "User ID is required!"]
},
products: [
{
productId: {
type: String,
required: [true, "Product ID is required!"]
},
quantity: {
type: Number,
required: [true, "Quantity is required!"],
min: 1,
default: 1
},
subtotal: {
type: Number,
required: [true, "Subtotal is required"],
default: 0
}
}
],
totalAmount: {
type: Number,
required: [true, "Total Amount is required!"],
default: 0
},
orderStatus: {
type: String,
required: [true, "Order Status is required!"],
default:"Pending"
},
purchasedOn: {
type: Date,
default: new Date
}
});
module.exports = mongoose.model("Order", orderSchema);
and a screenshot of my database
I will really appreciate your help! Thank you so much!
答案1
得分: 1
你已经创建的文档不会反映您的新模式字段。因为这些字段只会在更新时反映出来。所以,请将orderStatus
设置为默认的"pending"状态。您必须使用更新方法迁移这些文档。这将是一次性的过程。
所以,在终端中,您可以编写以下代码并运行
db.collection.update({}, {$set: {orderStatus: "Pending"}}, {upsert: true, multi: true})
或者如果您对代码本身感到满意,您可以在您的项目中编写更新代码,像这样
await orderModel.updateMany({}, {$set: {orderStatus: "Pending"}}, {upsert: true})
并创建一个API来调用此代码。
在这个过程中,您将看到在您的文档中创建了一个名为orderStatus
的新键,其值为"pending"。
英文:
Your documents which were already created will not reflect your new schema field. Since those fields only will get reflected on the update only. So, add orderStatus
as pending as the default. You must migrate those documents by the update method. This is going to be a one-time process.
So, in the terminal, you can write the following code and run
db.collection.update({}, {$set: {orderStatus: "Pending"}}, {upsert: true, muti: true})
Or you are comfortable with the code itself you can write your update code in your project itself like this
await orderModel.updateMany({}, {$set: {orderStatus: "Pending"}}, {upsert: true})
and make an API and call this code.
In this process, you will see that in your documents that new key called orderStatus with pending value has been created.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论