lanying56123 发表于 2018-10-24 12:57:23

mongodb CRUD操作 -Select

  https://docs.mongodb.com/manual/reference/method/db.collection.find/
  查询文档
  db.collection.find(query, projection)
  方法:
  db.collection.find()
  Additional Methods

[*]  db.collection.findOne
[*]  In aggregation pipeline, the $match pipeline stage provides access to MongoDB queries.
  db.inventory.insertMany([

  { item: "journal", qty: 25,>
  { item: "notebook", qty: 50,>
  { item: "paper", qty: 100,>
  { item: "planner", qty: 75,>
  { item: "postcard", qty: 45,>  ]);
  参考:
  > db.inventory.find( {} ,{_id:0})
{ "item" : "journal", "qty" : 25, "size" : { "h" : 14, "w" : 21, "uom" : "cm" }, "status" : "A" }  
{ "item" : "notebook", "qty" : 50, "size" : { "h" : 8.5, "w" : 11, "uom" : "in" }, "status" : "A" }
  
{ "item" : "paper", "qty" : 100, "size" : { "h" : 8.5, "w" : 11, "uom" : "in" }, "status" : "D" }
  
{ "item" : "planner", "qty" : 75, "size" : { "h" : 22.85, "w" : 30, "uom" : "cm" }, "status" : "D" }
  
{ "item" : "postcard", "qty" : 45, "size" : { "h" : 10, "w" : 15.25, "uom" : "cm" }, "status" : "A" }
  操作必须以键值对的方式,必须用{}括起来,所以所有的查询先用{}括起来
  1、Select All Documents in a Collection
  db.inventory.find( {} )=db.inventory.find()
  2、Specify Equality Condition
  db.inventory.find( { status: "D" } )
  SELECT * FROM inventory WHERE status = "D"
{ "item" : "paper", "qty" : 100, "size" : { "h" : 8.5, "w" : 11, "uom" : "in" }, "status" : "D" }  
{ "item" : "planner", "qty" : 75, "size" : { "h" : 22.85, "w" : 30, "uom" : "cm" }, "status" : "D" }
  3、Specify Conditions Using Query Operators
  db.inventory.find( { status: { $in: [ "A", "D" ] } } )
  SELECT * FROM inventory WHERE status in ("A", "D")
{ "item" : "journal", "qty" : 25, "size" : { "h" : 14, "w" : 21, "uom" : "cm" }, "status" : "A" }  
{ "item" : "notebook", "qty" : 50, "size" : { "h" : 8.5, "w" : 11, "uom" : "in" }, "status" : "A" }
  
{ "item" : "paper", "qty" : 100, "size" : { "h" : 8.5, "w" : 11, "uom" : "in" }, "status" : "D" }
  
{ "item" : "planner", "qty" : 75, "size" : { "h" : 22.85, "w" : 30, "uom" : "cm" }, "status" : "D" }
  
{ "item" : "postcard", "qty" : 45, "size" : { "h" : 10, "w" : 15.25, "uom" : "cm" }, "status" : "A" }
  4、Specify AND Conditions
  db.inventory.find( { status: "A", qty: { $lt: 30 } } )
  SELECT * FROM inventory WHERE status = &quot;A&quot; AND qty < 30
{ &quot;item&quot; : &quot;journal&quot;, &quot;qty&quot; : 25, &quot;size&quot; : { &quot;h&quot; : 14, &quot;w&quot; : 21, &quot;uom&quot; : &quot;cm&quot; }, &quot;status&quot; : &quot;A&quot; }  5、Specify OR Conditions
  db.inventory.find( { $or: [ { status: &quot;A&quot; }, { qty: { $lt: 30 } } ] } )
  SELECT * FROM inventory WHERE status = &quot;A&quot; OR qty < 30
{ &quot;item&quot; : &quot;journal&quot;, &quot;qty&quot; : 25, &quot;size&quot; : { &quot;h&quot; : 14, &quot;w&quot; : 21, &quot;uom&quot; : &quot;cm&quot; }, &quot;status&quot; : &quot;A&quot; }  
{ &quot;item&quot; : &quot;notebook&quot;, &quot;qty&quot; : 50, &quot;size&quot; : { &quot;h&quot; : 8.5, &quot;w&quot; : 11, &quot;uom&quot; : &quot;in&quot; }, &quot;status&quot; : &quot;A&quot; }
  
{ &quot;item&quot; : &quot;postcard&quot;, &quot;qty&quot; : 45, &quot;size&quot; : { &quot;h&quot; : 10, &quot;w&quot; : 15.25, &quot;uom&quot; : &quot;cm&quot; }, &quot;status&quot; : &quot;A&quot; }
  6、Specify AND as well as OR Conditions
  db.inventory.find( {
  status: &quot;A&quot;,
  $or: [ { qty: { $lt: 30 } }, { item: /^p/ } ]
  } )
  SELECT * FROM inventory WHERE status = &quot;A&quot; AND ( qty < 30 OR item LIKE &quot;p%&quot;)
{ &quot;item&quot; : &quot;journal&quot;, &quot;qty&quot; : 25, &quot;size&quot; : { &quot;h&quot; : 14, &quot;w&quot; : 21, &quot;uom&quot; : &quot;cm&quot; }, &quot;status&quot; : &quot;A&quot; }  
{ &quot;item&quot; : &quot;postcard&quot;, &quot;qty&quot; : 45, &quot;size&quot; : { &quot;h&quot; : 10, &quot;w&quot; : 15.25, &quot;uom&quot; : &quot;cm&quot; }, &quot;status&quot; : &quot;A&quot; }
  Additional Query Tutorials
  1、Query on Embedded/Nested Documents 查询嵌入/嵌套文档(字段值是一个文档)
  db.inventory.insertMany( [

  { item: &quot;journal&quot;, qty: 25,>
  { item: &quot;notebook&quot;, qty: 50,>
  { item: &quot;paper&quot;, qty: 100,>
  { item: &quot;planner&quot;, qty: 75,>
  { item: &quot;postcard&quot;, qty: 45,>  ])
  > db.inventory.find( {} ,{_id:0})
{ &quot;item&quot; : &quot;journal&quot;, &quot;qty&quot; : 25, &quot;size&quot; : { &quot;h&quot; : 14, &quot;w&quot; : 21, &quot;uom&quot; : &quot;cm&quot; }, &quot;status&quot; : &quot;A&quot; }  
{ &quot;item&quot; : &quot;notebook&quot;, &quot;qty&quot; : 50, &quot;size&quot; : { &quot;h&quot; : 8.5, &quot;w&quot; : 11, &quot;uom&quot; : &quot;in&quot; }, &quot;status&quot; : &quot;A&quot; }
  
{ &quot;item&quot; : &quot;paper&quot;, &quot;qty&quot; : 100, &quot;size&quot; : { &quot;h&quot; : 8.5, &quot;w&quot; : 11, &quot;uom&quot; : &quot;in&quot; }, &quot;status&quot; : &quot;D&quot; }
  
{ &quot;item&quot; : &quot;planner&quot;, &quot;qty&quot; : 75, &quot;size&quot; : { &quot;h&quot; : 22.85, &quot;w&quot; : 30, &quot;uom&quot; : &quot;cm&quot; }, &quot;status&quot; : &quot;D&quot; }
  
{ &quot;item&quot; : &quot;postcard&quot;, &quot;qty&quot; : 45, &quot;size&quot; : { &quot;h&quot; : 10, &quot;w&quot; : 15.25, &quot;uom&quot; : &quot;cm&quot; }, &quot;status&quot; : &quot;A&quot; }
  Match an Embedded/Nested Document
  查询size字段的值是{h:14,w:21,uom:&quot;cm&quot;}的所有文档:(在作为嵌入/嵌套文档的字段上指定相等条件)
{ &quot;item&quot; : &quot;journal&quot;, &quot;qty&quot; : 25, &quot;size&quot; : { &quot;h&quot; : 14, &quot;w&quot; : 21, &quot;uom&quot; : &quot;cm&quot; }, &quot;status&quot; : &quot;A&quot; }  db.inventory.find({size:{w:21,h:14,uom:&quot;cm&quot;}})
  注:文档的精确匹配,包括顺序。
  db.inventory.find({size:{w:21,h:14,uom:“cm”}})// 与集合中的任何文档不匹配
  Query on Nested Field
  Specify Equality Match on a Nested Field
  查询size字段中嵌套的字段uom的值等于“in”的所有文档:
  db.inventory.find( { &quot;size.uom&quot;: &quot;in&quot; } )
{ &quot;item&quot; : &quot;notebook&quot;, &quot;qty&quot; : 50, &quot;size&quot; : { &quot;h&quot; : 8.5, &quot;w&quot; : 11, &quot;uom&quot; : &quot;in&quot; }, &quot;status&quot; : &quot;A&quot; }  
{ &quot;item&quot; : &quot;paper&quot;, &quot;qty&quot; : 100, &quot;size&quot; : { &quot;h&quot; : 8.5, &quot;w&quot; : 11, &quot;uom&quot; : &quot;in&quot; }, &quot;status&quot; : &quot;D&quot; }
  Specify Match using Query Operator
  查询size字段中嵌套的字段h的值小于15的所有文档:
  db.inventory.find( { &quot;size.h&quot;: { $lt: 15 } } )
{ &quot;item&quot; : &quot;journal&quot;, &quot;qty&quot; : 25, &quot;size&quot; : { &quot;h&quot; : 14, &quot;w&quot; : 21, &quot;uom&quot; : &quot;cm&quot; }, &quot;status&quot; : &quot;A&quot; }  
{ &quot;item&quot; : &quot;notebook&quot;, &quot;qty&quot; : 50, &quot;size&quot; : { &quot;h&quot; : 8.5, &quot;w&quot; : 11, &quot;uom&quot; : &quot;in&quot; }, &quot;status&quot; : &quot;A&quot; }
  
{ &quot;item&quot; : &quot;paper&quot;, &quot;qty&quot; : 100, &quot;size&quot; : { &quot;h&quot; : 8.5, &quot;w&quot; : 11, &quot;uom&quot; : &quot;in&quot; }, &quot;status&quot; : &quot;D&quot; }
  
{ &quot;item&quot; : &quot;postcard&quot;, &quot;qty&quot; : 45, &quot;size&quot; : { &quot;h&quot; : 10, &quot;w&quot; : 15.25, &quot;uom&quot; : &quot;cm&quot; }, &quot;status&quot; : &quot;A&quot; }
  Specify AND Condition
  查询size字段中嵌套的字段h的值小于15,uom的值为“in”,status的值为“D”的所有文档:
  db.inventory.find( { &quot;size.h&quot;: { $lt: 15 }, &quot;size.uom&quot;: &quot;in&quot;, status: &quot;D&quot; } )
{ &quot;item&quot; : &quot;paper&quot;, &quot;qty&quot; : 100, &quot;size&quot; : { &quot;h&quot; : 8.5, &quot;w&quot; : 11, &quot;uom&quot; : &quot;in&quot; }, &quot;status&quot; : &quot;D&quot; }  2、Query an Array 查询数组(字段值是一个数组)
  db.inventory.insertMany([
  { item: &quot;journal&quot;, qty: 25, tags: [&quot;blank&quot;, &quot;red&quot;], dim_cm: [ 14, 21 ] },
  { item: &quot;notebook&quot;, qty: 50, tags: [&quot;red&quot;, &quot;blank&quot;], dim_cm: [ 14, 21 ] },
  { item: &quot;paper&quot;, qty: 100, tags: [&quot;red&quot;, &quot;blank&quot;, &quot;plain&quot;], dim_cm: [ 14, 21 ] },
  { item: &quot;planner&quot;, qty: 75, tags: [&quot;blank&quot;, &quot;red&quot;], dim_cm: [ 22.85, 30 ] },
  { item: &quot;postcard&quot;, qty: 45, tags: [&quot;blue&quot;], dim_cm: [ 10, 15.25 ] }
  ])
  参考:
{ &quot;item&quot; : &quot;journal&quot;, &quot;qty&quot; : 25, &quot;tags&quot; : [ &quot;blank&quot;, &quot;red&quot; ], &quot;dim_cm&quot; : [ 14, 21 ] }  
{ &quot;item&quot; : &quot;notebook&quot;, &quot;qty&quot; : 50, &quot;tags&quot; : [ &quot;red&quot;, &quot;blank&quot; ], &quot;dim_cm&quot; : [ 14, 21 ] }
  
{ &quot;item&quot; : &quot;paper&quot;, &quot;qty&quot; : 100, &quot;tags&quot; : [ &quot;red&quot;, &quot;blank&quot;, &quot;plain&quot; ], &quot;dim_cm&quot; : [ 14, 21 ] }
  
{ &quot;item&quot; : &quot;planner&quot;, &quot;qty&quot; : 75, &quot;tags&quot; : [ &quot;blank&quot;, &quot;red&quot; ], &quot;dim_cm&quot; : [ 22.85, 30 ] }
  
{ &quot;item&quot; : &quot;postcard&quot;, &quot;qty&quot; : 45, &quot;tags&quot; : [ &quot;blue&quot; ], &quot;dim_cm&quot; : [ 10, 15.25 ] }
  Match an Array
  查询数组字段tags中正好包含两个元素“red和blank”,并且顺序相同的所有文档:
  db.inventory.find( { tags: [&quot;red&quot;, &quot;blank&quot;] } )
  注:精确匹配,包括数组中元素的顺序。
{ &quot;item&quot; : &quot;notebook&quot;, &quot;qty&quot; : 50, &quot;tags&quot; : [ &quot;red&quot;, &quot;blank&quot; ], &quot;dim_cm&quot; : [ 14, 21 ] }  查询数组字段tags中包含“red”和“blank”两个元素的所有文档,不考虑数组中的顺序和其他元素:
  db.inventory.find( { tags: { $all: [&quot;red&quot;, &quot;blank&quot;] } } )
  等同于
  db.inventory.find({$and: [ { tags: &quot;red&quot; }, { tags: &quot;blank&quot; }]},{_id:0})
{ &quot;item&quot; : &quot;journal&quot;, &quot;qty&quot; : 25, &quot;tags&quot; : [ &quot;blank&quot;, &quot;red&quot; ], &quot;dim_cm&quot; : [ 14, 21 ] }  
{ &quot;item&quot; : &quot;notebook&quot;, &quot;qty&quot; : 50, &quot;tags&quot; : [ &quot;red&quot;, &quot;blank&quot; ], &quot;dim_cm&quot; : [ 14, 21 ] }
  
{ &quot;item&quot; : &quot;paper&quot;, &quot;qty&quot; : 100, &quot;tags&quot; : [ &quot;red&quot;, &quot;blank&quot;, &quot;plain&quot; ], &quot;dim_cm&quot; : [ 14, 21 ] }
  
{ &quot;item&quot; : &quot;planner&quot;, &quot;qty&quot; : 75, &quot;tags&quot; : [ &quot;blank&quot;, &quot;red&quot; ], &quot;dim_cm&quot; : [ 22.85, 30 ] }
  Query an Array for an Element
  查询数组字段tags中包含red元素的所有文档:
  db.inventory.find( { tags: &quot;red&quot; } )
{ &quot;item&quot; : &quot;journal&quot;, &quot;qty&quot; : 25, &quot;tags&quot; : [ &quot;blank&quot;, &quot;red&quot; ], &quot;dim_cm&quot; : [ 14, 21 ] }  
{ &quot;item&quot; : &quot;notebook&quot;, &quot;qty&quot; : 50, &quot;tags&quot; : [ &quot;red&quot;, &quot;blank&quot; ], &quot;dim_cm&quot; : [ 14, 21 ] }
  
{ &quot;item&quot; : &quot;paper&quot;, &quot;qty&quot; : 100, &quot;tags&quot; : [ &quot;red&quot;, &quot;blank&quot;, &quot;plain&quot; ], &quot;dim_cm&quot; : [ 14, 21 ] }
  
{ &quot;item&quot; : &quot;planner&quot;, &quot;qty&quot; : 75, &quot;tags&quot; : [ &quot;blank&quot;, &quot;red&quot; ], &quot;dim_cm&quot; : [ 22.85, 30 ] }
  查询数组字段dim_cm中至少有一个元素的值大于25的所有文档:
  db.inventory.find( { dim_cm: { $gt: 25 } } )
{ &quot;item&quot; : &quot;planner&quot;, &quot;qty&quot; : 75, &quot;tags&quot; : [ &quot;blank&quot;, &quot;red&quot; ], &quot;dim_cm&quot; : [ 22.85, 30 ] }  Specify Multiple Conditions for Array Elements
  Query an Array with Compound Filter Conditions on the Array Elements
  查询数组字段dim_cn中一个元素的值大于15且另一个元素的值小于20,或者单个元素满足两个条件的所有文档:
  db.inventory.find( { dim_cm: { $gt: 15, $lt: 20 } } )
{ &quot;item&quot; : &quot;journal&quot;, &quot;qty&quot; : 25, &quot;tags&quot; : [ &quot;blank&quot;, &quot;red&quot; ], &quot;dim_cm&quot; : [ 14, 21 ] }  
{ &quot;item&quot; : &quot;notebook&quot;, &quot;qty&quot; : 50, &quot;tags&quot; : [ &quot;red&quot;, &quot;blank&quot; ], &quot;dim_cm&quot; : [ 14, 21 ] }
  
{ &quot;item&quot; : &quot;paper&quot;, &quot;qty&quot; : 100, &quot;tags&quot; : [ &quot;red&quot;, &quot;blank&quot;, &quot;plain&quot; ], &quot;dim_cm&quot; : [ 14, 21 ] }
  
{ &quot;item&quot; : &quot;postcard&quot;, &quot;qty&quot; : 45, &quot;tags&quot; : [ &quot;blue&quot; ], &quot;dim_cm&quot; : [ 10, 15.25 ] }
  Query for an Array Element that Meets Multiple Criteria 查询符合多个标准的数组元素
  查询数组字段dim_cm中至少有一个元素的值大于22且小于30的所有文档:
  db.inventory.find( { dim_cm: { $elemMatch: { $gt: 22, $lt: 30 } } } )
{ &quot;item&quot; : &quot;planner&quot;, &quot;qty&quot; : 75, &quot;tags&quot; : [ &quot;blank&quot;, &quot;red&quot; ], &quot;dim_cm&quot; : [ 22.85, 30 ] }  Query for an Element by the Array Index Position 通过数组索引位置查询元素
  查询数组字段dim_cm中第二个元素的值大于25的所有文档:
  db.inventory.find( { &quot;dim_cm.1&quot;: { $gt: 25 } } )
{ &quot;item&quot; : &quot;planner&quot;, &quot;qty&quot; : 75, &quot;tags&quot; : [ &quot;blank&quot;, &quot;red&quot; ], &quot;dim_cm&quot; : [ 22.85, 30 ] }  Query an Array by Array Length
  查询数组字段tags中具有三个元素的所有文档:
  db.inventory.find( { &quot;tags&quot;: { $size: 3 } } )
{ &quot;item&quot; : &quot;paper&quot;, &quot;qty&quot; : 100, &quot;tags&quot; : [ &quot;red&quot;, &quot;blank&quot;, &quot;plain&quot; ], &quot;dim_cm&quot; : [ 14, 21 ] }  3、Query an Array of Embedded Documents 查询嵌入式文档的数组(字段值包含数组,数组里面嵌套文档)
  db.inventory.insertMany( [
  { item: &quot;journal&quot;, instock: [ { warehouse: &quot;A&quot;, qty: 5 }, { warehouse: &quot;C&quot;, qty: 15 } ] },
  { item: &quot;notebook&quot;, instock: [ { warehouse: &quot;C&quot;, qty: 5 } ] },
  { item: &quot;paper&quot;, instock: [ { warehouse: &quot;A&quot;, qty: 60 }, { warehouse: &quot;B&quot;, qty: 15 } ] },
  { item: &quot;planner&quot;, instock: [ { warehouse: &quot;A&quot;, qty: 40 }, { warehouse: &quot;B&quot;, qty: 5 } ] },
  { item: &quot;postcard&quot;, instock: [ { warehouse: &quot;B&quot;, qty: 15 }, { warehouse: &quot;C&quot;, qty: 35 } ] }
  ])
  参考:
{ &quot;item&quot; : &quot;journal&quot;, &quot;instock&quot; : [ { &quot;warehouse&quot; : &quot;A&quot;, &quot;qty&quot; : 5 }, { &quot;warehouse&quot; : &quot;C&quot;, &quot;qty&quot; : 15 } ] }  
{ &quot;item&quot; : &quot;notebook&quot;, &quot;instock&quot; : [ { &quot;warehouse&quot; : &quot;C&quot;, &quot;qty&quot; : 5 } ] }
  
{ &quot;item&quot; : &quot;paper&quot;, &quot;instock&quot; : [ { &quot;warehouse&quot; : &quot;A&quot;, &quot;qty&quot; : 60 }, { &quot;warehouse&quot; : &quot;B&quot;, &quot;qty&quot; : 15 } ] }
  
{ &quot;item&quot; : &quot;planner&quot;, &quot;instock&quot; : [ { &quot;warehouse&quot; : &quot;A&quot;, &quot;qty&quot; : 40 }, { &quot;warehouse&quot; : &quot;B&quot;, &quot;qty&quot; : 5 } ] }
  
{ &quot;item&quot; : &quot;postcard&quot;, &quot;instock&quot; : [ { &quot;warehouse&quot; : &quot;B&quot;, &quot;qty&quot; : 15 }, { &quot;warehouse&quot; : &quot;C&quot;, &quot;qty&quot; : 35 } ] }
  Query for a Document Nested in an Array 查询嵌套在数组中的文档
  查询数组字段instock中的嵌入式文档与指定文档匹配的所有文档:
  db.inventory.find( { &quot;instock&quot;: { warehouse: &quot;A&quot;, qty: 5 } } )
{ &quot;item&quot; : &quot;journal&quot;, &quot;instock&quot; : [ { &quot;warehouse&quot; : &quot;A&quot;, &quot;qty&quot; : 5 }, { &quot;warehouse&quot; : &quot;C&quot;, &quot;qty&quot; : 15 } ] }  注:精确匹配,包括字段的顺序。
  db.inventory.find( { &quot;instock&quot;: { qty: 5, warehouse: &quot;A&quot; } } ) //与集合中的任何文档不匹配。
  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 使用数组索引来查询嵌入文档中的字段
  查询数组字段instock中第一个嵌入式文档中qty字段的值小于或等于20的所有文档:
  db.inventory.find( { 'instock.0.qty': { $lte: 20 } } )
{ &quot;item&quot; : &quot;journal&quot;, &quot;instock&quot; : [ { &quot;warehouse&quot; : &quot;A&quot;, &quot;qty&quot; : 5 }, { &quot;warehouse&quot; : &quot;C&quot;, &quot;qty&quot; : 15 } ] }  
{ &quot;item&quot; : &quot;notebook&quot;, &quot;instock&quot; : [ { &quot;warehouse&quot; : &quot;C&quot;, &quot;qty&quot; : 5 } ] }
  
{ &quot;item&quot; : &quot;postcard&quot;, &quot;instock&quot; : [ { &quot;warehouse&quot; : &quot;B&quot;, &quot;qty&quot; : 15 }, { &quot;warehouse&quot; : &quot;C&quot;, &quot;qty&quot; : 35 } ] }
  Specify a Query Condition on a Field Embedded in an Array of Documents 在文档数组中嵌入的字段上指定查询条件
  查询数组字段instock中至少有一个嵌入式文档qty字段的值小于或等于20的所有文档:
  db.inventory.find( { 'instock.qty': { $lte: 20 } } )
{ &quot;item&quot; : &quot;journal&quot;, &quot;instock&quot; : [ { &quot;warehouse&quot; : &quot;A&quot;, &quot;qty&quot; : 5 }, { &quot;warehouse&quot; : &quot;C&quot;, &quot;qty&quot; : 15 } ] }  
{ &quot;item&quot; : &quot;notebook&quot;, &quot;instock&quot; : [ { &quot;warehouse&quot; : &quot;C&quot;, &quot;qty&quot; : 5 } ] }
  
{ &quot;item&quot; : &quot;paper&quot;, &quot;instock&quot; : [ { &quot;warehouse&quot; : &quot;A&quot;, &quot;qty&quot; : 60 }, { &quot;warehouse&quot; : &quot;B&quot;, &quot;qty&quot; : 15 } ] }
  
{ &quot;item&quot; : &quot;planner&quot;, &quot;instock&quot; : [ { &quot;warehouse&quot; : &quot;A&quot;, &quot;qty&quot; : 40 }, { &quot;warehouse&quot; : &quot;B&quot;, &quot;qty&quot; : 5 } ] }
  
{ &quot;item&quot; : &quot;postcard&quot;, &quot;instock&quot; : [ { &quot;warehouse&quot; : &quot;B&quot;, &quot;qty&quot; : 15 }, { &quot;warehouse&quot; : &quot;C&quot;, &quot;qty&quot; : 35 } ] }
  Specify Multiple Conditions for Array of Documents
  A Single Nested Document Meets Multiple Query Conditions on Nested Fields 单个嵌套文档在嵌套字段上符合多个查询条件
  查询数组字段instock中至少有一个嵌入式文档qty字段的值等于5并且warehouse字段的值等于A的所有文档:
  db.inventory.find( { &quot;instock&quot;: { $elemMatch: { qty: 5, warehouse: &quot;A&quot; } } } )
{ &quot;item&quot; : &quot;journal&quot;, &quot;instock&quot; : [ { &quot;warehouse&quot; : &quot;A&quot;, &quot;qty&quot; : 5 }, { &quot;warehouse&quot; : &quot;C&quot;, &quot;qty&quot; : 15 } ] }  查询数组字段instock中至少有一个嵌入式文档qty字段的值大于10小于20的所有文档:
  db.inventory.find( { &quot;instock&quot;: { $elemMatch: { qty: { $gt: 10, $lte: 20 } } } } )
{ &quot;item&quot; : &quot;journal&quot;, &quot;instock&quot; : [ { &quot;warehouse&quot; : &quot;A&quot;, &quot;qty&quot; : 5 }, { &quot;warehouse&quot; : &quot;C&quot;, &quot;qty&quot; : 15 } ] }  
{ &quot;item&quot; : &quot;paper&quot;, &quot;instock&quot; : [ { &quot;warehouse&quot; : &quot;A&quot;, &quot;qty&quot; : 60 }, { &quot;warehouse&quot; : &quot;B&quot;, &quot;qty&quot; : 15 } ] }
  
{ &quot;item&quot; : &quot;postcard&quot;, &quot;instock&quot; : [ { &quot;warehouse&quot; : &quot;B&quot;, &quot;qty&quot; : 15 }, { &quot;warehouse&quot; : &quot;C&quot;, &quot;qty&quot; : 35 } ] }
  Combination of Elements Satisfies the Criteria 元素的组合满足标准
  查询数组字段instock中至少有一个嵌入式文档的qty字段的值大于10,并且另一个嵌入式文档的qty字段的值小于或等于20的所有文档,或者同时满足两个条件:
  db.inventory.find( { &quot;instock.qty&quot;: { $gt: 10,$lte: 20 } } )
{ &quot;item&quot; : &quot;journal&quot;, &quot;instock&quot; : [ { &quot;warehouse&quot; : &quot;A&quot;, &quot;qty&quot; : 5 }, { &quot;warehouse&quot; : &quot;C&quot;, &quot;qty&quot; : 15 } ] }  
{ &quot;item&quot; : &quot;paper&quot;, &quot;instock&quot; : [ { &quot;warehouse&quot; : &quot;A&quot;, &quot;qty&quot; : 60 }, { &quot;warehouse&quot; : &quot;B&quot;, &quot;qty&quot; : 15 } ] }
  
{ &quot;item&quot; : &quot;planner&quot;, &quot;instock&quot; : [ { &quot;warehouse&quot; : &quot;A&quot;, &quot;qty&quot; : 40 }, { &quot;warehouse&quot; : &quot;B&quot;, &quot;qty&quot; : 5 } ] }
  
{ &quot;item&quot; : &quot;postcard&quot;, &quot;instock&quot; : [ { &quot;warehouse&quot; : &quot;B&quot;, &quot;qty&quot; : 15 }, { &quot;warehouse&quot; : &quot;C&quot;, &quot;qty&quot; : 35 } ] }
  查询数组字段instock中至少有一个嵌入式文档的qty字段的值等于5,并且另一个嵌入式文档的warehouse字段的值是A的所有文档,或者同时满足两个条件:
  db.inventory.find({&quot;instock.qty&quot;:5,&quot;instock.warehouse&quot;:&quot;A&quot; })
  { &quot;item&quot; : &quot;journal&quot;, &quot;instock&quot; : [ { &quot;warehouse&quot; : &quot;A&quot;, &quot;qty&quot; : 5 }, { &quot;warehouse&quot; : &quot;C&quot;, &quot;qty&quot; : 15 } ] }
  { &quot;item&quot; : &quot;planner&quot;, &quot;instock&quot; : [ { &quot;warehouse&quot; : &quot;A&quot;, &quot;qty&quot; : 40 }, { &quot;warehouse&quot; : &quot;B&quot;, &quot;qty&quot; : 5 } ] }
  4、Project Fields to Return from Query 从查询结果中返回指定的字段
  db.inventory.insertMany( [

  { item: &quot;journal&quot;, status: &quot;A&quot;,>  { item: &quot;notebook&quot;, status: &quot;A&quot;,size: { h: 8.5, w: 11, uom: &quot;in&quot; }, instock: [ { warehouse: &quot;C&quot;, qty: 5 } ] },

  { item: &quot;paper&quot;, status: &quot;D&quot;,>
  { item: &quot;planner&quot;, status: &quot;D&quot;,>
  { item: &quot;postcard&quot;, status: &quot;A&quot;,>  ])
{ &quot;item&quot; : &quot;journal&quot;, &quot;status&quot; : &quot;A&quot;, &quot;size&quot; : { &quot;h&quot; : 14, &quot;w&quot; : 21, &quot;uom&quot; : &quot;cm&quot; }, &quot;instock&quot; : [ { &quot;warehouse&quot; : &quot;A&quot;, &quot;qty&quot; : 5 } ] }  
{ &quot;item&quot; : &quot;notebook&quot;, &quot;status&quot; : &quot;A&quot;, &quot;size&quot; : { &quot;h&quot; : 8.5, &quot;w&quot; : 11, &quot;uom&quot; : &quot;in&quot; }, &quot;instock&quot; : [ { &quot;warehouse&quot; : &quot;C&quot;, &quot;qty&quot; : 5 } ] }
  
{ &quot;item&quot; : &quot;paper&quot;, &quot;status&quot; : &quot;D&quot;, &quot;size&quot; : { &quot;h&quot; : 8.5, &quot;w&quot; : 11, &quot;uom&quot; : &quot;in&quot; }, &quot;instock&quot; : [ { &quot;warehouse&quot; : &quot;A&quot;, &quot;qty&quot; : 60 } ] }
  
{ &quot;item&quot; : &quot;planner&quot;, &quot;status&quot; : &quot;D&quot;, &quot;size&quot; : { &quot;h&quot; : 22.85, &quot;w&quot; : 30, &quot;uom&quot; : &quot;cm&quot; }, &quot;instock&quot; : [ { &quot;warehouse&quot; : &quot;A&quot;, &quot;qty&quot; : 40 } ] }
  
{ &quot;item&quot; : &quot;postcard&quot;, &quot;status&quot; : &quot;A&quot;, &quot;size&quot; : { &quot;h&quot; : 10, &quot;w&quot; : 15.25, &quot;uom&quot; : &quot;cm&quot; }, &quot;instock&quot; : [ { &quot;warehouse&quot; : &quot;B&quot;, &quot;qty&quot; : 15 }, { &quot;warehouse&quot; : &quot;C&quot;, &quot;qty&quot; : 35 } ] }
  Return All Fields in Matching Documents
  db.inventory.find( { status: &quot;A&quot; } )
  SELECT * from inventory WHERE status = &quot;A&quot;
  Return the Specified Fields and the _id Field Only
  db.inventory.find( { status: &quot;A&quot; }, { item: 1, status: 1 } )
  SELECT _id, item, status from inventory WHERE status = &quot;A&quot;
  by default, the _id fields return in the matching documents.
  Suppress _id Field
  db.inventory.find( { status: &quot;A&quot; }, { item: 1, status: 1, _id: 0 } )
  SELECT item, status from inventory WHERE status = &quot;A&quot;
  Return All But the Excluded Fields
  db.inventory.find( { status: &quot;A&quot; }, { status: 0, instock: 0 } )
  With the exception of the _id field, you cannot combine inclusion and exclusion statements in projection documents.
  Return Specific Fields in Embedded Documents
  size字段只返回嵌入式文档中的uom字段:
  db.inventory.find(
  { status: &quot;A&quot; },
  { _id:0,item: 1, status: 1, &quot;size.uom&quot;: 1 }
  )
{ &quot;item&quot; : &quot;journal&quot;, &quot;status&quot; : &quot;A&quot;, &quot;size&quot; : { &quot;uom&quot; : &quot;cm&quot; } }  
{ &quot;item&quot; : &quot;notebook&quot;, &quot;status&quot; : &quot;A&quot;, &quot;size&quot; : { &quot;uom&quot; : &quot;in&quot; } }
  
{ &quot;item&quot; : &quot;postcard&quot;, &quot;status&quot; : &quot;A&quot;, &quot;size&quot; : { &quot;uom&quot; : &quot;cm&quot; } }
  Suppress Specific Fields in Embedded Documents
  size字段不返回嵌入式文档中的uom字段:
  db.inventory.find(
  { status: &quot;A&quot; },
  { &quot;size.uom&quot;: 0 }
  )
{ &quot;item&quot; : &quot;journal&quot;, &quot;status&quot; : &quot;A&quot;, &quot;size&quot; : { &quot;h&quot; : 14, &quot;w&quot; : 21 }, &quot;instock&quot; : [ { &quot;warehouse&quot; : &quot;A&quot;, &quot;qty&quot; : 5 } ] }  
{ &quot;item&quot; : &quot;notebook&quot;, &quot;status&quot; : &quot;A&quot;, &quot;size&quot; : { &quot;h&quot; : 8.5, &quot;w&quot; : 11 }, &quot;instock&quot; : [ { &quot;warehouse&quot; : &quot;C&quot;, &quot;qty&quot; : 5 } ] }
  
{ &quot;item&quot; : &quot;postcard&quot;, &quot;status&quot; : &quot;A&quot;, &quot;size&quot; : { &quot;h&quot; : 10, &quot;w&quot; : 15.25 }, &quot;instock&quot; : [ { &quot;warehouse&quot; : &quot;B&quot;, &quot;qty&quot; : 15 }, { &quot;warehouse&quot; : &quot;C&quot;, &quot;qty&quot; : 35 } ] }
  Projection on Embedded Documents in an Array 嵌入式文档在数组中的投影
  数组字段instock只返回嵌入式文档中的qty字段:
  db.inventory.find( { status: &quot;A&quot; }, { item: 1, status: 1, &quot;instock.qty&quot;: 1 } )
{ &quot;item&quot; : &quot;journal&quot;, &quot;status&quot; : &quot;A&quot;, &quot;instock&quot; : [ { &quot;qty&quot; : 5 } ] }  
{ &quot;item&quot; : &quot;notebook&quot;, &quot;status&quot; : &quot;A&quot;, &quot;instock&quot; : [ { &quot;qty&quot; : 5 } ] }
  
{ &quot;item&quot; : &quot;postcard&quot;, &quot;status&quot; : &quot;A&quot;, &quot;instock&quot; : [ { &quot;qty&quot; : 15 }, { &quot;qty&quot; : 35 } ] }
  Project Specific Array Elements in the Returned Array
  数组字段instock只返回最后一个元素:
  db.inventory.find( { status: &quot;A&quot; }, { _id:0,item: 1, status: 1, instock: { $slice: -1 } } )
{ &quot;item&quot; : &quot;journal&quot;, &quot;status&quot; : &quot;A&quot;, &quot;instock&quot; : [ { &quot;warehouse&quot; : &quot;A&quot;, &quot;qty&quot; : 5 } ] }  
{ &quot;item&quot; : &quot;notebook&quot;, &quot;status&quot; : &quot;A&quot;, &quot;instock&quot; : [ { &quot;warehouse&quot; : &quot;C&quot;, &quot;qty&quot; : 5 } ] }
  
{ &quot;item&quot; : &quot;postcard&quot;, &quot;status&quot; : &quot;A&quot;, &quot;instock&quot; : [ { &quot;warehouse&quot; : &quot;C&quot;, &quot;qty&quot; : 35 } ] }
  5、Query for Null or Missing Fields 查询null或空值
  db.inventory.insertMany([
  { _id: 1, item: null },
  { _id: 2 }
  ])
  Equality Filter
  db.inventory.find( { item: null } )
{ &quot;_id&quot; : 1, &quot;item&quot; : null }  
{ &quot;_id&quot; : 2 }
  Type Check
  db.inventory.find( { item : { $type: 10 } } ) //指定类型
{ &quot;_id&quot; : 1, &quot;item&quot; : null }  $type 有效的类型值,如下:
TypeNumberAliasNotesDouble1“double”String2“string”Object3“object”Array4“array”Binary data5“binData”Undefined6“undefined”Deprecated.ObjectId7“objectId”Boolean8“bool”Date9“date”Null10“null”Regular Expression11“regex”DBPointer12“dbPointer”Deprecated.JavaScript13“javascript”Symbol14“symbol”Deprecated.JavaScript (with scope)15“javascriptWithScope”32-bit integer16“int”Timestamp17“timestamp”64-bit integer18“long”Decimal12819“decimal”New in version 3.4.Min key-1“minKey”Max key127“maxKey”  Existence Check
  db.inventory.find( { item : { $exists: false } } )
{ &quot;_id&quot; : 2 }  查询和投影操作符
  Comparison Query Operators
  $eq      Matches values that are equal to a specified value.
  $gt      Matches values that are greater than a specified value.
  $gte      Matches values that are greater than or equal to a specified value.
  $lt      Matches values that are less than a specified value.
  $lte      Matches values that are less than or equal to a specified value.
  $ne      Matches all values that are not equal to a specified value.
  db.inventory.find( { qty: { $ne: 20 } } ) #$ne,把没有含有qty列的文档也会查询出来
  This query will select all documents in the inventory collection where the qty field value does not equal 20, including those documents that do not contain the qty field.
  $in      Matches any of the values specified in an array.
  $in操作符选择字段的值等于指定数组中的任何值的文档。
  db.inventory.find( { qty: { $in: [ 5, 15 ] } } )
  $nin      Matches none of the values specified in an array.
  $nin selects the documents where:

[*]  the field value is not in the specified array or
[*]  the field does not exist.
  db.inventory.find( { qty: { $nin: [ 5, 15 ] } } )
  Logical Query Operators
  $or      Joins query clauses with a logical OR returns all documents that match the conditions of either clause.
  $and      Joins query clauses with a logical AND returns all documents that match the conditions of both clauses.
  $not      Inverts the effect of a query expression and returns documents that do not match the query expression.
  返回与查询表达式不匹配的文档。
  db.inventory.find( { price: { $not: { $gt: 1.99 } } } )

[*]  the price field value is less than or equal to 1.99 or
[*]  the price field does not exist
  $nor      Joins query clauses with a logical NOR returns all documents that fail to match both clauses.
  使用逻辑NOR连接查询子句返回所有不匹配两个子句的文档。
  db.inventory.find( { $nor: [ { price: 1.99 }, { sale: true } ]} )

[*]  contain the price field whose value is not equal to 1.99 and contain the sale field whose value is not equal to true or
[*]  contain the price field whose value is not equal to 1.99 but do not contain the sale field or
[*]  do not contain the price field but contain the sale field whose value is not equal to true or
[*]  do not contain the price field and do not contain the sale field
  db.inventory.find( { $nor: [ { price: 1.99 }, { qty: { $lt: 20 } }, { sale: true } ] } )

[*]  the price field value does not equal 1.99 and
[*]  the qty field value is not less than 20 and
[*]  the sale field value is not equal to true
  including those documents that do not contain these field(s).
  Element Query Operators 元素查询操作符
  $existsMatches documents that have the specified field.
  { a: 5, b: 5, c: null }
  { a: 3, b: null, c: 8 }
  { a: null, b: 3, c: 9 }
  { a: 1, b: 2, c: 3 }
  { a: 2, c: 5 }
  { a: 3, b: 2 }
  { a: 4 }
  { b: 2, c: 4 }
  { b: 2 }
  { c: 6 }
  db.records.find( { a: { $exists: true } } )
  { a: 5, b: 5, c: null }
  { a: 3, b: null, c: 8 }
  { a: null, b: 3, c: 9 }
  { a: 1, b: 2, c: 3 }
  { a: 2, c: 5 }
  { a: 3, b: 2 }
  { a: 4 }
  db.records.find( { b: { $exists: false } } )
  { a: 2, c: 5 }
  { a: 4 }
  { c: 6 }
  $typeSelects documents if a field is of the specified type.
  Projection Operators
  $
  The positional $operator limits the contents of anfrom the query results to contain only the first element matching the query document.
  位置$运算符将查询结果中的的内容限制为仅包含与查询文档匹配的第一个元素。
  { &quot;_id&quot; : 1, &quot;semester&quot; : 1, &quot;grades&quot; : [ 70, 87, 90 ] }
  { &quot;_id&quot; : 2, &quot;semester&quot; : 1, &quot;grades&quot; : [ 90, 88, 92 ] }
  { &quot;_id&quot; : 3, &quot;semester&quot; : 1, &quot;grades&quot; : [ 85, 100, 90 ] }
  { &quot;_id&quot; : 4, &quot;semester&quot; : 2, &quot;grades&quot; : [ 79, 85, 80 ] }
  { &quot;_id&quot; : 5, &quot;semester&quot; : 2, &quot;grades&quot; : [ 88, 88, 92 ] }
  { &quot;_id&quot; : 6, &quot;semester&quot; : 2, &quot;grades&quot; : [ 95, 90, 96 ] }
  db.students.find( { semester: 1, grades: { $gte: 85 } },
  { &quot;grades.$&quot;: 1 } )
  { &quot;_id&quot; : 1, &quot;grades&quot; : [ 87 ] }
  { &quot;_id&quot; : 2, &quot;grades&quot; : [ 90 ] }
  { &quot;_id&quot; : 3, &quot;grades&quot; : [ 85 ] }
  { &quot;_id&quot; : 7, semester: 3, &quot;grades&quot; : [ { grade: 80, mean: 75, std: 8 },
  { grade: 85, mean: 90, std: 5 },
  { grade: 90, mean: 85, std: 3 } ] }
  { &quot;_id&quot; : 8, semester: 3, &quot;grades&quot; : [ { grade: 92, mean: 88, std: 8 },
  { grade: 78, mean: 90, std: 5 },
  { grade: 88, mean: 85, std: 3 } ] }
  db.students.find(
  { &quot;grades.mean&quot;: { $gt: 70 } },
  { &quot;grades.$&quot;: 1 }
  )
  //返回grades字段中mean值大于70的第一个元素:
  { &quot;_id&quot; : 7, &quot;grades&quot; : [{&quot;grade&quot; : 80,&quot;mean&quot; : 75,&quot;std&quot; : 8 } ] }
  { &quot;_id&quot; : 8, &quot;grades&quot; : [{&quot;grade&quot; : 92,&quot;mean&quot; : 88,&quot;std&quot; : 8 } ] }
  $elemMatch
  The $elemMatch operator limits the contents of anfield from the query results to contain only the first element matching the $elemMatch condition.
  $elemMatch操作符将查询结果中的字段的内容限制为仅包含与$elemMatch条件匹配的第一个元素。
  {
  _id: 1,
  zipcode: &quot;63109&quot;,
  students: [
  { name: &quot;john&quot;, school: 102, age: 10 },
  { name: &quot;jess&quot;, school: 102, age: 11 },
  { name: &quot;jeff&quot;, school: 108, age: 15 }
  ]
  }
  {
  _id: 2,
  zipcode: &quot;63110&quot;,
  students: [
  { name: &quot;ajax&quot;, school: 100, age: 7 },
  { name: &quot;achilles&quot;, school: 100, age: 8 },
  ]
  }
  {
  _id: 3,
  zipcode: &quot;63109&quot;,
  students: [
  { name: &quot;ajax&quot;, school: 100, age: 7 },
  { name: &quot;achilles&quot;, school: 100, age: 8 },
  ]
  }
  {
  _id: 4,
  zipcode: &quot;63109&quot;,
  students: [
  { name: &quot;barney&quot;, school: 102, age: 7 },
  { name: &quot;ruth&quot;, school: 102, age: 16 },
  ]
  }
  db.schools.find( { zipcode: &quot;63109&quot; },
  { students: { $elemMatch: { school: 102 } } } )
  { &quot;_id&quot; : 1, &quot;students&quot; : [ { &quot;name&quot; : &quot;john&quot;, &quot;school&quot; : 102, &quot;age&quot; : 10 } ] }
  { &quot;_id&quot; : 3 }
  { &quot;_id&quot; : 4, &quot;students&quot; : [ { &quot;name&quot; : &quot;barney&quot;, &quot;school&quot; : 102, &quot;age&quot; : 7 } ] }

[*]  For the document with _id equal to 1, the students array contains multiple elements with the school field equal to 102. However, the $elemMatch projection returns only the first matching element from the array.
[*]  The document with _id equal to 3 does not contain the students field in the result since no element in its students array matched the $elemMatch condition.
  db.schools.find( { zipcode: &quot;63109&quot; },
  { students: { $elemMatch: { school: 102, age: { $gt: 10} } } } )
  { &quot;_id&quot; : 1, &quot;students&quot; : [ { &quot;name&quot; : &quot;jess&quot;, &quot;school&quot; : 102, &quot;age&quot; : 11 } ] }
  { &quot;_id&quot; : 3 }
  { &quot;_id&quot; : 4, &quot;students&quot; : [ { &quot;name&quot; : &quot;ruth&quot;, &quot;school&quot; : 102, &quot;age&quot; : 16 } ] }
  The document with _id equal to 3 does not contain the students field since no array element matched the $elemMatch criteria.


页: [1]
查看完整版本: mongodb CRUD操作 -Select