MongoDB: How to query a collection named “version”?


For this, use the concept of createCollection() and getCollection(). Following is the query to create a collection with name ‘version’ −

> db.createCollection('version');
{ "ok" : 1 }

Let us first create a collection with documents −

> db.getCollection('version').insertOne({"VersionName":"1.0"});
{
   "acknowledged" : true,
   "insertedId" : ObjectId("5e100b47d7df943a7cec4fae")
}
> db.getCollection('version').insertOne({"VersionName":"1.1"});
{
   "acknowledged" : true,
   "insertedId" : ObjectId("5e100b49d7df943a7cec4faf")
}
> db.getCollection('version').insertOne({"VersionName":"1.2"});
{
   "acknowledged" : true,
   "insertedId" : ObjectId("5e100b4cd7df943a7cec4fb0")
}

Following is the query to display all documents from a collection with the help of find() method −

> db.getCollection('version').find();

This will produce the following output −

{ "_id" : ObjectId("5e100b47d7df943a7cec4fae"), "VersionName" : "1.0" }
{ "_id" : ObjectId("5e100b49d7df943a7cec4faf"), "VersionName" : "1.1" }
{ "_id" : ObjectId("5e100b4cd7df943a7cec4fb0"), "VersionName" : "1.2" }

Updated on: 01-Apr-2020

93 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements