Arrays
When a document attribute is an array MongoDB searches through the array values when you use the find() function. This can be odd if you are not aware of such behavior. Even though this feature is important to note that there is not support for recursive searches on array values as such.
If the attribute value is an array the below query will perform the matching against the array elements as well.
> db.grades.find( { "test" : 2, "test var" : "london" } ) { "_id" : ObjectId("52b78121e7a1580719615d4f"), "test" : 2, "test var" : [ "munich", "london" ] } { "_id" : ObjectId("52b78125e7a1580719615d50"), "test" : 2, "test var" : "london" }
For more advance searches you can the $all and $or with arrays.
The $all operator means that your attribute array needs to have all the specified values.
> db.grades.find( { "test" : 2, "test var" : { $all : [ "london" , "munich"] } } ) { "_id" : ObjectId("52b78121e7a1580719615d4f"), "test" : 2, "test var" : [ "munich", "london" ] }
Where the $in operator means that your document array attribute needs to have at least one of the specified strings.
> db.grades.find( { "test" : 2, "test var" : { $in : [ "london" , "munich"] } } ) { "_id" : ObjectId("52b78121e7a1580719615d4f"), "test" : 2, "test var" : [ "munich", "london" ] } { "_id" : ObjectId("52b78125e7a1580719615d50"), "test" : 2, "test var" : "london" }
No comments:
Post a Comment