1

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"
}

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

6 Comments

thanks @ mickl but even after editing postman payload it still giving the same response though i have changed the payload { "orderId":"5e26be38c13b7149d0a95111", "submittedTo":"5e2555363405363bc4bf86c2" rest payload is same except changes in the tradecopies inside vendorOrder
@sachin I tried and it works. Can you paste your Order schema ?
const orderSchema = new mongoose.Schema( { vendorOrder: [ { publicationCode: { type: String }, publicationName: { type: String }, editionName: { type: String }, productCode: { type: String }, subscriptionCopies: { type: Number }, tradeCopies: { type: Number } } ], submittedTo: { type: mongoose.Schema.Types.ObjectId }, @mickl
@sachin looks fine. Here's my working example: pastebin.com/532U8zMX , please make sure that the database you're connected to already contains the collection and the document you're trying to modify (collection name should be orders)
i dont know whats the issue with my code, i am stuck in this, the same document with same id's is there in db i have modified it with the same query in mongoshell also the ids are same in the asked question
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.