Database Reference
In-Depth Information
Unfortunately, the preceding query will return both documents. The reason for this
is that both documents have a track with the title called “Smells Like Teen Spirit” and
both have a track number 1. If you want to match an entire document within the array,
you can use $elemMatch , as in this example:
> db.media.find ( { Tracklist: { "$elemMatch" : { Title:
"Smells like teen spirit", Track : "1" } } } )
{ "_id" : ObjectId("4c1a86bb2955000000004076"), "Type" : "CD", "Artist" :
"Nirvana", "Title" : "Nevermind", "Tracklist" : [
{
"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.
 
Search WWH ::




Custom Search