javaee论坛

普通会员

225648

帖子

65

回复

79

积分

楼主
发表于 2017-06-19 05:08:44 | 查看: 44 | 回复: 1
2017-06-16T13:50:49.552+0800 I CONTROL  [main] Hotfix KB2731284 or later update is not installed, will zero-out data files
MongoDB shell version: 3.2.4
connecting to: test
>


> db.inventory.find( { "instock": { warehouse: "A", qty: 5 } } )
{ "_id" : ObjectId("5943714f0429b8c7852eaf20"), "item" : "journal", "instock" : [ { "warehouse" : "A", "qty" : 5 }, { "warehouse" : "C", "qty" : 15 } ] }
> db.inventory.find( { } )
{ "_id" : ObjectId("5943714f0429b8c7852eaf20"), "item" : "journal", "instock" : [ { "warehouse" : "A", "qty" : 5 }, { "warehouse" : "C", "qty" : 15 } ] }
{ "_id" : ObjectId("5943714f0429b8c7852eaf21"), "item" : "notebook", "instock" : [ { "warehouse" : "C", "qty" : 5 } ] }
{ "_id" : ObjectId("5943714f0429b8c7852eaf22"), "item" : "paper", "instock" : [ { "warehouse" : "A", "qty" : 60 }, { "warehouse" : "B", "qty" : 15 } ] }
{ "_id" : ObjectId("5943714f0429b8c7852eaf23"), "item" : "planner", "instock" : [ { "warehouse" : "A", "qty" : 40 }, { "warehouse" : "B", "qty" : 5 } ] }
{ "_id" : ObjectId("5943714f0429b8c7852eaf24"), "item" : "postcard", "instock" : [ { "warehouse" : "B", "qty" : 15 }, { "warehouse" : "C", "qty" : 35 } ] }
> db.inventory.find( { "instock": { qty: 5 } } )
> db.inventory.find( { "instock.qty": 5 } )
{ "_id" : ObjectId("5943714f0429b8c7852eaf20"), "item" : "journal", "instock" : [ { "warehouse" : "A", "qty" : 5 }, { "warehouse" : "C", "qty" : 15 } ] }
{ "_id" : ObjectId("5943714f0429b8c7852eaf21"), "item" : "notebook", "instock" : [ { "warehouse" : "C", "qty" : 5 } ] }
{ "_id" : ObjectId("5943714f0429b8c7852eaf23"), "item" : "planner", "instock" : [ { "warehouse" : "A", "qty" : 40 }, { "warehouse" : "B", "qty" : 5 } ] }
> db.inventory.find( { "instock.qty": 15} )
{ "_id" : ObjectId("5943714f0429b8c7852eaf20"), "item" : "journal", "instock" : [ { "warehouse" : "A", "qty" : 5 }, { "warehouse" : "C", "qty" : 15 } ] }
{ "_id" : ObjectId("5943714f0429b8c7852eaf22"), "item" : "paper", "instock" : [ { "warehouse" : "A", "qty" : 60 }, { "warehouse" : "B", "qty" : 15 } ] }
{ "_id" : ObjectId("5943714f0429b8c7852eaf24"), "item" : "postcard", "instock" : [ { "warehouse" : "B", "qty" : 15 }, { "warehouse" : "C", "qty" : 35 } ] }
> db.inventory.find( { "instock.qty": 15,'instock.warehouse':'B'} )
{ "_id" : ObjectId("5943714f0429b8c7852eaf22"), "item" : "paper", "instock" : [ { "warehouse" : "A", "qty" : 60 }, { "warehouse" : "B", "qty" : 15 } ] }
{ "_id" : ObjectId("5943714f0429b8c7852eaf24"), "item" : "postcard", "instock" : [ { "warehouse" : "B", "qty" : 15 }, { "warehouse" : "C", "qty" : 35 } ] }
> db.inventory.find( { "instock": { qty: 15, warehouse: "B" } } )
> db.inventory.find( { "instock": { warehouse: "B", qty:15 } } )
{ "_id" : ObjectId("5943714f0429b8c7852eaf22"), "item" : "paper", "instock" : [ { "warehouse" : "A", "qty" : 60 }, { "warehouse" : "B", "qty" : 15 } ] }
{ "_id" : ObjectId("5943714f0429b8c7852eaf24"), "item" : "postcard", "instock" : [ { "warehouse" : "B", "qty" : 15 }, { "warehouse" : "C", "qty" : 35 } ] }
> db.inventory.find( { "instock": {$match:{ warehouse: "B", qty:15 }} } )
Error: error: {
        "waitedMS" : NumberLong(0),
        "ok" : 0,
        "errmsg" : "unknown operator: $match",
        "code" : 2
}
> db.inventory.find( { "instock": {$eleMatch:{ warehouse: "B", qty:15 }} } )
Error: error: {
        "waitedMS" : NumberLong(0),
        "ok" : 0,
        "errmsg" : "unknown operator: $eleMatch",
        "code" : 2
}
> db.inventory.find( { "instock": {$elemMatch:{ warehouse: "B", qty:15 }} } )
{ "_id" : ObjectId("5943714f0429b8c7852eaf22"), "item" : "paper", "instock" : [ { "warehouse" : "A", "qty" : 60 }, { "warehouse" : "B", "qty" : 15 } ] }
{ "_id" : ObjectId("5943714f0429b8c7852eaf24"), "item" : "postcard", "instock" : [ { "warehouse" : "B", "qty" : 15 }, { "warehouse" : "C", "qty" : 35 } ] }
> db.inventory.find( { "instock": {$elemMatch:{ warehouse: "B" }} } )
{ "_id" : ObjectId("5943714f0429b8c7852eaf22"), "item" : "paper", "instock" : [ { "warehouse" : "A", "qty" : 60 }, { "warehouse" : "B", "qty" : 15 } ] }
{ "_id" : ObjectId("5943714f0429b8c7852eaf23"), "item" : "planner", "instock" : [ { "warehouse" : "A", "qty" : 40 }, { "warehouse" : "B", "qty" : 5 } ] }
{ "_id" : ObjectId("5943714f0429b8c7852eaf24"), "item" : "postcard", "instock" : [ { "warehouse" : "B", "qty" : 15 }, { "warehouse" : "C", "qty" : 35 } ] }
> db.inventory.find( { "instock.qty": { $gt: 10,  $lte: 20 } } )
{ "_id" : ObjectId("5943714f0429b8c7852eaf20"), "item" : "journal", "instock" : [ { "warehouse" : "A", "qty" : 5 }, { "warehouse" : "C", "qty" : 15 } ] }
{ "_id" : ObjectId("5943714f0429b8c7852eaf22"), "item" : "paper", "instock" : [ { "warehouse" : "A", "qty" : 60 }, { "warehouse" : "B", "qty" : 15 } ] }
{ "_id" : ObjectId("5943714f0429b8c7852eaf23"), "item" : "planner", "instock" : [ { "warehouse" : "A", "qty" : 40 }, { "warehouse" : "B", "qty" : 5 } ] }
{ "_id" : ObjectId("5943714f0429b8c7852eaf24"), "item" : "postcard", "instock" : [ { "warehouse" : "B", "qty" : 15 }, { "warehouse" : "C", "qty" : 35 } ] }
> db.inventory.find( { "instock": {$elemMatch:{ "qty": { $gt: 10,  $lte: 20 } }} } )
{ "_id" : ObjectId("5943714f0429b8c7852eaf20"), "item" : "journal", "instock" : [ { "warehouse" : "A", "qty" : 5 }, { "warehouse" : "C", "qty" : 15 } ] }
{ "_id" : ObjectId("5943714f0429b8c7852eaf22"), "item" : "paper", "instock" : [ { "warehouse" : "A", "qty" : 60 }, { "warehouse" : "B", "qty" : 15 } ] }
{ "_id" : ObjectId("5943714f0429b8c7852eaf24"), "item" : "postcard", "instock" : [ { "warehouse" : "B", "qty" : 15 }, { "warehouse" : "C", "qty" : 35 } ] }
>



参考下面的

Query an Array of Embedded Documents

This page provides examples of query operations on an array of nested documents using thedb.collection.find() method in the mongo shell. The examples on this page use the inventorycollection. To populate the inventory collection, run the following:

< style="font-family:'Source Code Pro',monospace; font-size:14px; word-break:break-all; word-wrap:normal; border-left-width:5px; border-style:none none none solid; border-left-color:rgb(73,71,71); overflow:auto; padding:24px 12px; color:rgb(34,34,34); margin-top:24px; margin-bottom:24px">db.inventory.insertMany( [ { item: "journal", instock: [ { warehouse: "A", qty: 5 }, { warehouse: "C", qty: 15 } ] }, { item: "notebook", instock: [ { warehouse: "C", qty: 5 } ] }, { item: "paper", instock: [ { warehouse: "A", qty: 60 }, { warehouse: "B", qty: 15 } ] }, { item: "planner", instock: [ { warehouse: "A", qty: 40 }, { warehouse: "B", qty: 5 } ] }, { item: "postcard", instock: [ { warehouse: "B", qty: 15 }, { warehouse: "C", qty: 35 } ] }]);

You can run the operation in the web shell below:

Query for a Document Nested in an Array

The following examples selects all documents where an element in the instock array matches the specified document:

< style="font-family:'Source Code Pro',monospace; font-size:14px; word-break:break-all; word-wrap:normal; border-left-width:5px; border-style:none none none solid; border-left-color:rgb(73,71,71); overflow:auto; padding:24px 12px; color:rgb(34,34,34); margin-top:24px; margin-bottom:24px">db.inventory.find( { "instock": { warehouse: "A", qty: 5 } } )

Equality matches on the whole embedded/nested document require an exact match of the specified document, including the field order. For example, the following query does not match any documents in the inventorycollection:

