Display MongoDB with document and subdocument example and update

MongoDB allows storing documents with subdocuments using nested arrays and objects. This structure enables complex data relationships within a single document, making it ideal for hierarchical data.

Syntax

db.collectionName.insertOne({
    fieldName: "value",
    subdocumentArray: [
        {
            nestedField1: "value1",
            nestedField2: "value2"
        }
    ]
});

Sample Data

Let's create a collection with documents containing subdocuments ?

db.demo706.insertMany([
    {
        PortalName: "GameApplication",
        ApplicationConfiguration: [
            {
                "URL": "jdbc:mysql://localhost/customer_tracker?autoReconnect=true",
                "USERNAME": "root",
                "PASSWORD": "root"
            }
        ]
    },
    {
        PortalName: "WebMyBusinessApplication",
        ApplicationConfiguration: [
            {
                "URL": "jdbc:oracle:thin:@localhost:1521:xe",
                "USERNAME": "App",
                "PASSWORD": "App"
            }
        ]
    }
]);
{
    "acknowledged": true,
    "insertedIds": [
        ObjectId("5ea6f557551299a9f98c93c6"),
        ObjectId("5ea6f558551299a9f98c93c7")
    ]
}

Display Documents

View all documents using the find() method ?

db.demo706.find();
{
    "_id": ObjectId("5ea6f557551299a9f98c93c6"),
    "PortalName": "GameApplication",
    "ApplicationConfiguration": [
        {
            "URL": "jdbc:mysql://localhost/customer_tracker?autoReconnect=true",
            "USERNAME": "root",
            "PASSWORD": "root"
        }
    ]
}
{
    "_id": ObjectId("5ea6f558551299a9f98c93c7"),
    "PortalName": "WebMyBusinessApplication",
    "ApplicationConfiguration": [
        {
            "URL": "jdbc:oracle:thin:@localhost:1521:xe",
            "USERNAME": "App",
            "PASSWORD": "App"
        }
    ]
}

Update Document

Update the PortalName field using $set operator ?

db.demo706.updateOne(
    { PortalName: "WebMyBusinessApplication" },
    { $set: { "PortalName": "OnlineCustomerTracker" } }
);
{
    "acknowledged": true,
    "matchedCount": 1,
    "modifiedCount": 1
}

Verify Update

Check the updated documents ?

db.demo706.find();
{
    "_id": ObjectId("5ea6f557551299a9f98c93c6"),
    "PortalName": "GameApplication",
    "ApplicationConfiguration": [
        {
            "URL": "jdbc:mysql://localhost/customer_tracker?autoReconnect=true",
            "USERNAME": "root",
            "PASSWORD": "root"
        }
    ]
}
{
    "_id": ObjectId("5ea6f558551299a9f98c93c7"),
    "PortalName": "OnlineCustomerTracker",
    "ApplicationConfiguration": [
        {
            "URL": "jdbc:oracle:thin:@localhost:1521:xe",
            "USERNAME": "App",
            "PASSWORD": "App"
        }
    ]
}

Conclusion

MongoDB's document and subdocument structure provides flexibility for storing complex data. Use insertOne() or insertMany() for creating nested documents, and updateOne() with $set for modifying specific fields while preserving the subdocument structure.

Updated on: 2026-03-15T03:40:13+05:30

387 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements