此查询正在 mongo shell 中运行
db.orders.updateOne({ _id: ObjectId("5e26be38c13b7149d0a95111"),
"submittedTo":ObjectId("5e2555363405363bc4bf86c2") },
{ $set: {
... "vendorOrder" : [
... {
... "_id" : ObjectId("5e26be38c13b7149d0a95113"),
... "publicationCode" : "TOI",
... "publicationName" : "Times of India",
... "editionName" : "chennai city",
... "productCode" : "TCE1",
... "subscriptionCopies" : 70,
... "tradeCopies" : 9
... },
... {
... "_id" : ObjectId("5e26be38c13b7149d0a95112"),
... "publicationCode" : "ET",
... "publicationName" : "Economic Times",
... "editionName" : "chennai city",
... "productCode" : "ECE1",
... "subscriptionCopies" : 20,
... "tradeCopies" : 4
... }
... ]}})
Mongo shell 响应: { "acknowledged": true, "matchedCount": 1, "modifiedCount": 1 }
但它无法在nodejs中工作:这是我迄今为止尝试过的 查询1:
exports.editOrder = async (req, res, next) => {
const { orderId, dealerId, vendorOrder } = req.body;
try {
const orders = await Order.updateOne(
{
_id: orderId,
submittedTo:dealerId
},
{ $set: { vendorOrder } }
);
res.status(200).json({
orders,
message: "order submitted"
});
} catch (error) {
res.send(error);
}
};
查询2
exports.editOrder = async (req, res, next) => {
const { orderId, dealerId, vendorOrder } = req.body;
try {
const orders = await Order.updateOne(
{
_id: mongoose.Types.ObjectId(orderId),
submittedTo: mongoose.Types.ObjectId(dealerId)
},
{ $set: { vendorOrder: vendorOrder } }
);
res.status(200).json({
orders,
message: "order submitted"
});
} catch (error) {
res.send(error);
}
};
查询3:与双“”其余代码完全相同
const { orderId, dealerId, vendorOrder } = req.body;
try {
const orders = await Order.updateOne(
{
"_id": mongoose.Types.ObjectId(orderId),
"submittedTo": mongoose.Types.ObjectId(dealerId)
},
{ $set: { "vendorOrder": vendorOrder } }
);
postman JSON:
{
"orderId":"5e26be38c13b7149d0a95111",
"submittedTo":"5e2555363405363bc4bf86c2",
"vendorOrder" : [
{
"_id" : "5e26be38c13b7149d0a95113",
"publicationCode" : "TOI",
"publicationName" : "Times of India",
"editionName" : "chennai city",
"productCode" : "TCE1",
"subscriptionCopies" : 70,
"tradeCopies" : 90
},
{
"_id" : "5e26be38c13b7149d0a95112",
"publicationCode" : "ET",
"publicationName" : "Economic Times",
"editionName" : "chennai city",
"productCode" : "ECE1",
"subscriptionCopies" : 20,
"tradeCopies" : 40
}
]
postman 响应
{
"orders": {
"n": 0,
"nModified": 0,
"ok": 1
},
"message": "order submitted"
}
最佳答案
两点说明:
查询 #2(或 #3)是正确的 - 您应该始终使用 mongoose.Types.ObjectId(...)
将字符串转换为 ObjectId
,因为这两个字段都作为字符串存储在数据库中。
代码的下一个问题是解构:
const { orderId、dealerId、vendorOrder } = req.body;
需要 dealerId
,而您的负载包含 subscribedTo
字段,因此您应该尝试:
const { orderId, submittedTo, vendorOrder } = req.body;
const orders = await Order.updateOne(
{
_id: mongoose.Types.ObjectId(orderId),
submittedTo: mongoose.Types.ObjectId(submittedTo)
},
{ $set: { vendorOrder: vendorOrder } }
);
效果很好(请记住,如果 vendorOrder
变量和存储在数据库中的 vendorOrder
之间没有更改,nModified
将返回为 0
。
关于node.js - 更新在 shell 中工作但不在 Nodejs 驱动程序中工作的查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59917877/