--多条件查询。下面的示例等同于SQL语句的where name = "stephen" and age = 35
> db.test.find({"name":"stephen","age":35})
{ "_id" : ObjectId("4fd58ecbb9ac507e96276f1a"), "name" : "stephen", "age" : 35, "genda" : "male", "email" : "stephen@hotmail.com" }
--返回指定的文档键值对。下面的示例将只是返回name和age键值对。
> db.test.find({}, {"name":1,"age":1})
{ "_id" : ObjectId("4fd58ecbb9ac507e96276f1a"), "name" : "stephen", "age" : 35 }
--指定不返回的文档键值对。下面的示例将返回除name之外的所有键值对。
> db.test.find({}, {"name":0})
{ "_id" : ObjectId("4fd58ecbb9ac507e96276f1a"), "age" : 35, "genda" : "male", "email" : "stephen@hotmail.com" }
MongoDB提供了一组比较操作符:$lt/$lte/$gt/$gte/$ne,依次等价于</<=/>/>=/!=。
--下面的示例返回符合条件age >= 18 && age <= 40的文档。
> db.test.find({"age":{"$gte":18, "$lte":40}})
{ "_id" : ObjectId("4fd58ecbb9ac507e96276f1a"), "name" : "stephen", "age" : 35,"genda" : "male", "email" : "stephen@hotmail.com" }
--下面的示例返回条件符合name != "stephen1"
> db.test.find({"name":{"$ne":"stephen1"}})
{ "_id" : ObjectId("4fd58ecbb9ac507e96276f1a"), "name" : "stephen", "age" : 35,"genda" : "male", "email" : "stephen@hotmail.com" }
--$in等同于SQL中的in,下面的示例等同于SQL中的in ("stephen","stephen1")
> db.test.find({"name":{"$in":["stephen","stephen1"]}})
{ "_id" : ObjectId("4fd58ecbb9ac507e96276f1a"), "name" : "stephen", "age" : 35,"genda" : "male", "email" : "stephen@hotmail.com" }
--和SQL不同的是,MongoDB的in list中的数据可以是不同类型。这种情况可用于不同类型的别名场景。
> db.test.find({"name":{"$in":["stephen",123]}})
{ "_id" : ObjectId("4fd58ecbb9ac507e96276f1a"), "name" : "stephen", "age" : 35,"genda" : "male", "email" : "stephen@hotmail.com" }
--$nin等同于SQL中的not in,同时也是$in的取反。如:
> db.test.find({"name":{"$nin":["stephen2","stephen1"]}})
{ "_id" : ObjectId("4fd58ecbb9ac507e96276f1a"), "name" : "stephen", "age" : 35,"genda" : "male", "email" : "stephen@hotmail.com" }
--$or等同于SQL中的or,$or所针对的条件被放到一个数组中,每个数组元素表示or的一个条件。
--下面的示例等同于name = "stephen1" or age = 35
> db.test.find({"$or": [{"name":"stephen1"}, {"age":35}]})
{ "_id" : ObjectId("4fd58ecbb9ac507e96276f1a"), "name" : "stephen", "age" : 35,"genda" : "male", "email" : "stephen@hotmail.com" }
--下面的示例演示了如何混合使用$or和$in。
> db.test.find({"$or": [{"name":{"$in":["stephen","stephen1"]}}, {"age":36}]})
{ "_id" : ObjectId("4fd58ecbb9ac507e96276f1a"), "name" : "stephen", "age" : 35,"genda" : "male", "email" : "stephen@hotmail.com" }
--$not表示取反,等同于SQL中的not。
> db.test.find({"name": {"$not": {"$in":["stephen2","stephen1"]}}})
{ "_id" : ObjectId("4fd58ecbb9ac507e96276f1a"), "name" : "stephen", "age" : 35,"genda" : "male", "email" : "stephen@hotmail.com" }