在MongoDB中查询找到“发货”地址等于发货地址的文档
若要检查相等性并获取文档,请使用MongoDB中的$where。我们使用文档创建一个集合−
> db.demo589.insertOne({deliveryAddress:"US",billingAddress:"UK"});{ "acknowledged" : true, "insertedId" : ObjectId("5e92c117fd2d90c177b5bccc") } > db.demo589.insertOne({deliveryAddress:"US",billingAddress:"US"});{ "acknowledged" : true, "insertedId" : ObjectId("5e92c11bfd2d90c177b5bccd") } > db.demo589.insertOne({deliveryAddress:"US",billingAddress:"AUS"});{ "acknowledged" : true, "insertedId" : ObjectId("5e92c11ffd2d90c177b5bcce") } > db.demo589.insertOne({deliveryAddress:"UK",billingAddress:"US"});{ "acknowledged" : true, "insertedId" : ObjectId("5e92c127fd2d90c177b5bccf") }
使用find()方法从集合中显示所有文档−
> db.demo589.find();
这将产生以下输出−
{ "_id" : ObjectId("5e92c117fd2d90c177b5bccc"), "deliveryAddress" : "US", "billingAddress" : "UK" } { "_id" : ObjectId("5e92c11bfd2d90c177b5bccd"), "deliveryAddress" : "US", "billingAddress" : "US" } { "_id" : ObjectId("5e92c11ffd2d90c177b5bcce"), "deliveryAddress" : "US", "billingAddress" : "AUS" } { "_id" : ObjectId("5e92c127fd2d90c177b5bccf"), "deliveryAddress" : "UK", "billingAddress" : "US" }
以下是查询“发货”地址等于收货地址并获取文档的方法−
> db.demo589.find( { $where: "this.deliveryAddress == this.billingAddress" } );
这将产生以下输出−
{ "_id" : ObjectId("5e92c11bfd2d90c177b5bccd"), "deliveryAddress" : "US", "billingAddress" : "US" }
广告