Computer >> Computer tutorials >  >> Programming >> MongoDB

What does the max field mean in the output of MongoDB db.<collectionname>.stats( )?


Use max field in order to limit the number of documents in the collection. Following is the query to use the max field in the capped collection −

> db.createCollection("demo673", { capped : true, size : 100, max :50 } )
{ "ok" : 1 }

Let us create a collection with documents −

> 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")
}

Display all documents from a collection with the help of find() method −

> db.demo673.find();

This will produce the following output −

{ "_id" : ObjectId("5ea3ec7304263e90dac943e8"), "Name" : "John", "Age" : 23 }
{ "_id" : ObjectId("5ea3ec7804263e90dac943e9"), "Name" : "Bob", "Age" : 21 }
{ "_id" : ObjectId("5ea3ec7f04263e90dac943ea"), "Name" : "David", "Age" : 20 }