2

I have been trying to update the order status uisng nodejs and mongoose. But i came to problem on updating the status.

I have been trying to access the cart from ORDER document (table). I want to change the status inside cart from 'packed' to 'shifted'. I have been trying all day long but could not find the solution. Please help me with it. How can i select the cart and change the status in mongoose using nodejs?

ORDER

{
    "_id": {
        "$oid": "60a9a8b2bc65933bb4c22a3b"
    },
    "cart": [{
        "_id": {
            "$oid": "60a8bcafe7815c2950de4b28"
        },
        "userId": "60a3ad7fb51de12a5472de37",
        "count": "1",
        "status": "packed",
        "__v": 0
    }],
    "userId": "60a3ad7fb51de12a5472de37",
    "fName": "jay sree",
    "street": "5th cross hindurous",
    
}

MODEL

const mongoose = require('mongoose')

const schema = mongoose.Schema

const orderSchema = new schema(
  {
    userId: {
      type: String,
      required: true
    },
      cart: [{
          type: Object
      }],
      fName: {
      type: String,
      required: true
    },
    street: {
      type: String,
    },
  },
  {
    timestamps: true,
  }
);

module.exports = mongoose.model("Order", orderSchema)

Controller

exports.postUpdateStatus = (req, res, next) => {
    let status = req.params.status //shifted
    let productId =  mongoose.Types.ObjectId(req.params.productid )//60a8bcafe7815c2950de4b28
    let orderId = mongoose.Types.ObjectId(req.params.orderid )//60a9a8b2bc65933bb4c22a3b
    Order.find({id: orderId})
    .select({ cart: {$elemMatch: {id: productId}}})
    .then(product => {
        res.jsonp(product)
    })
    .catch(err => {
        console.log(err)
    })
}

1 Answer 1

2

You can call the findOneAndUpdate function and use the positional operator to update the matching cart-item. Something like:

Order.findOneAndUpdate({
    "_id": "60a9a8b2bc65933bb4c22a3b",
    "cart._id": "60a8bcafe7815c2950de4b28"
}, {
    "$set": {
        "cart.$.status": "shifted"
    }
}, {
    new: true
});

Note that I'm using the new-option here, in order for findOneAndUpdate to return the updated document.

Here's an example on mongoplayground where you can play around with the query: https://mongoplayground.net/p/5sW7NB1mJK7

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

8 Comments

I tried but its not updating anything. Where could I have done wrong? do I need to upload more information?
Can you try converting your ids to mongoose ids explicitely? E.g. let productId = mongoose.Types.ObjectId(req.params.productid); Do this for both ids.
yes ! tried that as well still no change. is there any alternative?
i did some changes Order.findOneAndUpdate({"cart._id": productId }, { $push: {"cart.status": status}}, {new: true}) and made my code like this and now i am geting a error which says cannot create a field. any idea what is the issue?
Thank you. It worked. Just i updaded the controller as you said and I had Problem with model.
|

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.