PruitIgoe
PruitIgoe

Reputation: 6384

Mongo and regexp

I'm just starting out with mongo and python and am stuck on getting this query to run properly:

#get a database
db = connection.test

#get a colleciton
family = db.family

#get all names that begin with E
namesWithE = family.find({'name' : 'E/^'})
print nameWithE.count()

I know there are two records that will match this query but I keep getting a count of 0

Upvotes: 0

Views: 979

Answers (2)

Vicky
Vicky

Reputation: 889

You can also do this directly by

    namesWithE = family.find({'name' : /^E.*/})

Mongodb takes the argument as regular expression if written between / and you can also add options like case insensitivity after the expression like this

   namesWithE = family.find({'name' : /^E.*/i})

Now this expression will return all the documents from the collection that have name field and their value start with either E or e

Upvotes: 0

asenovm
asenovm

Reputation: 6517

namesWithE = family.find({name : {$regex : 'E.*'}}) 

--> this works for me

Upvotes: 2

Related Questions