0

this is the structure in mongodb

{   code:'ACBD5588',
    email:'owner@mail.com',
    object{
        array_doc:[//many documents with this structure
        {
            code_doc:'ANCB7894',
            info_doc:'....',
            //etc
        }
        ],
        counting:45,
    }
}

im trying to delete a document inside an array, this document haas a unique code_array so in the array there are many documents with a unique code_array, i want to erase the document inside the array

db.collection('database').findAndModify(
        {
            'code':code,
            'email':email,
        },[],{$pull:{'object.array_doc':{'code_doc':code_doc}}},function(err,resultado)
        {

but the operation gives me this error

uncaught exception: findAndModifyFailed failed: { "errmsg" :
 "need remove or update", "ok" : 0 }
andrescabana86
  • 1,778
  • 8
  • 30
  • 56

1 Answers1

0

Which version of MongoDB are you using? In the latest version MongoDB expects "update" or "remove" to be mentioned explicitly in the findAndModify command. Please use the following query instead.

db.collectionName.findAndModify({
  query : {
    'code':code,
    'email':email
  },
  update: {
     $pull : {'object.array_doc':{'code_doc':code_doc} }
  }
})
Tridib
  • 49
  • 1
  • 1