让我们创建一个包含文档的集合-
> db.demo714.insertOne({FirstName:"Chris",LastName:"Brown"}); { "acknowledged" : true, "insertedId" : ObjectId("5ea9a2da85324c2c98cc4c2b") } > db.demo714.insertOne({FirstName:"Adam",LastName:"Smith"}); { "acknowledged" : true, "insertedId" : ObjectId("5ea9a2e585324c2c98cc4c2c") } > db.demo714.insertOne({FirstName:"David",LastName:"Miller"}); { "acknowledged" : true, "insertedId" : ObjectId("5ea9a2ed85324c2c98cc4c2d") } > db.demo714.insertOne({FirstName:"John",LastName:"Doe"}); { "acknowledged" : true, "insertedId" : ObjectId("5ea9a2f785324c2c98cc4c2e") }
在find()
方法的帮助下显示集合中的所有文档-
> db.demo714.find();
这将产生以下输出-
{ "_id" : ObjectId("5ea9a2da85324c2c98cc4c2b"), "FirstName" : "Chris", "LastName" : "Brown" } { "_id" : ObjectId("5ea9a2e585324c2c98cc4c2c"), "FirstName" : "Adam", "LastName" : "Smith" } { "_id" : ObjectId("5ea9a2ed85324c2c98cc4c2d"), "FirstName" : "David", "LastName" : "Miller" } { "_id" : ObjectId("5ea9a2f785324c2c98cc4c2e"), "FirstName" : "John", "LastName" : "Doe" }
这是对复合条件的查询-
> db.demo714.find({"LastName":{$in:["Miller","Brown"]}});
这将产生以下输出-
{ "_id" : ObjectId("5ea9a2da85324c2c98cc4c2b"), "FirstName" : "Chris", "LastName" : "Brown" } { "_id" : ObjectId("5ea9a2ed85324c2c98cc4c2d"), "FirstName" : "David", "LastName" : "Miller" }