計算以 13 開頭的欄位值的檔案


要統計文件,請使用 $count。對於以 13 開頭的值,請使用 $regex。您可以使用 $regex。我們來建立一個具有文件的集合 −

> db.demo570.insertOne({Information:{Value:"13675"}});{
   "acknowledged" : true, "insertedId" : ObjectId("5e90959b39cfeaaf0b97b583")
}
> db.demo570.insertOne({Information:{Value:"14135"}});{
   "acknowledged" : true, "insertedId" : ObjectId("5e9095a739cfeaaf0b97b584")
}
> db.demo570.insertOne({Information:{Value:"15113"}});{
   "acknowledged" : true, "insertedId" : ObjectId("5e9095b639cfeaaf0b97b585")
}
> db.demo570.insertOne({Information:{Value:"13141"}});{
   "acknowledged" : true, "insertedId" : ObjectId("5e9095c139cfeaaf0b97b586")
}

藉助 find() 方法,從集合中顯示所有文件 −

> db.demo570.find();

這將產生以下輸出 −

{ "_id" : ObjectId("5e90959b39cfeaaf0b97b583"), "Information" : { "Value" : "13675" } }
{ "_id" : ObjectId("5e9095a739cfeaaf0b97b584"), "Information" : { "Value" : "14135" } }
{ "_id" : ObjectId("5e9095b639cfeaaf0b97b585"), "Information" : { "Value" : "15113" } }
{ "_id" : ObjectId("5e9095c139cfeaaf0b97b586"), "Information" : { "Value" : "13141" } }

以下是用於提高使用索引的效能的查詢 −

> db.demo570.count({'Information.Value': { '$regex' : '^13' }});

這將產生以下輸出 −

2

更新於: 2020 年 5 月 14 日

74 次瀏覽

開啟你的 職業生涯

完成該課程以獲得認證

開始
廣告
© . All rights reserved.