MongoDB查询系统

  首先,我们先向集合(collections)中添加测试文档(documents)。如下:

> for(i=1;i<=5;i++) db.test.insert({x:i,y:i*i,z:6-i})
WriteResult({ "nInserted" : 1 })

  1、下面查看我们插入的数据:

> db.test.find()
{ "_id" : ObjectId("59361be7c718d6f408c6cae5"), "x" : 1, "y" : 1, "z" : 5 }
{ "_id" : ObjectId("59361be7c718d6f408c6cae6"), "x" : 2, "y" : 4, "z" : 4 }
{ "_id" : ObjectId("59361be7c718d6f408c6cae7"), "x" : 3, "y" : 9, "z" : 3 }
{ "_id" : ObjectId("59361be7c718d6f408c6cae8"), "x" : 4, "y" : 16, "z" : 2 }
{ "_id" : ObjectId("59361be7c718d6f408c6cae9"), "x" : 5, "y" : 25, "z" : 1 }

  2、只查看部分数据字段

> db.test.find({},{_id:0,x:1})
{ "x" : 1 }
{ "x" : 2 }
{ "x" : 3 }
{ "x" : 4 }
{ "x" : 5 }(其中,第一个{}中是筛选条件,第二个{}中是决定哪些数据字段显示,即过滤器,0表示不显示,1表示显示)

  3、按指定条件筛选数据

> db.test.find({x:1},{_id:0})
{ "x" : 1, "y" : 1, "z" : 5 }

  4、比较部分

操作

格式

范例

等于

{<key>:<value>}

db.col.find({x:1,y:1}).pretty()

小于

{<key>:{$lt:<value>}}

db.col.find({x:{$lt:3}}).pretty()

小于或等于

{<key>:{$lte:<value>}}

db.col.find({x:{$lte:3}}).pretty()

大于

{<key>:{$gt:<value>}}

db.col.find({x:{$gt:3}}).pretty()

大于或等于

{<key>:{$gte:<value>}}

