Mongodb $ pull regex matching not working

I am trying to perform an update on a list, wanting to match with regexp. The fact that the / app / matches criteria for .find, but not for .update, suggests that I am doing something wrong. Is there any other way to get similar results?

> db.things.find({items:/app/})
{ "_id" : 3, "items" : [ "appstore.com", "engineapp.com", "asp.ca" ] }
> db.things.update({}, { $pull: { items: /app/} })
> db.things.find({items:/app/})
{ "_id" : 3, "items" : [ "appstore.com", "engineapp.com", "asp.ca" ] }
+3
source share
2 answers

Here is the code you are looking for:

db.things.update({}, {$pull : { items: {$regex: 'app' } } })

Based on information pulled from the $pull documentation .

I added a test script here .

+9
source

You can only $ pull constant values ​​from an array, but not values ​​that match any criteria.

0
source

All Articles