使用最大字段为了限制收集中的文档数。以下是使用上限集合中的max字段的查询-
> db.createCollection("demo673", { capped : true, size : 100, max :50 } ) { "ok" : 1 }
让我们创建一个包含文档的集合-
> db.demo673.insertOne({Name:"John",Age:23}); { "acknowledged" : true, "insertedId" : ObjectId("5ea3ec7304263e90dac943e8") } > db.demo673.insertOne({Name:"Bob",Age:21}); { "acknowledged" : true, "insertedId" : ObjectId("5ea3ec7804263e90dac943e9") } > db.demo673.insertOne({Name:"David",Age:20}); { "acknowledged" : true, "insertedId" : ObjectId("5ea3ec7f04263e90dac943ea") }
在find()方法的帮助下显示集合中的所有文档-
> db.demo673.find();
这将产生以下输出-
{ "_id" : ObjectId("5ea3ec7304263e90dac943e8"), "Name" : "John", "Age" : 23 } { "_id" : ObjectId("5ea3ec7804263e90dac943e9"), "Name" : "Bob", "Age" : 21 } { "_id" : ObjectId("5ea3ec7f04263e90dac943ea"), "Name" : "David", "Age" : 20 }