Home:ALL Converter>Pull element from an array of arrays MongoDb

Pull element from an array of arrays MongoDb

Ask Time:2022-05-18T23:48:58         Author:beginnersLuck.css

Json Formatter

I am struggling to find the format for a query to remove an element (with an _id) from an array of arrays in Mongo.

When looking at the docs I couldn't find anything that was similar to what I have https://www.mongodb.com/docs/manual/reference/operator/update/pull/#up._S_pull

I know: the _id of the document in MySchema and the _id of the array element in innerArray.

I don't know the outerArray _id

Could someone help point out where I went wrong Thank you!

This is an example of the data (imagine the _id in ObjectId)

{
  outerArray:[
           {
             _id: 1
             innerArray: [{_id: 23, name: '123'}, {_id: 13, name: 'asdac'} ] 
           },
           {
             _id: 2,
             innerArray: [{_id: 16,name:'asf' }, {_id: 18,name:'asf' } ] 
           },
           {
            _id: 3,
            innerArray: [{_id: 136,name:'asf' }, {_id: 128,name:'asf' } ] 
           }
          ]
}

innerIds is an array of mongoose.Types.ObjectId

return MySchema.updateOne(
    {
      _id: documentId,
    },
    { $pull: { outerArray: { innerArray: { _id: { $in: innerIds } } } } },
  )
    .session(session)
    .exec()

Author:beginnersLuck.css,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/72292301/pull-element-from-an-array-of-arrays-mongodb
yy