MongoDB 复合条件获取文档?
让我们创建一个包含文档的集合 −
> 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" }
广告