skip to Main Content

Here’s the document of mongodb:

{
  _id: new ObjectId("646fa9b33e467de3f38b2418"),
  price: 3500,
  orderConfirmation: false,
  dateConfirmationOrder: null,
  dateDelivered: null,
  clothID: '6450c20d7fd78c92313cd348',
  clientName: 'irtaza',
  clientNumber: 3320523524,
  desc: '11',
  address: 'Ahsan road, kala gujran, jhelum',
  marketerPayment: 200,
  marketerPaymentStatus: false,
  quantity: 1,
  deliveryStatus: false,
  marketerid: '644fae86bd576878837479be',
  length: '39',
  chest: '21.5',
  waist: '22',
  daman: '22.5',
  sleeves: '23.5',
  shoulders: '17.5',
  colloar: '14.5',
  armhole: '9.5',
  returnDate: null,
  returnDeliveredDate: null,
  returnStatus: false,
  returnDeliveredStatus: false,
  returnSentBackStatus: false,
  returnSentBackDate: null,
  returnDesc: '',
  dateOrdered: 2023-05-25T18:32:19.893Z,
  __v: 0
}

and here’s the query:

orderModel.findOneAndUpdate(
          {
            _id: id,
            marketerid: marketerid,
          },
          {
            $set: {
              returnStatus: true,
              returnDate: Date.now,
            },
          },
          {
            writeConcern: {
              w: 1,
            },
          }
        );

I cannot find out the mistake why my record isn’t getting update after this query!!!

Solving a Basic Update query problem!!!

2

Answers


  1. Added parentheses to call the function ex.Date.now()

    orderModel.findOneAndUpdate(
        {
            _id: id,
            marketerid: marketerid,
        },
        {
            $set: {
                returnStatus: true,
                returnDate: Date.now(),
            },
        },
        {
            writeConcern: {
                w: 1,
            },
        }
    );
    
    Login or Signup to reply.
  2. To find document by id, you need to use new ObjectId() instead of passing id as string.
    Date.now returns a function, so do Date.now() to get current date timestamp.

    import { MongoClient, ServerApiVersion, ObjectId } from "mongodb";
    
    //... some code 
    
    orderModel.findOneAndUpdate({
      _id: new ObjectId(id),
      marketerid: marketerid,
    },
    {
      $set: {
        returnStatus: true,
        returnDate: Date.now(),
      },
    },
    {
      writeConcern: {
        w: 1,
      },
    });
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search