zoukankan      html  css  js  c++  java
  • 11.Query an Array of Embedded Documents-官方文档摘录

    总结

    1.插入数据

    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 } ] }
    ]);

    2.查找嵌套在数组中的文档

    db.inventory.find({"instock":{"warehouse":"A","qty":5}})

    3.根据数组索引查找内嵌文档元素

    db.inventory.find({"instock.0.qty":{$lte:20}})

    如果不是精确到具体的索引元素,则只要里面满足即可展现

    db.inventory.find({"instock.qty":{$lte:20}})

    4.使用elemMatch来查找符合数组元素的条件

    db.inventory.find({"instock":{$elemMatch:{qty:5,warehouse:"A"}}})

    5.使用elemMatch查找范围

    db.inventory.find( { "instock": { $elemMatch: { qty: { $gt: 10, $lte: 20 } } } } )

    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:

    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:

    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 theinventory collection:

    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:

    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:

    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:

    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:

    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:

    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:

    db.inventory.find( { "instock.qty": 5, "instock.warehouse": "A" } )
    

    Additional Query Tutorials

    For additional query examples, see:

  • 相关阅读:
    11 Apr 18 类与对象 对象的使用 一切皆对象
    10 Apr 18 项目构架 hashlib模块 subprocess模块 configparser模块 面向对象的编程 类与对象
    9 Apr 18 shelve模块 xml模块 re模块
    8 Apr 18 包的使用 常用模块
    定义一个Student类,包含名字一个数据成员,定义无参构造函数、有参构造函数、拷贝构造函数、析构函数及对于名字的封装函数,在main函数中实例化Student对象,并访问相关函数,观察运行结果。
    皮马印第安人糖尿病数据集免费下载分享
    机器学习之模型训练(二)皮马印第安人糖尿病数据集
    anaconda的虚拟环境sklearn中如何安装pandas
    人工智能之模型评估之参数选择
    人工智能之模型评估之数据分离
  • 原文地址:https://www.cnblogs.com/olinux/p/7248760.html
Copyright © 2011-2022 走看看