条件操作符号

MongoDB中条件操作符有:

  • (>) 大于 - $gt
  • (<) 小于 - $lt
  • (>=) 大于等于 - $gte
  • (<=) 小于等于 - $lte
  • (!= ) 不等于 - $ne

大于操作符 - $gt

> db.haha.find({"age":{$gt:10}})

{ "_id" : ObjectId("5d2ef9ab10c2764480fc19b1"), "name" : 23, "age" : 23 }

大于等于操作符 - $gte

> db.haha.find({"age":{$gte:10}})

{ "_id" : ObjectId("5d2ef9ab10c2764480fc19b1"), "name" : 23, "age" : 23 }

 小于操作符 - $lt

> db.haha.find({"age":{$lt:30}})

{ "_id" : ObjectId("5d2ef9ab10c2764480fc19b1"), "name" : 23, "age" : 23 }

小于等于操作符 - $lte

> db.haha.find({"age":{$lte:23}})

{ "_id" : ObjectId("5d2ef9ab10c2764480fc19b1"), "name" : 23, "age" : 23 }

不等于操作符 - $ne

> db.haha.find({"age":{$ne:23}})

{ "_id" : ObjectId("5d2f03e410c2764480fc19b2"), "name" : "alex", "hobby" : "烧烤" }
{ "_id" : ObjectId("5d2f03e410c2764480fc19b3"), "name" : "tom", "hobby" : "jack" }
{ "_id" : ObjectId("5d2f03fe10c2764480fc19b4"), "name" : "alex", "hobby" : "烧烤" }
{ "_id" : ObjectId("5d2f03fe10c2764480fc19b5"), "name" : "tom", "hobby" : "烧烤" }

模糊查询

查询包含指定条件的字段

> db.haha.find({"name":/t/})

{ "_id" : ObjectId("5d2f03e410c2764480fc19b3"), "name" : "tom", "hobby" : "jack" }
{ "_id" : ObjectId("5d2f03fe10c2764480fc19b5"), "name" : "tom", "hobby" : "烧烤" }

查询以指定条件开头的字段

> db.haha.find({"hobby":/^j/})

{ "_id" : ObjectId("5d2f03e410c2764480fc19b3"), "name" : "tom", "hobby" : "jack" }

查询以指定条件结尾的字段

> db.haha.find({"hobby":/烤$/})

{ "_id" : ObjectId("5d2f03e410c2764480fc19b2"), "name" : "alex", "hobby" : "烧烤" }
{ "_id" : ObjectId("5d2f03fe10c2764480fc19b4"), "name" : "alex", "hobby" : "烧烤" }
{ "_id" : ObjectId("5d2f03fe10c2764480fc19b5"), "name" : "tom", "hobby" : "烧烤" }

 

posted @ 2019-07-17 19:56  __Invoker  阅读(448)  评论(0编辑  收藏  举报