Syntax error Can we use the “.” symbol in MongoDB collection name?

Can we use the “.” symbol in MongoDB collection name?



Yes, we can use the “.” symbol in MongoDB collection name. Let us create a collection with documents −

> db.getCollection('demo28.example');
web.demo28.example
>
>
> db.getCollection('demo28.example').insertOne({"Name":"Chris","Age":32});
{
   "acknowledged" : true,
   "insertedId" : ObjectId("5e15fbe48f2315c2efc48e6b")
}
> db.getCollection('demo28.example').insertOne({"Name":"Bob","Age":31});
{
   "acknowledged" : true,
   "insertedId" : ObjectId("5e15fbec8f2315c2efc48e6c")
}
> db.getCollection('demo28.example').insertOne({"Name":"David","Age":33});
{
   "acknowledged" : true,
   "insertedId" : ObjectId("5e15fbf38f2315c2efc48e6d")
}

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

> db.getCollection('demo28.example').find().pretty();

This will produce the following output −

{
   "_id" : ObjectId("5e15fbe48f2315c2efc48e6b"),
   "Name" : "Chris",
   "Age" : 32
}
{ "_id" : ObjectId("5e15fbec8f2315c2efc48e6c"), "Name" : "Bob", "Age" : 31 }
{
   "_id" : ObjectId("5e15fbf38f2315c2efc48e6d"),
   "Name" : "David",
   "Age" : 33
}
Updated on: 2020-04-02T12:30:08+05:30

253 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements