简体   繁体   中英

how to find and update objects in an array in mongoDb arrays?

This is the Mongo db collection

{
"_id": "62035cd8a76fcc0e09f46c22",
"particulars":[
{
                "serialNo": 1,
                "item": "desk",
                "link": "http//",
                "images": "http//",
                "quantity": 13,
                "unitPrice": 100,
                "amount": 1300
            },
            {
                "serialNo": 2,
                "item": "desk",
                "link": "http//",
                "images": "http//",
                "quantity": 13,
                "unitPrice": 100,
                "amount": 1300
            },
            {
                "serialNo": 3,
                "item": "desk",
                "link": "http//",
                "images": "http//",
                "quantity": 13,
                "unitPrice": 100,
                "amount": 1300
            }
]
}

the array from frontend is this

procurement_details:[
{
                "serialNo": 1,
                "item": "ac",
                "link": "http//",
                "images": "http//",
                "quantity": 1,
                "unitPrice": 100,
                "amount": 100
            },
            {
                "serialNo": 2,
                "item": "fan",
                "link": "http//",
                "images": "http//",
                "quantity": 1,
                "unitPrice": 100,
                "amount": 100
            }
]

output i want. Need to replace the matching serialNo objects in particular with the objects in particulars

particulars:[
{
                "serialNo": 1,
                "item": "ac",
                "link": "http//",
                "images": "http//",
                "quantity": 1,
                "unitPrice": 100,
                "amount": 100
            },
            {
                "serialNo": 2,
                "item": "fan",
                "link": "http//",
                "images": "http//",
                "quantity": 1,
                "unitPrice": 100,
                "amount": 100
            },
            {
                "serialNo": 3,
                "item": "desk",
                "link": "http//",
                "images": "http//",
                "quantity": 13,
                "unitPrice": 100,
                "amount": 1300
            }
]

i need to replace the objects in particulars with the new objects from procurement_details with respect to the serialNo.so can anyone help me to figure this out and im using nodejs and mongoClient.

var bulk=await tickets.initializeOrderedBulkOp()

   for(procurement_detail of procurement_details){
    const resultt=await tickets.find({"_id":mongodb.ObjectId(ticketId),"particulars.serialNo":procurement_detail.serialNo},{projection:{"particulars.$":1,_id:0}}).toArray()
    console.log("resultts",resultt);
    await bulk.find({"_id":mongodb.ObjectId(ticketId),"particulars.serialNo":procurement_detail.serialNo}).update({
     
            $set:{"particulars.$":procurement_detail},
            $push:{ticketHistory:{$each:resultt[0].particulars}}
            
        })

    }
   

await bulk.execute()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM