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

การค้นหาค่าที่มากกว่าหรือเท่ากับค่าที่กำหนดใน MongoDB


เพื่อค้นหาค่าที่มากกว่าหรือเท่ากับค่าที่กำหนด ไวยากรณ์คือการใช้ $gte ใน MongoDB ดังนี้

db.yourCollectionName.find({yourFieldName:{$gte:yourValue}});

มาสร้างคอลเล็กชันกันเถอะ

> db.demo571.insertOne({"Price":140});
{ "acknowledged" : true, "insertedId" : ObjectId("5e909b3439cfeaaf0b97b587") }
> db.demo571.insertOne({"Price":100});
{ "acknowledged" : true, "insertedId" : ObjectId("5e909b3639cfeaaf0b97b588") }
> db.demo571.insertOne({"Price":110});
{ "acknowledged" : true, "insertedId" : ObjectId("5e909b3839cfeaaf0b97b589") }
> db.demo571.insertOne({"Price":240});
{ "acknowledged" : true, "insertedId" : ObjectId("5e909b3c39cfeaaf0b97b58a") }

แสดงเอกสารทั้งหมดด้วยเมธอด find()

> db.demo571.find();

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

{ "_id" : ObjectId("5e909b3439cfeaaf0b97b587"), "Price" : 140 }
{ "_id" : ObjectId("5e909b3639cfeaaf0b97b588"), "Price" : 100 }
{ "_id" : ObjectId("5e909b3839cfeaaf0b97b589"), "Price" : 110 }
{ "_id" : ObjectId("5e909b3c39cfeaaf0b97b58a"), "Price" : 240 }

คำสั่งดึงเอกสารที่มีราคามากกว่าหรือเท่ากับ 140

> db.demo571.find({Price:{$gte:140}});

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

{ "_id" : ObjectId("5e909b3439cfeaaf0b97b587"), "Price" : 140 }
{ "_id" : ObjectId("5e909b3c39cfeaaf0b97b58a"), "Price" : 240 }