开发者

mongodb $pull matching with regexp not working

开发者 https://www.devze.com 2023-03-12 07:24 出处:网络
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.

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", "engineap开发者_开发问答p.com", "asp.ca" ] }


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.


You can only $pull constant values from an array but not values matching any criteria.

0

精彩评论

暂无评论...
验证码 换一张
取 消