Uploaded image for project: 'Core Server'
  1. Core Server
  2. SERVER-61618

bulkUpdate $pull operation on array with objects not working

    • Type: Icon: Bug Bug
    • Resolution: Done
    • Priority: Icon: Major - P3 Major - P3
    • None
    • Affects Version/s: 4.4.3
    • Component/s: None
    • ALL
    • Hide

       

      import { MongoClient } from 'mongodb'
      
      const client = new MongoClient('mongodb://127.0.0.1:27017')
      await client.connect()
      const db = client.db('test')
      const insertResult = await db.collection('test').insertOne({ arr: [{ _id: 1, isAdmin: true }, { _id: 2, isAdmin: false }] })
      const doc = await db.collection('test').findOne({ _id: insertResult.insertedId })
      console.log('before bulk update', doc)
      const bulkUpdate = db.collection('test').initializeUnorderedBulkOp()
      bulkUpdate.find({ _id: doc.insertedId }).updateOne({ $pull: { arr: { _id: 1 } } })
      if (bulkUpdate.length > 0) {
       console.log('executing bulk update')
       await bulkUpdate.execute()
      }
      // await db.collection('test').updateOne({ _id: doc._id }, { $pull: { arr: { _id: 1 } } })
      const updatedDoc = await db.collection('test').findOne({ _id: doc._id })
      console.log('after bulk update', updatedDoc)
      await db.collection('test').deleteOne({ _id: doc._id })
      

       

      Show
        import { MongoClient } from 'mongodb' const client = new MongoClient( 'mongodb: //127.0.0.1:27017' ) await client.connect() const db = client.db( 'test' ) const insertResult = await db.collection( 'test' ).insertOne({ arr: [{ _id: 1, isAdmin: true }, { _id: 2, isAdmin: false }] }) const doc = await db.collection( 'test' ).findOne({ _id: insertResult.insertedId }) console.log( 'before bulk update' , doc) const bulkUpdate = db.collection( 'test' ).initializeUnorderedBulkOp() bulkUpdate.find({ _id: doc.insertedId }).updateOne({ $pull: { arr: { _id: 1 } } }) if (bulkUpdate.length > 0) { console.log( 'executing bulk update' ) await bulkUpdate.execute() } // await db.collection( 'test' ).updateOne({ _id: doc._id }, { $pull: { arr: { _id: 1 } } }) const updatedDoc = await db.collection( 'test' ).findOne({ _id: doc._id }) console.log( 'after bulk update' , updatedDoc) await db.collection( 'test' ).deleteOne({ _id: doc._id })  

      os: ArchLinux 5.15.2

      nodeJS: 14.17.4
      mongodb: 4.4.3
      nodejs mongodb drvier: 4.2.0

      Usual updateOne working when call $pull on array fields containing objects, but bulk operation have no effect.

            Assignee:
            Unassigned Unassigned
            Reporter:
            ivan.c@taskworld.com Ivan Cherviakov
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: