MongoDB query to retrieve records from a collection named with letters and numbers


At first, let us create a collection with letters and numbers, for example −

7664734-541d-r5i5f-845575e-ghfhjrjr3747_demo368

Access the above collection using db.getCollection(). Let us now create a collection with the name mentioned above −

> db.createCollection('7664734-541d-r5i5f-845575e-ghfhjrjr3747_demo368');
{ "ok" : 1 }
>db.getCollection('7664734-541d-r5i5f-845575e-ghfhjrjr3747_demo368').insertOne({"FirstName":"Chris"});
{
   "acknowledged" : true,
   "insertedId" : ObjectId("5e57e2152ae06a1609a00aea")
}
>db.getCollection('7664734-541d-r5i5f-845575e-ghfhjrjr3747_demo368').insertOne({"FirstName":"David"});
{
   "acknowledged" : true,
   "insertedId" : ObjectId("5e57e21a2ae06a1609a00aeb")
}
>db.getCollection('7664734-541d-r5i5f-845575e-ghfhjrjr3747_demo368').insertOne({"FirstName":"Bob"});
{
   "acknowledged" : true,
   "insertedId" : ObjectId("5e57e21e2ae06a1609a00aec")
}

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

> db.getCollection('7664734-541d-r5i5f-845575e-ghfhjrjr3747_demo368').find();

This will produce the following output −

{ "_id" : ObjectId("5e57e2152ae06a1609a00aea"), "FirstName" : "Chris" }
{ "_id" : ObjectId("5e57e21a2ae06a1609a00aeb"), "FirstName" : "David" }
{ "_id" : ObjectId("5e57e21e2ae06a1609a00aec"), "FirstName" : "Bob" }

Updated on: 02-Apr-2020

145 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements