• 企业400电话
  • 网络优化推广
  • AI电话机器人
  • 呼叫中心
  • 全 部 栏 目

    网站建设 商标✡知产 微网小程序 电商运营 彩铃•短信 增值拓展业务
    MongoDB操作符中的$elemMatch问题
    POST TIME:2021-10-18 23:15

    问题

    如果MongoDB 数据库集合中仅存在一条记录

    {
      "_id" : ObjectId("5e6b4ef546b5f44e5c5b276d"),
      "name" : "赵小明",
      "used_name" : [ 
        "赵明", 
        "赵小朋"
      ],
      "age" : 16,
      "gender" : 0,
      "relatives" : [ 
        {
          "name" : "赵刚",
          "relationship" : 0
        }, 
        {
          "name" : "秀英",
          "relationship" : 1
        }
      ]
    }

    我们执行查询

    db.getCollection('Persion').find({"relatives.name": "赵刚", "relatives.relationship": 1})

    此时会得到结果吗?

    最开始我想当然的以为是不会出现结果的,但结果往往与期望背道而驰。

    什么,一瞬间我陷入了迷茫,Mongo的查询结果不是必须都满足所有条件的吗?

    分析

    不信邪的我又尝试了喜闻乐见的小白查询

    db.getCollection('Persion').find({"name": "赵小明", "age": 18})

    这次结果为空,嗯,这才是我熟悉的Mongo嘛?

    那这两次查询有啥区别呢?不同有两点

    那我们将数据改为

    {
      "_id" : ObjectId("5e6b4ef546b5f44e5c5b276d"),
      "name" : "赵小明",
      "used_name" : [ 
        "赵明", 
        "赵小朋"
      ],
      "age" : 16,
      "gender" : 0,
      "relative" : {
        "name" : "赵刚",
        "relationship" : 0
      }
    }

    继续执行查询

    db.getCollection('Persion').find({"relatives.name": "赵刚", "relatives.relationship": 1})

    此次结果为空集

    接下来尝试查询

    db.getCollection('Persion').find({"relatives.name": "赵刚", "relatives.relationship": 0})

    此次可得到一条结果

    通过上述两次查询基本可以排除二级字段的影响

    那就是数组的原因了,那具体是为什么呢?

    将数据还原为最初的格式,继续进行不同的查询

    db.getCollection('Persion').find({"relatives.name": "赵刚", "relatives.relationship": 2})

    此次结果为空集

    那我们可以得到结论,对于数组字段,每个查询条件仅需有数组中的一项满足条件即可,而不是数组中必须存在一项满足所有查询条件。

    那如果我想达到后面的效果要怎么做呢?

    解决

    此时,我们需要用到我们今天的主角 $elemMatch ,它的官方定义是这样的:

    The $elemMatch operator matches documents that contain an array field with at least one element that matches all the specified query criteria.

    { field>: { $elemMatch: { query1>, query2>, ... } } }
    If you specify only a single condition in the $elemMatch expression, you do not need to use $elemMatch.

    You cannot specify a $where expression in an $elemMatch.
    You cannot specify a $text query expression in an $elemMatch.

    那上边的查询我们可以改成

    db.getCollection('Persion').find({"relatives":{"$elemMatch":{"name": "赵四", "relationship": 0}}})

    此时可以得到结果,但

    db.getCollection('Persion').find({"relatives":{"$elemMatch":{"name": "赵四", "relationship": 1}}})

    结果为空集

    结语

    此操作符和索引也有一些不得不说的事,今天就不在这里细说了,之后我会专门总结一篇有关MongoDB索引相关的博客

    等不及的看官可以自行百度Google一下。

    总结

    到此这篇关于MongoDB操作符之$elemMatch的文章就介绍到这了,更多相关MongoDB操作符之$elemMatch内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

    您可能感兴趣的文章:
    • MongoDB 管道的介绍及操作符实例
    • 基于MongoDB数据库的数据类型和$type操作符详解
    上一篇:mongodb driver使用代码详解
    下一篇:MongoDB的基本操作实例详解【服务端启动,客户端连接,CRUD操作】
  • 相关文章
  • 

    关于我们 | 付款方式 | 荣誉资质 | 业务提交 | 代理合作


    © 2016-2020 巨人网络通讯

    时间:9:00-21:00 (节假日不休)

    地址:江苏信息产业基地11号楼四层

    《增值电信业务经营许可证》 苏B2-20120278

    X

    截屏,微信识别二维码

    微信号:veteran88

    (点击微信号复制,添加好友)

     打开微信