Database Reference
In-Depth Information
{
"Track" : "1",
"Title" : "Smells Like Teen Spirit",
"Lenght" : "5:02"
},
{
"Track" : "2",
"Title" : "In Bloom",
"Length" : "4:15"
}
] }
This query gave the desired result and only returned the first document.
$not (meta-operator)
You can use the $not meta-operator to negate any check performed by a standard operator. The following example
returns all documents in your collection, except for the one seen in the $elemMatch example:
> db.media.find ( { Tracklist : { $not : { "$elemMatch" : { Title:
"Smells Like Teen Spirit", "Track" : "1" } } } } )
Specifying Additional Query Expressions
Apart from the structured query syntax you've seen so far, you can also specify additional query expressions in
JavaScript. The big advantage of this is that JavaScript is extremely flexible and allows you to do tons of additional
things. The downside of using JavaScript is that it's a tad slower than the native operators baked into MongoDB.
For example, assume you want to search for a DVD within your collection that is older than 1995. All of the
following code examples would return this information:
db.media.find ( { "Type" : "DVD", "Released" : { $lt : 1995 } } )
db.media.find ( { "Type" : "DVD", $where: "this.Released < 1995" } )
db.media.find ("this.Released < 1995")
f = function() { return this.Released < 1995 }
db.media.find(f)
And that's how flexible MongoDB is! Using these operators should enable you to find just about anything
throughout your collections.
Leveraging Regular Expressions
Regular expressions are another powerful tool you can use to query information. Regular expressions regex , for
short—are special text strings that you can use to describe your search pattern. These work much like wildcards, but
they are far more powerful and flexible.
MongoDB allows you to use these regular expressions when searching for data in your collections; however,
it will attempt to use an index whenever possible for simple prefix queries.
 
Search WWH ::




Custom Search