mongo数组查询

一.嵌入式文档的查询

{
    "_id": 1,
    "user": {
        "name": "zhangsan",
        "user_id": 1,
        "age": 18
    }
}

  直接用点取值就可以:

  

db.getCollection('test').find({'user.age':{$gt:18}},{'_id':0,'user.age':1})

 

二.数组查询

{
    "_id": 111,
    "size": ['x', 's', 'xl', 'm'],
    "name": "裤子",
   "price":[100,300,400]
}

   1.查询数组包含的情况

db.getCollection('test').find({'size':'m'})

  2.查询数组不包含的情况

db.getCollection('test').find({'size':{$ne:'m'}})

  3.查询数组至少有一个元素在某个范围

db.getCollection('test').find({'price':{'$gte:100,'$lt':500}})

  4.数组长度查询(长度必须是具体长度,而不能是范围长度)

db.getCollection('test').find({'price':{'$size':2}})

  5.数组索引查询

db.getCollection('test').find({'size.0':'m'})

 

db.getCollection('test').find({'price.0':{'$gt':500}})

 

posted @ 2020-06-24 00:18  阿布_alone  阅读(1444)  评论(0编辑  收藏  举报
TOP