MongoDB: How to query a collection named "version"?

To query a collection named "version" in MongoDB, use the getCollection() method with the collection name as a string parameter. This is necessary because "version" is a reserved word in MongoDB shell.

Syntax

db.getCollection("version").find();
db.getCollection("version").insertOne({document});

Create the Collection

First, create a collection named "version" ?

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

Insert Sample Data

Insert multiple version documents using insertMany() ?

db.getCollection("version").insertMany([
    {"VersionName": "1.0"},
    {"VersionName": "1.1"},
    {"VersionName": "1.2"}
]);
{
    "acknowledged" : true,
    "insertedIds" : [
        ObjectId("5e100b47d7df943a7cec4fae"),
        ObjectId("5e100b49d7df943a7cec4faf"),
        ObjectId("5e100b4cd7df943a7cec4fb0")
    ]
}

Query the Collection

Display all documents from the "version" collection using find() ?

db.getCollection("version").find();
{ "_id" : ObjectId("5e100b47d7df943a7cec4fae"), "VersionName" : "1.0" }
{ "_id" : ObjectId("5e100b49d7df943a7cec4faf"), "VersionName" : "1.1" }
{ "_id" : ObjectId("5e100b4cd7df943a7cec4fb0"), "VersionName" : "1.2" }

Key Points

  • Use getCollection() method when the collection name is a reserved word in MongoDB.
  • Always enclose the collection name in quotes when using getCollection().
  • All standard MongoDB operations work with getCollection() syntax.

Conclusion

The getCollection() method enables querying collections with reserved names like "version". This approach ensures proper access to collections that would otherwise conflict with MongoDB shell keywords.

Updated on: 2026-03-15T02:26:58+05:30

183 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements