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?
Here's the sample answer using $exists:
db.collection.find( { property : { $exists: true } } );
Just invert the query and search for documents where the property is not null ($ne not equals)
db.collection.find( { property : { $ne : null } } );
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With