< style="font-family:'Source Code Pro',monospace; font-size:14px; word-break:break-all; word-wrap:normal; border-left-width:5px; border-style:none none none solid; border-left-color:rgb(73,71,71); overflow:auto; padding:24px 12px; color:rgb(34,34,34); margin-top:24px; margin-bottom:24px">db.inventory.find( { "instock": { qty: 5, warehouse: "A" } } )

Specify a Query Condition on a Field in an Array of Documents

Use the Array Index to Query for a Field in the Embedded Document

Using the dot notation, you can specify query conditions for field in a document at a particular index or position of the array. The array uses zero-based indexing.

The following example selects all documents where the instock array has as its first element a document that contains the field qty whose value is less than or equal to 20:

< style="font-family:'Source Code Pro',monospace; font-size:14px; word-break:break-all; word-wrap:normal; border-left-width:5px; border-style:none none none solid; border-left-color:rgb(73,71,71); overflow:auto; padding:24px 12px; color:rgb(34,34,34); margin-top:24px; margin-bottom:24px">db.inventory.find( { 'instock.0.qty': { $lte: 20 } } )

Specify a Query Condition on a Field Embedded in an Array of Documents

If you do not know the index position of the document nested in the array, concatenate the name of the array field, with a dot (.) and the name of the field in the nested document.

The following example selects all documents where the instock array has at least one embedded document that contains the field qty whose value is less than or equal to 20:

< style="font-family:'Source Code Pro',monospace; font-size:14px; word-break:break-all; word-wrap:normal; border-left-width:5px; border-style:none none none solid; border-left-color:rgb(73,71,71); overflow:auto; padding:24px 12px; color:rgb(34,34,34); margin-top:24px; margin-bottom:24px">db.inventory.find( { 'instock.qty': { $lte: 20 } } )

Specify Multiple Conditions for Array of Documents

When specifying conditions on more than one field nested in an array of documents, you can specify the query such that either a single document meets these condition or any combination of documents (including a single document) in the array meets the conditions.

A Single Nested Document Meets Multiple Query Conditions on Nested Fields

Use $elemMatch operator to specify multiple criteria on an array of embedded documents such that at least one embedded document satisfies all the specified criteria.

The following example queries for documents where the instock array has at least one embedded document that contains both the field qty equal to 5 and the field warehouse equal to A:

< style="font-family:'Source Code Pro',monospace; font-size:14px; word-break:break-all; word-wrap:normal; border-left-width:5px; border-style:none none none solid; border-left-color:rgb(73,71,71); overflow:auto; padding:24px 12px; color:rgb(34,34,34); margin-top:24px; margin-bottom:24px">db.inventory.find( { "instock": { $elemMatch: { qty: 5, warehouse: "A" } } } )

The following example queries for documents where the instock array has at least one embedded document that contains the field qty that is greater than 10 and less than or equal to 20:

< style="font-family:'Source Code Pro',monospace; font-size:14px; word-break:break-all; word-wrap:normal; border-left-width:5px; border-style:none none none solid; border-left-color:rgb(73,71,71); overflow:auto; padding:24px 12px; color:rgb(34,34,34); margin-top:24px; margin-bottom:24px">db.inventory.find( { "instock": { $elemMatch: { qty: { $gt: 10, $lte: 20 } } } } )

Combination of Elements Satisfies the Criteria

If the compound query conditions on an array field do not use the $elemMatch operator, the query selects those documents whose array contains any combination of elements that satisfies the conditions.

For example, the following query matches documents where any document nested in the instock array has the qty field greater than 10 and any document (but not necessarily the same embedded document) in the array has the qty field less than or equal to 20:

< style="font-family:'Source Code Pro',monospace; font-size:14px; word-break:break-all; word-wrap:normal; border-left-width:5px; border-style:none none none solid; border-left-color:rgb(73,71,71); overflow:auto; padding:24px 12px; color:rgb(34,34,34); margin-top:24px; margin-bottom:24px">db.inventory.find( { "instock.qty": { $gt: 10, $lte: 20 } } )

The following example queries for documents where the instock array has at least one embedded document that contains the field qty equal to 5 and at least one embedded document (but not necessarily the same embedded document) that contains the field warehouse equal to A:

< style="font-family:'Source Code Pro',monospace; font-size:14px; word-break:break-all; word-wrap:normal; border-left-width:5px; border-style:none none none solid; border-left-color:rgb(73,71,71); overflow:auto; padding:24px 12px; color:rgb(34,34,34); margin-top:24px; margin-bottom:24px">db.inventory.find( { "instock.qty": 5, "instock.warehouse": "A" } )


普通会员

0

帖子

53

回复

61

积分
沙发
发表于 2019-11-19 09:25:16

楼主听话,快到碗里来!

您需要登录后才可以回帖 登录 | 立即注册

触屏版| 电脑版

技术支持 历史网 V2.0 © 2016-2017