Query MongoDB collection starting with _?

MongoDBDatabaseBig Data Analytics

For MongoDB collection beginning with_, following is the syntax −

db.createCollection(‘_yourCollectionName’);

Insert query using below syntax −

db.getCollection('_yourCollectionName').insertOne({"yourFieldName1":"yourValue1","yourFieldName2":yourValue2,............N});

Let us first create a collection with documents −

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

>db.getCollection('_testUnderscoreCollectionDemo').insertOne({"StudentFirstName":"John","StudentAge":23});
{
   "acknowledged" : true,
   "insertedId" : ObjectId("5ccfb4a6140b992277dae0e4")
}

>db.getCollection('_testUnderscoreCollectionDemo').insertOne({"StudentFirstName":"Carol","StudentAge":21});
{
   "acknowledged" : true,
   "insertedId" : ObjectId("5ccfb4af140b992277dae0e5")
}

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

> db.getCollection('_testUnderscoreCollectionDemo').find().pretty();

This will produce the following output −

{
   "_id" : ObjectId("5ccfb4a6140b992277dae0e4"),
   "StudentFirstName" : "John",
   "StudentAge" : 23
}
{
   "_id" : ObjectId("5ccfb4af140b992277dae0e5"),
   "StudentFirstName" : "Carol",
   "StudentAge" : 21
}
raja
Published on 22-May-2019 11:54:48
Advertisements