在mongodb集合中查詢欄位等於給定整數值的文件?


要查詢其中一個欄位等於給定整數的文件,請使用find()。我們使用文件建立一個集合−

> db.demo472.insertOne({"Project_Id":-101,"ProjectName":"Online Customer Tracking"});{
   "acknowledged" : true,
   "insertedId" : ObjectId("5e80586cb0f3fa88e227907a")
}
> db.demo472.insertOne({"Project_Id":101,"ProjectName":"Online Banking System"});{
   "acknowledged" : true,
   "insertedId" : ObjectId("5e805884b0f3fa88e227907b")
}
> db.demo472.insertOne({"Project_Id":102,"ProjectName":"Online Library System"});{
   "acknowledged" : true,
   "insertedId" : ObjectId("5e805893b0f3fa88e227907c")
}

使用find()方法從集合中顯示所有文件−

> db.demo472.find();

這將產生以下輸出−

{ "_id" : ObjectId("5e80586cb0f3fa88e227907a"), "Project_Id" : -101, "ProjectName" : "Online
Customer Tracking" }
{ "_id" : ObjectId("5e805884b0f3fa88e227907b"), "Project_Id" : 101, "ProjectName" : "Online
Banking System" }
{ "_id" : ObjectId("5e805893b0f3fa88e227907c"), "Project_Id" : 102, "ProjectName" : "Online
Library System" }

以下是查詢MongoDB集合中其中一個欄位等於給定整數的文件的查詢−

> db.getCollection('demo472').find({"Project_Id":-101});

這將產生以下輸出−

{ "_id" : ObjectId("5e80586cb0f3fa88e227907a"), "Project_Id" : -101, "ProjectName" : "Online
Customer Tracking" }

更新於:11-May-2020

227瀏覽

開啟你的職業

透過完成課程獲得認證

開始
廣告
© . All rights reserved.