Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> MongoDB

คำสั่ง MongoDB หาเอกสารที่ Billing Address เท่ากับ Delivery Address


เพื่อตรวจสอบความเท่าเทียมและดึงเอกสาร ให้ใช้ $where ใน MongoDB มาสร้างคอลเล็กชันกันเถอะ

> 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" }

คำสั่งตรวจสอบ where Billing Address เท่ากับ Delivery Address

> db.demo589.find( { $where: "this.deliveryAddress == this.billingAddress" } );

ผลลัพธ์ที่ได้

{ "_id" : ObjectId("5e92c11bfd2d90c177b5bccd"), "deliveryAddress" : "US", "billingAddress" : "US" }