• MongoDB_7:MongoDB下根据数组大小查询


    MongoDB使用"$size"

    对于下面的collection名字为test进行操作

    查询field名为test的字段,$size为3

    MongoVue中在find中输入


    <span style="font-size:14px;">{
    "test":{$size:3}
    }</span>
    实际执行语句为

    db.test.find({ "test" : { "$size" : 3 } }).limit(50);
    查询结果为

    <span style="color:#cc0000;">db.test.find({test:{$size:{$gt:2}}}); //错误</span>
    $size不能限定数组大小的范围,解决方案用$exists
    eg:比如查询数组test大小大于2的数据

    MongoVue中在find中输入

    {
    "test.2":{$exists:1}
    }
    实际执行语句为

    db.test.find({ "test.2" : { "$exists" : 1 } }).limit(50);
    表示查询test的第三个元素存在的数据,也就是test数组大小大于2的数据
    查询结果为


    看网上说的另外一种是用$where如下

    db.test.find({ $where: "this.test.length > 2" })
    本人试验没有成功,原因未知

  • 相关阅读:
    Swift
    Swift
    Swift
    Swift
    Swift
    Swift
    Swift
    C++生产和使用的临时对象
    RecyclerView0基于使用
    Docker创建MySQL集装箱
  • 原文地址:https://www.cnblogs.com/hailin2018/p/15393748.html
Copyright © 2020-2023  润新知