Skip to content

Instantly share code, notes, and snippets.

@gatesvp
Created June 12, 2011 01:53
Show Gist options
  • Select an option

  • Save gatesvp/1021164 to your computer and use it in GitHub Desktop.

Select an option

Save gatesvp/1021164 to your computer and use it in GitHub Desktop.
Pull with regex
MongoDB shell version: 1.8.1
connecting to: test
> db.test.insert({ _id:3, "items": [ 'appstore.com', 'engineapp.com', 'asp.ca' ] })
> db.test.find()
{ "_id" : 3, "items" : [ "appstore.com", "engineapp.com", "asp.ca" ] }
> db.test.update({}, {$pull : { items: {$regex: 'app' } } })
> db.test.find()
{ "_id" : 3, "items" : [ "asp.ca" ] }
MongoDB shell version: 1.8.1
connecting to: test
> db.test.insert({ _id:3, "items": [ 'appstore.com', 'engineapp.com', 'asp.ca', /app/ ] })
> db.test.find()
{ "_id" : 3, "items" : [ "appstore.com", "engineapp.com", "asp.ca", /app/ ] }
> db.test.update({}, {$pull : { items: /app/ } })
> db.test.find()
{ "_id" : 3, "items" : [ "appstore.com", "engineapp.com", "asp.ca" ] }
@zuriby
Copy link

zuriby commented Jun 12, 2011

version()
version: 1.8.1
db.test.find()
{ "_id" : 974978785, "aliases" : [
"http://even-home.appspot.com/",
"http://twitter.com/zjf1119",
"http://twitter.com/account/redirect_by_id?id=101147112"
], "confidence" : 50, "source" : 1 }
{ "_id" : 83911156, "aliases" : [
"http://twitter.com/account/redirect_by_id?id=53921636",
"http://xreamblog.appspot.com/",
"http://twitter.com/xream"
], "confidence" : 50, "source" : 1 }
{ "_id" : 942476775, "aliases" : [
"http://twitter.com/account/redirect_by_id?id=19028049",
"http://samdeha.appspot.com/",
"http://www.quora.com/zhao-zhou",
"http://twitter.com/xueyufan"
], "confidence" : 50, "source" : 1 }
db.test.update({}, {$pull : { aliases: {$regex: '.*appspot.com' } } })
db.test.find()
{ "_id" : 974978785, "aliases" : [
"http://twitter.com/zjf1119",
"http://twitter.com/account/redirect_by_id?id=101147112"
], "confidence" : 50, "source" : 1 }
{ "_id" : 83911156, "aliases" : [
"http://twitter.com/account/redirect_by_id?id=53921636",
"http://xreamblog.appspot.com/",
"http://twitter.com/xream"
], "confidence" : 50, "source" : 1 }
{ "_id" : 942476775, "aliases" : [
"http://twitter.com/account/redirect_by_id?id=19028049",
"http://samdeha.appspot.com/",
"http://www.quora.com/zhao-zhou",
"http://twitter.com/xueyufan"
], "confidence" : 50, "source" : 1 }

@gatesvp
Copy link
Author

gatesvp commented Jun 12, 2011

So it worked.

The following entry was updated: "_id": 974978785
Notice how "http://even-home.appspot.com/" is now gone?

Of course, it did not update all documents because you did not set the "multi" flag to true.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment