美文网首页
MongoDB 数组

MongoDB 数组

作者: Eve0 | 来源:发表于2017-08-21 15:07 被阅读0次

    数据准备

    {
            "_id" : ObjectId("59982b08b8f62e79cdc29da2"),
            "name" : "lily",
            "age" : 12
    }
    { "_id" : ObjectId("5999ebe32af74d2adccf516a"), "name" : "t", "age" : 13 }
    {
            "_id" : ObjectId("599ac13091cf72aa04177629"),
            "name" : "xiaowang",
            "age" : 16,
            "habbit" : [
                    "banana",
                    "apple",
                    "peach"
            ],
            "score" : [
                    51,
                    62
            ]
    }
    {
            "_id" : ObjectId("599ac13091cf72aa0417762a"),
            "name" : "xiaoli",
            "age" : 17,
            "habbit" : [
                    "apple",
                    "peach"
            ],
            "score" : [
                    73,
                    23
            ]
    }
    {
            "_id" : ObjectId("599ac13091cf72aa0417762b"),
            "name" : "xiaoming",
            "age" : 16,
            "habbit" : [
                    "banana",
                    "apple",
                    "peach",
                    "grape"
            ],
            "score" : [
                    83,
                    97
            ]
    }
    {
            "_id" : ObjectId("599ac13091cf72aa0417762c"),
            "name" : "xiaohong",
            "age" : 17,
            "habbit" : [
                    "banana",
                    "grape",
                    "tomato"
            ],
            "score" : [
                    87,
                    83
            ]
    }
    {
            "_id" : ObjectId("599ac13091cf72aa0417762d"),
            "name" : "xiaofang",
            "age" : 18,
            "habbit" : [
                    "peach",
                    "grape"
            ],
            "score" : [
                    54,
                    67
            ]
    }
    {
            "_id" : ObjectId("599ac13091cf72aa0417762e"),
            "name" : "xiaoqiu",
            "age" : 17,
            "habbit" : [
                    "potato",
                    "tomato"
            ],
            "score" : [
                    91,
                    82
            ]
    }
    {
            "_id" : ObjectId("599ac13091cf72aa0417762f"),
            "name" : "xiaozhang",
            "age" : 19,
            "habbit" : [
                    "peach",
                    "grape"
            ],
            "score" : [
                    79,
                    83
            ]
    }
    {
            "_id" : ObjectId("599ac13091cf72aa04177630"),
            "name" : "xiaowang",
            "age" : 16,
            "habbit" : [
                    "banana",
                    "apple",
                    "peach"
            ],
            "score" : [
                    87,
                    89
            ]
    }
    {
            "_id" : ObjectId("599ac13091cf72aa04177631"),
            "name" : "xiaoli",
            "age" : 17,
            "habbit" : [
                    "apple",
                    "peach"
            ],
            "score" : [
                    93,
                    95
            ]
    }
    

    1.全文匹配$all

    db.userlist.find({habbit:{$all:['grape','peach']}}).pretty()#habbit包含peach和grape的

    {
            "_id" : ObjectId("599ac13091cf72aa0417762b"),
            "name" : "xiaoming",
            "age" : 16,
            "habbit" : [
                    "banana",
                    "apple",
                    "peach",
                    "grape"
            ],
            "score" : [
                    83,
                    97
            ]
    }
    {
            "_id" : ObjectId("599ac13091cf72aa0417762d"),
            "name" : "xiaofang",
            "age" : 18,
            "habbit" : [
                    "peach",
                    "grape"
            ],
            "score" : [
                    54,
                    67
            ]
    }
    {
            "_id" : ObjectId("599ac13091cf72aa0417762f"),
            "name" : "xiaozhang",
            "age" : 19,
            "habbit" : [
                    "peach",
                    "grape"
            ],
            "score" : [
                    79,
                    83
            ]
    }
    
    

    2.符号运算(>=<)

    下边的文档查询的例子是查询score元素:一个元素可以满足>70,另一个元素<90,或者是一个元素可以同时满足这两个条件。
    db.userlist.find({score:{$gt:70,$lt:99}})

    3. 元素存在$exists

    • 某属性是否存在$exists
      db.userlist.find({habbit:{$exists:true}})#查询userlist中有habbit元素的所有文档
      db.userlist.find({habbit:{$exists:false}})#查询userlist中没有habbit元素的所有文档

    4. 根据元素的个数查询

    db.userlist.find({habbit:{$size:3}})#查询有3个habbit的人
    显示结果:

    {
            "_id" : ObjectId("599ac13091cf72aa04177629"),
            "name" : "xiaowang",
            "age" : 16,
            "habbit" : [
                    "banana",
                    "apple",
                    "peach"
            ],
            "score" : [
                    51,
                    62
            ]
    }
    {
            "_id" : ObjectId("599ac13091cf72aa0417762c"),
            "name" : "xiaohong",
            "age" : 17,
            "habbit" : [
                    "banana",
                    "grape",
                    "tomato"
            ],
            "score" : [
                    87,
                    83
            ]
    }
    {
            "_id" : ObjectId("599ac13091cf72aa04177630"),
            "name" : "xiaowang",
            "age" : 16,
            "habbit" : [
                    "banana",
                    "apple",
                    "peach"
            ],
            "score" : [
                    87,
                    89
            ]
    }
    

    5.数组中指定下标

    db.userlist.find({'habbit.2':'peach'})#寻找habbit下标2为peach的元素。

    {
            "_id" : ObjectId("599ac13091cf72aa04177629"),
            "name" : "xiaowang",
            "age" : 16,
            "habbit" : [
                    "banana",
                    "apple",
                    "peach"
            ],
            "score" : [
                    51,
                    62
            ]
    }
    {
            "_id" : ObjectId("599ac13091cf72aa0417762b"),
            "name" : "xiaoming",
            "age" : 16,
            "habbit" : [
                    "banana",
                    "apple",
                    "peach",
                    "grape"
            ],
            "score" : [
                    83,
                    97
            ]
    }
    {
            "_id" : ObjectId("599ac13091cf72aa04177630"),
            "name" : "xiaowang",
            "age" : 16,
            "habbit" : [
                    "banana",
                    "apple",
                    "peach"
            ],
            "score" : [
                    87,
                    89
            ]
    }
    

    6.元素匹配$elemMatch

    数据准备:

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

    查询stock中warehouse=C的元素:
    db.inventory.find({'instock':{$elemMatch:{warehouse:'C'}}})

    { "_id" : ObjectId("599af38691cf72aa04177632"), "item" : "journal", "instock" : [ { "warehouse" : "A", "qty" : 5 }, { "warehouse" : "C", "qty" : 15 } ] }
    { "_id" : ObjectId("599af38691cf72aa04177633"), "item" : "notebook", "instock" : [ { "warehouse" : "C", "qty" : 5 } ] }
    { "_id" : ObjectId("599af38691cf72aa04177636"), "item" : "postcard", "instock" : [ { "warehouse" : "B", "qty" : 15 }, { "warehouse" : "C", "qty" : 35 } ] }
    

    7.$where

    db.userlist.find({$where:'this.age<17'})

    相关文章

      网友评论

          本文标题:MongoDB 数组

          本文链接:https://www.haomeiwen.com/subject/ysdcdxtx.html