If a collection does not exist then MongoDB creates a collection in indexing part. The createdCollectionAutomatically tells that the operation created a collection.
For our example, let us create a collection with an index −
> db.createCollectionDemo.createIndex({"ClientCountryName":1});
This will produce the following output −
{ "createdCollectionAutomatically" : true, "numIndexesBefore" : 1, "numIndexesAfter" : 2, "ok" : 1 }
Let us create a collection with documents −
> db.createCollectionDemo.insertOne({"ClientName":"Larry","ClientCountryName":"US"}); { "acknowledged" : true, "insertedId" : ObjectId("5cd2950be3526dbddbbfb612") }
Following is the query to display all documents from a collection with the help of find() method −
> db.createCollectionDemo.find().pretty();
This will produce the following output −
{ "_id" : ObjectId("5cd2950be3526dbddbbfb612"), "ClientName" : "Larry", "ClientCountryName" : "US" }