Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mongodb $pull matching with regexp not working

Tags:

mongodb

I am trying to perform an update on a list, willing to get a match with regexp. The fact that the criteria /app/ matches for .find but not for .update suggest 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" ] }
like image 245
Tzury Bar Yochay Avatar asked Dec 22 '22 12:12

Tzury Bar Yochay


1 Answers

Here's the code you're looking for:

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

Based on info pulled from the $pull documentation.

I've added a test script here.

like image 193
Gates VP Avatar answered Jan 16 '23 15:01

Gates VP