如何用$ lte和$ in查询MongoDB的值?
首先让我们创建一个包含文档的集合-
> db.queryMongoValueDemo.insertOne( { _id:101, "ScoreDetails":[{Score:80},{Score:45},{Score:25},{Score:70}] } ); { "acknowledged" : true, "insertedId" : 101 } > db.queryMongoValueDemo.insertOne( { _id:102, "ScoreDetails":[{Score:80},{Score:24},{Score:34}] } ); { "acknowledged" : true, "insertedId" : 102 } > db.queryMongoValueDemo.insertOne( { _id:103, "ScoreDetails":[{Score:99},{Score:95}] } ); { "acknowledged" : true, "insertedId" : 103 }
在find()
方法的帮助下显示集合中的所有文档-
> db.queryMongoValueDemo.find().pretty();
这将产生以下输出-
{ "_id" : 101, "ScoreDetails" : [ { "Score" : 80 }, { "Score" : 45 }, { "Score" : 25 }, { "Score" : 70 } ] } { "_id" : 102, "ScoreDetails" : [ { "Score" : 80 }, { "Score" : 24 }, { "Score" : 34 } ] } { "_id" : 103, "ScoreDetails" : [ { "Score" : 99 }, { "Score" : 95 } ] }
查询$not与$gt运算符一起实现的$lte和$in运算符-
> db.queryMongoValueDemo.find({ "ScoreDetails.Score": { "$eq": 80, "$not": { "$gt": 80 } } });
这将产生以下输出-
{ "_id" : 101, "ScoreDetails" : [ { "Score" : 80 }, { "Score" : 45 }, { "Score" : 25 }, { "Score" : 70 } ] } { "_id" : 102, "ScoreDetails" : [ { "Score" : 80 }, { "Score" : 24 }, { "Score" : 34 } ] }