MongoDB-9:当数据是字典or列表时如何筛选

db.users.find({filter})

*数据为{{},{},{}}形式

{
        "_id" : ObjectId("5a194d5795063d2f7e6434a2"),
        "name" : "jack",
        "age" : 13,
        "hobbies" : {
                "surfing" : {
                        "desc" : "great"
                },
                "snowing" : {
                        "desc" : "bad"
                }
        }
}

查找方法

db.users.find({'hobbies.surfing.desc':'great'})

*数据为{[{},{},{}]}形式

{
        "_id" : ObjectId("5a194942d97f41539378b81d"),
        "name" : "jack",
        "age" : 13,
        "hobbies" : [
                {
                        "name" : "hiking",
                        "level" : "great"
                },
                {
                        "name" : "snowing",
                        "level" : "bad"
                }
        ]
}

查找方法

db.users.find({'hobbies.name':'hiking'})
  • 数据为{[],[],[]}形式
{
        "_id" : ObjectId("5a1949c8d97f41539378b81e"),
        "name" : "zha",
        "age" : 93,
        "hobbies" : [
                "hiking",
                "snowing",
                "eating"
        ]
}

查找方法

db.users.find({"hobbies.hiking":{$exists: true}})


作者:余生筑
链接:https://www.jianshu.com/p/201c4eb78d13
来源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
posted @ 2022-02-12 16:43  一路向北321  阅读(76)  评论(0编辑  收藏  举报