Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

how to update all the objects in mongoose at once?

I want to update all the objects in the array in mongoose when the query is fired.

I want to do something like this.

Initial data

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

{
  "_id": { "$oid": "64e460061cbb782e29b8b067" },
  "threadId": { "$oid": "64e460061cbb782e29b8b065" },
  "contactId": { "$oid": "64e460051cbb782e29b8b063" },
  "status": true,
  "conversation": [
    {
      "response": "ABC",
      "status": false,
      "_id": { "$oid": "64e460051cbb782e29b8b061" },
      "responsed": false
    },
    {
      "response": "DEF",
      "status": false,
      "_id": { "$oid": "64e460051cbb782e29b8b061" },
      "responsed": false
    },
    {
      "response": "GHI",
      "status": false,
      "_id": { "$oid": "64e460051cbb782e29b8b061" },
      "responsed": false
    },
    {
      "response": "JKL",
      "status": false,
      "_id": { "$oid": "64e460051cbb782e29b8b061" },
      "responsed": false
    }
  ],
  "__v": { "$numberInt": "0" }
}

Updated data after query

{
  "_id": { "$oid": "64e460061cbb782e29b8b067" },
  "threadId": { "$oid": "64e460061cbb782e29b8b065" },
  "contactId": { "$oid": "64e460051cbb782e29b8b063" },
  "status": true,
  "conversation": [
    {
      "response": "ABC",
      "status": false,
      "_id": { "$oid": "64e460051cbb782e29b8b061" },
      "responsed": true
    },
    {
      "response": "DEF",
      "status": false,
      "_id": { "$oid": "64e460051cbb782e29b8b061" },
      "responsed": true
    },
    {
      "response": "GHI",
      "status": false,
      "_id": { "$oid": "64e460051cbb782e29b8b061" },
      "responsed": true
    },
    {
      "response": "JKL",
      "status": false,
      "_id": { "$oid": "64e460051cbb782e29b8b061" },
      "responsed": true
    }
  ],
  "__v": { "$numberInt": "0" }
}

I just want to toggle each object’s responsed value to true if it is false.

I have written by myself but it is of no use, it just updates the first object rest all the objects remain untouched.

await Conversations.updateOne(
  { threadId: "64e460061cbb782e29b8b065", "conversation.responsed": false },
  { "conversation.$.responsed": true }
);

The model for above query is as follows:

const conversationsSchema = new schema({
    threadId:{
        type: schema.Types.ObjectId,
        ref: 'threads',
        unique: true
    },
    contactId: {
        type: schema.Types.ObjectId,
        ref: 'contacts',
        unique: true
    },
    status:{
        type: Boolean,
        default: true
    },
    conversation:[
        {
            response: {
                type: String,
                required: false
            },
            status: {
                type: Boolean,
                default: true
            },
            responsed: {
                type: Boolean,
                default: false
            }
        }
    ]
})

Thanks for the help.

>Solution :

One option is:

db.collection.updateOne({
  threadId: ObjectId("64e460061cbb782e29b8b065"),
  "conversation.responsed": false
},
{
  $set: {
    "conversation.$[].responsed": true
  }
})

See how it works on the playground example

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading