Instructions for updating mongoDB using regex

I have the following in mongoDB:

{ "_id" : ObjectId("552f4bf5344fbaee0f62ccef"), "name" : "testcase2", "steps" : [ { "spec1" : "postPet" }, { "spec2" : "putPet" }, { "spec2" : "getPetsHistory" } ] } 

I want to pull an element from an array of steps. I can use only the following values: "testcase2", "postPet".

I used regex in the update request, as described in the following link: https://gist.github.com/gatesvp/1021164

My request:

 db.testcaseCollection.update({"name" : "testcase3"}, {$pull : {"steps" : {$regex: 'postPet'}}}) 

and the result:

 WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 0 }) 

Can someone help with what I'm doing wrong?

0
source share
1 answer

You are trying to pull a string from an array based on a regular expression. The problem is that your array consists of maps / objects / whatever, so it is not surprising that you cannot pull anything out. This is why you see 1 matched and 0 modifies .

 db.testcaseCollection.update({ "name" : "testcase2" }, { $pull : { steps: { spec1: {$regex: 'postPet' }} } }) 

Take a look at the documentation on how you can use $ pull

0
source

Source: https://habr.com/ru/post/1272319/


All Articles