查询多个条件(pretty是数据格式化) >db.库名.find({key1:value1, key2:value2}).pretty() 案例:> db.col.find({"by":"菜鸟教程", "title":"MongoDB 教程"}).pretty() { "_id" : ObjectId("56063f17ade2f21f36b03133"), "title" : "MongoDB 教程", "description" : "MongoDB 是一个 Nosql 数据库", "by" : "菜鸟教程", "url" : "http://www.runoob.com", "tags" : [ "mongodb", "database", "NoSQL" ], "likes" : 100 }类似SQL语句 WHERE by='菜鸟教程' AND title='MongoDB 教程'
OR 条件语句:>db.库名.find( { $or: [ {key1: value1}, {key2:value2} ] } ).pretty()AND和OR一起使用(SQL 语句为:'where likes>50 AND (by = '菜鸟教程' OR title = 'MongoDB 教程')')>db.col.find( { $or:[{"by":"菜鸟教程"},{"title": "MongoDB 教程"}] } ).pretty() { "_id" : ObjectId("56063f17ade2f21f36b03133"), "title" : "MongoDB 教程", "description" : "MongoDB 是一个 Nosql 数据库", "by" : "菜鸟教程", "url" : "http://www.runoob.com", "tags" : [ "mongodb", "database", "NoSQL" ], "likes" : 100 }>db.col.find( { "likes": {$gt:50}, $or: [{"by": "菜鸟教程"},{"title": "MongoDB 教程"}] } ).pretty() likes大于50并且(by等于菜鸟教程或者title等于MongDB教程) { "_id" : ObjectId("56063f17ade2f21f36b03133"), "title" : "MongoDB 教程", "description" : "MongoDB 是一个 Nosql 数据库", "by" : "菜鸟教程", "url" : "http://www.runoob.com", "tags" : [ "mongodb", "database", "NoSQL" ], "likes" : 100 }
mongdb的查询深入
最新推荐文章于 2024-08-29 03:49:30 发布