• mongodb-查询


    基本 语法: db.collection.find( <query filter>, <projection> )

    • query filter 过滤条件
    • projection 又返回的字段,默认全部返回 
      可以对cursor进行修饰,添加范围limits,跳过skips和排序sort限制。如果未进行sort(),返回集合是没有排序的。

        插入数据:

        db.users.insertMany( [ { _id: 1, name: "sue", age: 19, type: 1, status: "P", favorites: { artist: "Picasso", food: "pizza" }, finished: [ 17, 3 ], badges: [ "blue", "black" ], points: [ { points: 85, bonus: 20 }, { points: 85, bonus: 10 } ] }, { _id: 2, name: "bob", age: 42, type: 1, status: "A", favorites: { artist: "Miro", food: "meringue" }, finished: [ 11, 25 ], badges: [ "green" ], points: [ { points: 85, bonus: 20 }, { points: 64, bonus: 12 } ] }, { _id: 3, name: "ahn", age: 22, type: 2, status: "A", favorites: { artist: "Cassatt", food: "cake" }, finished: [ 6 ], badges: [ "blue", "red" ], points: [ { points: 81, bonus: 8 }, { points: 55, bonus: 20 } ] }, { _id: 4, name: "xi", age: 34, type: 2, status: "D", favorites: { artist: "Chagall", food: "chocolate" }, finished: [ 5, 11 ], badges: [ "red", "black" ], points: [ { points: 53, bonus: 15 }, { points: 51, bonus: 15 } ] }, { _id: 5, name: "xyz", age: 23, type: 2, status: "D", favorites: { artist: "Noguchi", food: "nougat" }, finished: [ 14, 6 ], badges: [ "orange" ], points: [ { points: 71, bonus: 20 } ] }, { _id: 6, name: "abc", age: 43, type: 1, status: "A", favorites: { food: "pizza", artist: "Picasso" }, finished: [ 18, 12 ], badges: [ "black", "blue" ], points: [ { points: 78, bonus: 8 }, { points: 57, bonus: 7 } ] } ] );

    Select All Documents in a Collection 查询所有集合

     db.users.find( {} )

    /*这个两个是一样的*/

    db.users.find()

    Specify Equality Condition eq 等于条件

    使用键值对<field>:<value>来表示 等于查询条件,会查出集合内<field>等于<value>的所有记录。

    db.users.find( { status: "A" } )

    Specify Conditions Using Query Operators 指定条件的查询操作

    查询条件可以用下面的格式设置查询条件:

    { <field1>: { <operator1>: <value1> }, ... }

    下面的示例检索user集合,查询其中status等于"P""D"的所有文件:

    db.users.find( { status: { $in: [ "P", "D" ] } } ) //in条件查询

    Specify AND Conditions AND 条件

    复合查询可以指定集合的多个字段条件,AND条件会在复合查询时隐式调用,下面的示例表示查询users集合中status等于"A"并且age小于30的所有文档。

    db.users.find( { status: "A", age: { $lt: 30 } } )//$lt less than 小于

    Specify OR Conditions OR 条件

    使用$or 关键字 ,可以从集合在至少满足一个$or连接的条件的文档。

    下面的示例,检索出的 status 等于 "A" 或age小于 30 的所有文档

    db.users.find(
       {
         $or: [ { status: "A" }, { age: { $lt: 30 } } ]
       }
    )

    Specify AND as well as OR Conditions 使用ORAND复合查询

    db.users.find(
       {
         status: "A",
         $or: [ { age: { $lt: 30 } }, { type: 1 } ]
       }
    )

    Exact Match on the Embedded Document 完全匹配

    {<field>:<value>}必须要完全匹配,包括field顺序。

    db.users.find( { favorites: { artist: "Picasso", food: "pizza" } } )
    • 1

    Equality Match on Fields within an Embedded Document 在嵌入对象匹配字段

    使用dot notation点符号匹配嵌入对象的指定字段。等于查询 (eq)嵌入对象的特定字段,会查询集合中所有含有指定字段和指定值的嵌入对象。嵌入对象可以含有不在查询范围内的其他字段

    db.users.find( { "favorites.artist": "Picasso" } )


    Query on Arrays 查询数组

    当字段包含一个数组,可以查询一个确切的阵列匹配或数组中的特定值。如果数组包含嵌入的文档,你可以使用点符号查询嵌入文件的特定字段。

    如果使用$elemMatch关键字指定多个条件,数组必须包含满足所有条件的至少一个元素。See Single Element Satisfies the Criteria.

    如果指定了多个条件,不使用$elemMatch关键字,必须满足所有条件。 See Combination of Elements Satisfies the Criteria

    Exact Match on an Array 完全匹配

    db.users.find( { badges: [ "blue", "black" ] } ) 
    //查询元素完全匹配(包括顺序)的文档.
    //查询badges数组内容为[ "blue", "black" ]的记录


    Match an Array Element 查询数组内含有该元素

    等于查询可以查询数组中的单个元素,会查出指定字段至少含有一个该元素的文档

    db.users.find( { badges: "black" } )
    //查询badges数组内含有为"black"的记录
    • 1
    • 2

    Match a Specific Element of an Array 查询数组指定位置的一个元素

    • Single Element Satisfies the Criteria 
      等于查询可以使用点符号查询数组指定索引或者位置的元素
    db.users.find( { "badges.0": "black" } )
    //查询badges数组第0个元素为"black"的记录
    • 1
    • 2
    • Specify Multiple Criteria for Array Elements 至少有一个满足查询条件 (AND) 
      使用$elemMatch关键字指定多个查询条件对数组进行查询。
    db.users.find( { finished: { $elemMatch: { $gt: 15, $lt: 20 } } } )
    //查询finished数组内一个元素满足 大于15 AND 小于20
    • 1
    • 2
    • Combination of Elements Satisfies the Criteria 满足查询条件的数组,
    db.users.find( { finished: { $gt: 15, $lt: 20 } } )
    //查询数组内所有元素满足 大于15,小于20
    //[25,11]  这个数组内 有元素大于15,有元素小于15。满足查询条件
    //[18,1] 满足
    • 1
    • 2
    • 3
    • 4

    Array of Embedded Documents 嵌套对象数组

    • Match a Field in the Embedded Document Using the Array Index 使用数组索引指定嵌入对象的字段 
      如果你指定要查询嵌入对象在数组中的索引,你可以使用点符号指定文档
    db.users.find( { 'points.0.points': { $lte: 55 } } )
    //查询points数组中第一个元素的points字段小于或等于55
    • 1
    • 2
    • Match a Field Without Specifying Array Index 查询一个字段,没有索引 
      如果不知道数组中的文档的索引位置,使用数组名 + 点符号+字段名 +查询条件 进行查询 

      db.users.find( { 'points.points': { $lte: 55 } } ) 
      //查询points数组中所有元素的points对象小于或等于55 

    Specify Multiple Criteria for Array of Documents 多条件查询对象数组

    • Single Element Satisfies the Criteria 单元素满足 ($elemMatch
      使用$elemMatch关键字 查询数组内至少一个元素的同时满足所有指定字段的条件
    db.users.find( { points: { $elemMatch: { points: { $lte: 70 }, bonus: 20 } } } )
    //查询`points`数组中至少有个元素 points小于等于70 bonus等于20 的记录
    • 1
    • 2
    • Combination of Elements Satisfies the Criteria 任意元素满足条件
    db.users.find( { "points.points": { $lte: 70 }, "points.bonus": 20 } )
    //查询 数组内 任意元素 满足 points小于等于70 和 bonus等于20
    • 1
    • 2

    Additional Methods 其他方法

    db.collection.findOne()

    等同于db.collection.find().limit(1)

     
     
  • 相关阅读:
    linux下mysql安装
    出现GC overhead limit exceeded 的解决方案
    什么是OOM?如何解决OOM问题!
    老司机告诉你:别再被忽悠,汽车节气门这样洗最养车
    HDU 4352 XHXJ&#39;s LIS(数位dp&amp;状态压缩)
    Linux bash: scp: command not found的问题记录
    Codeforces Round #315 (Div. 2)
    【营销】非常重要
    firebug的应用
    powerdesigner中实现PDM到MYSQl数据库的转换
  • 原文地址:https://www.cnblogs.com/anxbb/p/9351084.html
Copyright © 2020-2023  润新知