$all

$all

The $all operator selects the documents where the value of a field is an array that contains all the specified elements. To specify an $all expression, use the following prototype:

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

Examples

The following examples use the inventory collection that contains the documents:

{
   _id: ObjectId("5234cc89687ea597eabee675"),
   code: "xyz",
   tags: [ "school", "book", "bag", "headphone", "appliance" ],
   qty: [
          { size: "S", num: 10, color: "blue" },
          { size: "M", num: 45, color: "blue" },
          { size: "L", num: 100, color: "green" }
        ]
}

{
   _id: ObjectId("5234cc8a687ea597eabee676"),
   code: "abc",
   tags: [ "appliance", "school", "book" ],
   qty: [
          { size: "6", num: 100, color: "green" },
          { size: "6", num: 50, color: "blue" },
          { size: "8", num: 100, color: "brown" }
        ]
}

{
   _id: ObjectId("5234ccb7687ea597eabee677"),
   code: "efg",
   tags: [ "school", "book" ],
   qty: [
          { size: "S", num: 10, color: "blue" },
          { size: "M", num: 100, color: "blue" },
          { size: "L", num: 100, color: "green" }
        ]
}

{
   _id: ObjectId("52350353b2eff1353b349de9"),
   code: "ijk",
   tags: [ "electronics", "school" ],
   qty: [
          { size: "M", num: 100, color: "green" }
        ]
}

Use $all to Match Values

The following operation uses the $all operator to query the inventory collection for documents where the value of the tags field is an array whose elements include applianceschool, and book:

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

The above query returns the following documents:

{
   _id: ObjectId("5234cc89687ea597eabee675"),
   code: "xyz",
   tags: [ "school", "book", "bag", "headphone", "appliance" ],
   qty: [
          { size: "S", num: 10, color: "blue" },
          { size: "M", num: 45, color: "blue" },
          { size: "L", num: 100, color: "green" }
        ]
}

{
   _id: ObjectId("5234cc8a687ea597eabee676"),
   code: "abc",
   tags: [ "appliance", "school", "book" ],
   qty: [
          { size: "6", num: 100, color: "green" },
          { size: "6", num: 50, color: "blue" },
          { size: "8", num: 100, color: "brown" }
        ]
}

Use $all with $elemMatch

If the field contains an array of documents, you can use the $all with the $elemMatch operator.

The following operation queries the inventory collection for documents where the value of the qty field is an array whose elements match the $elemMatch criteria:

db.inventory.find( {
                     qty: { $all: [
                                    { "$elemMatch" : { size: "M", num: { $gt: 50} } },
                                    { "$elemMatch" : { num : 100, color: "green" } }
                                  ] }
                   } )

The query returns the following documents:

{
   "_id" : ObjectId("5234ccb7687ea597eabee677"),
   "code" : "efg",
   "tags" : [ "school", "book"],
   "qty" : [
             { "size" : "S", "num" : 10, "color" : "blue" },
             { "size" : "M", "num" : 100, "color" : "blue" },
             { "size" : "L", "num" : 100, "color" : "green" }
           ]
}

{
   "_id" : ObjectId("52350353b2eff1353b349de9"),
   "code" : "ijk",
   "tags" : [ "electronics", "school" ],
   "qty" : [
             { "size" : "M", "num" : 100, "color" : "green" }
           ]
}

The $all operator exists to support queries on arrays. But 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 equals50.

NOTE

  • 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.

SEE ALSO

find()update(), and $set.


출처 - http://docs.mongodb.org/manual/reference/operator/query/all/#use-all-to-match-values



'DB > MongoDB' 카테고리의 다른 글

mongodb - $in, $all  (0) 2014.04.16
mongodb - array(list) update  (0) 2014.04.05
mongodb - $elemMatch, list/array에서 검색  (0) 2014.03.21
mongodb - User Privilege Roles  (0) 2014.02.23
mongodb - backup  (0) 2014.02.23
Posted by linuxism
,