0

i have a document as

{ "_id" : ObjectId("5de2a0a2336a4c5adc3fc25c"), "image" : { "otherImages" : [ { "imageUrl" : "HL2370207N_1575195338958.jpg", "status" : true }, { "imageUrl" : "HL2370207N_1575195373805.jpg", "status" : true }, { "imageUrl" : "HL2370207N_1575195382992.jpg", "status" : true } ] } }

I have to delete an item of an otherImages array whose imageUrl is "HL2370207N_1575195382992.jpg" $pull cannot applied since it is under node image which is not an array. please give any guidance . i am new to the mongoDB

  • 1
    Hum, why do you think *`$pull` cannot be applied*? Could you show your query? – styvane Dec 01 '19 at 10:53
  • db.userdetails.update( { }, { $pull: { image: {OtherImages: { $elemMatch: { imageUrl: "HL2370207N_1575195382992.jpg" } } } }} ) and the error is WriteResult({ "nMatched" : 0, "nUpserted" : 0, "nModified" : 0, "writeError" : { "code" : 2, "errmsg" : "Cannot apply $pull to a non-array value" } }) – Bharat Bhurtel Dec 01 '19 at 11:00

1 Answers1

0

I think you can specify the value you want to delete. Like so:

{ $pull: { <field1>: <value|condition> } }

Source & examples: https://docs.mongodb.com/manual/reference/operator/update/pull/