mongodb 条件查询
$gt $gte $eq $lte $lt
$and $or $nor
$in $not $ne
$all $size
> db.student.find()
{ "_id" : 1, "name" : { "firstname" : "wl", "lastname" : "wei" }, "num" : 21 }
{ "_id" : 2, "name" : { "firstname" : "rui", "lastname" : "cao" }, "num" : 22 }
{ "_id" : 3, "name" : { "firstname" : "hj", "lastname" : "yu" }, "num" : 23 }
> db.student.find({$nor:[{_id:1},{num:{$gte:21,$lte:23,$eq:22}}]}) // $nor $eq
{ "_id" : 3, "name" : { "firstname" : "hj", "lastname" : "yu" }, "num" : 23 }
> db.student.find({$nor:[{_id:1},{num:{$gte:21,$lte:23,$ne:22}}]}) //$nor $ne
{ "_id" : 2, "name" : { "firstname" : "rui", "lastname" : "cao" }, "num" : 22 }
> db.student.find({_id:{$not:{$in:[2,1]}}}) //$not $in
{ "_id" : 3, "name" : { "firstname" : "hj", "lastname" : "yu" }, "num" : 23 }
> db.food.find()
{ "_id" : ObjectId("5ce7452beacf9d5648fa8811"), "fruit" : [ "apple", "banana", "peach" ] }
{ "_id" : 1, "fruit" : [ "apple", "banana", "peach" ] }
{ "_id" : 2, "fruit" : [ "apple", "kumquat", "orange" ] }
{ "_id" : 3, "fruit" : [ "cherry", "banana", "apple" ] }
> db.food.find({fruit:{$all:["apple","banana"]}}) // $all
{ "_id" : ObjectId("5ce7452beacf9d5648fa8811"), "fruit" : [ "apple", "banana", "peach" ] }
{ "_id" : 1, "fruit" : [ "apple", "banana", "peach" ] }
{ "_id" : 3, "fruit" : [ "cherry", "banana", "apple" ] }
> db.food.find({fruit:{$size:3}}) //$size
{ "_id" : ObjectId("5ce7452beacf9d5648fa8811"), "fruit" : [ "apple", "banana", "peach" ] }
{ "_id" : 1, "fruit" : [ "apple", "banana", "peach" ] }
{ "_id" : 2, "fruit" : [ "apple", "kumquat", "orange" ] }
{ "_id" : 3, "fruit" : [ "cherry", "banana", "apple" ] }
> db.person.find()
{ "_id" : ObjectId("5ce7452beacf9d5648fa880b"), "name" : { "firstname" : "anjel", "lastname" : "ana" }, "age" : 35 }
{ "_id" : ObjectId("5ce7452beacf9d5648fa880c"), "name" : "li", "age" : 26, "gender" : "male" }
{ "_id" : ObjectId("5ce7452beacf9d5648fa880d"), "name" : "chen", "age" : 24 }
{ "_id" : ObjectId("5ce7452beacf9d5648fa880e"), "name" : "", "age" : 15 }
{ "_id" : ObjectId("5ce7452beacf9d5648fa880f"), "" : "cherry", "age" : 15 }
{ "_id" : ObjectId("5ce7452beacf9d5648fa8810"), "name" : "Chen", "age" : 27 }
{ "_id" : 1 }
{ "_id" : 2 }
{ "_id" : ObjectId("5cfc87bcc69f67914d6dd68c"), "name" : "ann", "age" : 30 }
> db.person.find({age:{$ne:30},name:{$not:/ann/}})
{ "_id" : ObjectId("5ce7452beacf9d5648fa880b"), "name" : { "firstname" : "anjel", "lastname" : "ana" }, "age" : 35 }
{ "_id" : ObjectId("5ce7452beacf9d5648fa880c"), "name" : "li", "age" : 26, "gender" : "male" }
{ "_id" : ObjectId("5ce7452beacf9d5648fa880d"), "name" : "chen", "age" : 24 }
{ "_id" : ObjectId("5ce7452beacf9d5648fa880e"), "name" : "", "age" : 15 }
{ "_id" : ObjectId("5ce7452beacf9d5648fa880f"), "" : "cherry", "age" : 15 }
{ "_id" : ObjectId("5ce7452beacf9d5648fa8810"), "name" : "Chen", "age" : 27 }
{ "_id" : 1 }
{ "_id" : 2 }