Reputation: 103
this query is working in 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 response: { "acknowledged" : true, "matchedCount" : 1, "modifiedCount" : 1 }
but its not working in nodejs:here is what i have tried so far query1:
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);
}
};
query2
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);
}
};
query 3: with double "" rest code is exactly the same
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 RESPONSE
{
"orders": {
"n": 0,
"nModified": 0,
"ok": 1
},
"message": "order submitted"
}
Upvotes: 1
Views: 43
Reputation: 49945
Two remarks:
Query #2 (or #3) is correct - you should always cast strings to ObjectId
using mongoose.Types.ObjectId(...)
since both fields are stored as string in your database.
The next issue with your code is that your destructuring:
const { orderId, dealerId, vendorOrder } = req.body;
expects dealerId
while your payload contains submittedTo
field, so you should try:
const { orderId, submittedTo, vendorOrder } = req.body;
const orders = await Order.updateOne(
{
_id: mongoose.Types.ObjectId(orderId),
submittedTo: mongoose.Types.ObjectId(submittedTo)
},
{ $set: { vendorOrder: vendorOrder } }
);
which works fine (keeping in mind that nModified
will be returned as 0
if there are no changes between vendorOrder
variable and vendorOrder
being stored in your database.
Upvotes: 1