MongoDB(filter数据筛选)

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
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