限制 MongoDB 中集合中的文件數量?


若要限制集合中的文件數量,請將 capped − true。其本身設定大小。我們建立一個包含文件的集合 −

> db.createCollection( "demo683", { capped: true, size: 5 ,max:4} )
{ "ok" : 1 }
> db.demo683.insertOne({Value:100});
{
   "acknowledged" : true,
   "insertedId" : ObjectId("5ea468afa7e81adc6a0b394e")
}
> db.demo683.insertOne({Value:500});
{
   "acknowledged" : true,
   "insertedId" : ObjectId("5ea468b0a7e81adc6a0b394f")
}
> db.demo683.insertOne({Value:1000});
{
   "acknowledged" : true,
   "insertedId" : ObjectId("5ea468b1a7e81adc6a0b3950")
}
> db.demo683.insertOne({Value:400});
{
   "acknowledged" : true,
   "insertedId" : ObjectId("5ea468b2a7e81adc6a0b3951")
}
> db.demo683.insertOne({Value:800});
{
   "acknowledged" : true,
   "insertedId" : ObjectId("5ea468b3a7e81adc6a0b3952")
}
> db.demo683.insertOne({Value:700});
{
   "acknowledged" : true,
   "insertedId" : ObjectId("5ea468b4a7e81adc6a0b3953")
}

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

> db.demo683.find();

這將產生以下輸出 −

{ "_id" : ObjectId("5ea468b1a7e81adc6a0b3950"), "Value" : 1000 }
{ "_id" : ObjectId("5ea468b2a7e81adc6a0b3951"), "Value" : 400 }
{ "_id" : ObjectId("5ea468b3a7e81adc6a0b3952"), "Value" : 800 }
{ "_id" : ObjectId("5ea468b4a7e81adc6a0b3953"), "Value" : 700 }

更新於: 2020 年 5 月 13 日

393 次瀏覽

開啟你的 事業

完成課程,獲得認證

開始學習
廣告
© . All rights reserved.