tags:

views:

12

answers:

1

How do I do a search in MongoDB that searches for any documents with a given property?

What I want to do is find all the documents that have the property regardless of it's value and I don't seem to be able to do this. I've tried the following

db.collection.find({"property", null}); //Finds things that don't have that property
db.collection.find({"proprety", {}}); //Doesn't find anything unless something has the empty object as the value

Is there actually a syntax for this or would I need to do a mapreduce operation?

+2  A: 

Just invert the query and search for documents where the property is not null ($ne not equals)

db.collection.find( { property : { $ne : null } } );
halfdan
Having searched some more it looks like $exists also does the same job
RobV
You are correct. $exists really is the way to go!
halfdan