发布于 2015-09-14 14:52:42 | 73 次阅读 | 评论: 0 | 来源: 网络整理

$all

Syntax: { field: { $all: [ <value> , <value1> ... ] }

$all selects the documents where the field holds an array and contains all elements (e.g. <value>, <value1>, etc.) in the array.

Consider the following example:

db.inventory.find( { tags: { $all: [ "appliances", "school", "book" ] } } )

This query selects all documents in the inventory collection where the tags field contains an array with the elements, appliances, school, and book.

Therefore, the above query will match documents in the inventory collection that have a tags field that hold either of the following arrays:

[ "school", "book", "bag", "headphone", "appliances" ]
[ "appliances", "school", "book" ]

The $all operator exists to describe and specify arrays in MongoDB queries. However, you may use the $all operator to select against a non-array field, as in the following example:

db.inventory.find( { qty: { $all: [ 50 ] } } )

However, use the following form to express the same query:

db.inventory.find( { qty: 50 } )

Both queries will select all documents in the inventory collection where the value of the qty field equals 50.

注解

In most cases, MongoDB does not treat arrays as sets. This operator provides a notable exception to this approach.

In the current release queries that use the $all operator must scan all the documents that match the first element in the query array. As a result, even with an index to support the query, the operation may be long running, particularly when the first element in the array is not very selective.

也可以参考

find(), update(), and $set.

最新网友评论  共有(0)条评论 发布评论 返回顶部

Copyright © 2007-2017 PHPERZ.COM All Rights Reserved   冀ICP备14009818号  版权声明  广告服务