db.col.find({x:{$gte:3}).pretty()

不等于

{<key>:{$ne:<value>}}

db.col.find({x:{$ne:1}}).pretty()

范围选择1

{<key>:{$lt:<val>,$gte:<val>}}

db.test.find({x:{$lt:4,$gte:2}})

范围选择2

{<key>:{$in:[value1,value2]}}

db.test.find({x:{$in:[1,2]}})

范围选择3

{<key>:{$nin:[value1,value2]}}

db.test.find({x:{$nin:[1,2]}})

  其中,$nin、$ne等是一种比较低效的查询选择器,它们会进行全表扫描,因此,最好不要单独使用。并且单独使用$ne也不会利用索引的优势,非常低效。

  5、And与Or

操作

格式

范例

效果

And

{$and:[{<key>:<value>},{}....]}

db.test.find({$and:[{x:2},{y:4}]},{_id:0})

{ "x" : 2, "y" : 4, "z" : 4 }

Or

{$or:[{<key>:<value>},{}....]}

db.test.find({$or:[{x:2},{y:1}]},{_id:0})

{ "x" : 1, "y" : 1, "z" : 5 }

{ "x" : 2, "y" : 4, "z" : 4 }

And Or

省略

db.test.find({x:{$lt:4},$or:[{y:4},{z:5}]},{_id:0})

{ "x" : 1, "y" : 1, "z" : 5 }

{ "x" : 2, "y" : 4, "z" : 4 }

  6、exists

    在mongodb中$exists与关系数据库中的exists不一样,因为在MongoDB中表(集合collection)结结构不是固定的,有的时候需要返回包含有某个字段的所有记录或者不包含某个字段的所有字段,$exists这时就可以派上用场了。此处我们为了测试就在插入一条数据:

> db.test.insert({x:1,y:1,flag:true})
WriteResult({ "nInserted" : 1 })

  下面我们假设需要访问包含flag字段的文档,如下:

> db.test.find({flag:{$exists:true}})
{ "_id" : ObjectId("59364d6561dce208b23fc306"), "x" : 1, "y" : 1, "flag" : true }

  当然,为了实现这种需求,我们也可以用另外一种语句来代替$exists

> db.test.find({flag:{$ne:null}})
{ "_id" : ObjectId("59364d6561dce208b23fc306"), "x" : 1, "y" : 1, "flag" : true }

  7、嵌套查询

  下面,向test中添加一个嵌套形式的数据,即某个字段的值也是一个BSON对象

> db.test.insert({id:1,name:"xiaoming",detail:{sex:"male",age:20}})
WriteResult({ "nInserted" : 1 })

> db.test.find({"detail.age":20}).pretty()
{
"_id" : ObjectId("5936507961dce208b23fc307"),
"id" : 1,
"name" : "xiaoming",
"detail" : {
"sex" : "male",
"age" : 20
}
}

  嵌套查询时匹配的key如果有多级嵌套深度,就一级一级地用点号展开

  8、数组操作

> db.test.insert({id:1,name:"xiaoliang",detail:[{sex:"male",age:22},{address:"china",post:5}]})
WriteResult({ "nInserted" : 1 })

> db.test.find({"detail.1.post":5}).pretty()
{
"_id" : ObjectId("593655b461dce208b23fc308"),
"id" : 1,
"name" : "xiaoliang",
"detail" : [
{
"sex" : "male",
"age" : 22
},
{
"address" : "china",
"post" : 5
}
]
}

匹配字符串中先取需要匹配的key(detail),由于detail键对应的value为数组,detail.1表示要取数组中的第2个位置处的元素,又由于数组的元素有是个BSON文档对象,因此,我们可以通过detail.1.post定位到需要匹配的键。

  9、查询最新数据

> db.ttt.find().pretty()
{
"_id" : ObjectId("593270bd0976e8f92b2d4514"),
"id" : 1,
"StatusInfo" : [
{
"status" : 9,
"desc" : "已取消"
},
{
"status" : 2,
"desc" : "已付款"
}
]
}

这是我们的测试数据,在现实需求中我们经常遇到需要查询的是最新数据,对于数据类型,我们有专门的操作符$silce来完成

> db.ttt.find({id:1},{_id:0,"StatusInfo":{"$slice":-1},"StatusInfo.desc":1})
{ "StatusInfo" : [ { "desc" : "已付款" } ] }

  10、正则查询

> db.test.find({age:20})
{ "_id" : ObjectId("59365ce461dce208b23fc309"), "name" : "zhangsan", "age" : 20 }
{ "_id" : ObjectId("59365ced61dce208b23fc30a"), "name" : "zhanan", "age" : 20 }

查询"name"以"zhan"开头的数据:

> db.test.find({name:{$regex:"zhan"}})
{ "_id" : ObjectId("59365ce461dce208b23fc309"), "name" : "zhangsan", "age" : 20 }
{ "_id" : ObjectId("59365ced61dce208b23fc30a"), "name" : "zhanan", "age" : 20 }

当然还是可以这样的:

> db.test.find({name:/zhan/})
{ "_id" : ObjectId("59365ce461dce208b23fc309"), "name" : "zhangsan", "age" : 20 }
{ "_id" : ObjectId("59365ced61dce208b23fc30a"), "name" : "zhanan", "age" : 20 }
> db.test.find({name:/han/})
{ "_id" : ObjectId("59365ce461dce208b23fc309"), "name" : "zhangsan", "age" : 20 }
{ "_id" : ObjectId("59365ced61dce208b23fc30a"), "name" : "zhanan", "age" : 20 }

忽略大小写查询

> db.test.find({name:{$regex:"zhan",$options:"$i"}})
{ "_id" : ObjectId("59365ce461dce208b23fc309"), "name" : "zhangsan", "age" : 20 }
{ "_id" : ObjectId("59365ced61dce208b23fc30a"), "name" : "zhanan", "age" : 20 }
{ "_id" : ObjectId("59365f1161dce208b23fc30b"), "name" : "ZHANbsd", "age" : 22 }
> db.test.find({name:/zhan/i})
{ "_id" : ObjectId("59365ce461dce208b23fc309"), "name" : "zhangsan", "age" : 20 }
{ "_id" : ObjectId("59365ced61dce208b23fc30a"), "name" : "zhanan", "age" : 20 }
{ "_id" : ObjectId("59365f1161dce208b23fc30b"), "name" : "ZHANbsd", "age" : 22 }

 

posted @ 2017-06-06 15:55  追求沉默者  阅读(2492)  评论(0编辑  收藏  举报