在MongoDB上未反映出来的Node.js中的修改模型

huangapple go评论60阅读模式
英文:

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);

以及我的数据库的截图

在MongoDB上未反映出来的Node.js中的修改模型

我会非常感激您的帮助!非常感谢!

英文:

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

在MongoDB上未反映出来的Node.js中的修改模型

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.

huangapple
  • 本文由 发表于 2023年1月7日 01:45:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/75034378.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定