以下是Mongo集合中的一些示例文档。虽然我正在处理负数,但它没有按预期给出正确的结果。我想在-2和-99之间搜索。所以这里最大值是-2,最小值是-99。但我的搜索给了我同样的零记录。
> db.btch_insrt.find({z: {"$gt" : -99, "$lt" : -2}}).count()
288
> db.btch_insrt.find({z: {"$gt" : -2, "$lt" : -99}}).count()
0
{ "_id" : ObjectId("59383e867421a8c9d281d29f"), "foo" : "bar", "baz" : 14, "z" : -4 }
{ "_id" : ObjectId("59383e867421a8c9d281d2a0"), "foo" : "bar", "baz" : 15, "z" : -5 }
{ "_id" : ObjectId("59383e867421a8c9d281d2a1"), "foo" : "bar", "baz" : 16, "z" : -6 }
{ "_id" : ObjectId("59383e867421a8c9d281d2a2"), "foo" : "bar", "baz" : 17, "z" : -7 }
{ "_id" : ObjectId("59383e867421a8c9d281d2a3"), "foo" : "bar", "baz" : 18, "z" : -8 }
{ "_id" : ObjectId("59383e867421a8c9d281d2a4"), "foo" : "bar", "baz" : 19, "z" : -9 }
{ "_id" : ObjectId("59383e867421a8c9d281d2a5"), "foo" : "bar", "baz" : 20, "z" : -10 }
{ "_id" : ObjectId("59383e867421a8c9d281d2a6"), "foo" : "bar", "baz" : 21, "z" : -11 }
{ "_id" : ObjectId("59383e867421a8c9d281d2a7"), "foo" : "bar", "baz" : 22, "z" : -12 }
{ "_id" : ObjectId("59383e867421a8c9d281d2a8"), "foo" : "bar", "baz" : 23, "z" : -13 }
{ "_id" : ObjectId("59383e867421a8c9d281d2a9"), "foo" : "bar", "baz" : 24, "z" : -14 }
{ "_id" : ObjectId("59383e867421a8c9d281d2aa"), "foo" : "bar", "baz" : 25, "z" : -15 }
{ "_id" : ObjectId("59383e867421a8c9d281d2ab"), "foo" : "bar", "baz" : 26, "z" : -16 }
{ "_id" : ObjectId("59383e867421a8c9d281d2ac"), "foo" : "bar", "baz" : 27, "z" : -17 }
{ "_id" : ObjectId("59383e867421a8c9d281d2ad"), "foo" : "bar", "baz" : 28, "z" : -18 }
{ "_id" : ObjectId("59383e867421a8c9d281d2ae"), "foo" : "bar", "baz" : 29, "z" : -19 }
{ "_id" : ObjectId("59383e867421a8c9d281d2af"), "foo" : "bar", "baz" : 30, "z" : -20 }
{ "_id" : ObjectId("59383e867421a8c9d281d2b0"), "foo" : "bar", "baz" : 31, "z" : -21 }
{ "_id" : ObjectId("59383e867421a8c9d281d2b1"), "foo" : "bar", "baz" : 32, "z" : -22 }
{ "_id" : ObjectId("59383e867421a8c9d281d2b2"), "foo" : "bar", "baz" : 33, "z" : -23 }
答案 0 :(得分:0)
使用$gte
(包括)其他$gt
(不包括)
{ "z": { $gt: NumberInt(-99), $lt: NumberInt(-2) } }
否定搜索
{ "z": { $not: { $gt: NumberInt(-99), $lt: NumberInt(-2) } } }
或者
{ $or: [ { "z": { $gt: NumberInt(-2) } }, { "z": { $lt: NumberInt(-99) } } ] }