Sunday 6 June 2021

MongoDB: Insert an array of entities

In this post, I am going to explain how to add arrays of data to a document.

 

Let’s take an employee collection as example.

> db.employee.find().pretty()
{
    "_id" : ObjectId("60bcd0dd8251689f64fb3eee"),
    "id" : 1,
    "firstName" : "Joel",
    "lastName" : "chelli"
}
{
    "_id" : ObjectId("60bcd0dd8251689f64fb3eef"),
    "id" : 2,
    "firstName" : "Ananad",
    "lastName" : "Bandaru"
}
{
    "_id" : ObjectId("60bcd0dd8251689f64fb3ef0"),
    "id" : 3,
    "firstName" : "Gopi",
    "lastName" : "Battu"
}
{
    "_id" : ObjectId("60bcd0dd8251689f64fb3ef1"),
    "id" : 4,
    "firstName" : "Ritwik",
    "lastName" : "Mohenthy"
}

 

Let’s add hobbies to the employee Ritwik.

db.employee.updateOne({"firstName" : "Ritwik"}, {"$set" : {"hobbies" : ["cooking", "trekking", "listening to music"]}})    
> db.employee.find().pretty()
{
    "_id" : ObjectId("60bcd0dd8251689f64fb3eee"),
    "id" : 1,
    "firstName" : "Joel",
    "lastName" : "chelli"
}
{
    "_id" : ObjectId("60bcd0dd8251689f64fb3eef"),
    "id" : 2,
    "firstName" : "Ananad",
    "lastName" : "Bandaru"
}
{
    "_id" : ObjectId("60bcd0dd8251689f64fb3ef0"),
    "id" : 3,
    "firstName" : "Gopi",
    "lastName" : "Battu"
}
{
    "_id" : ObjectId("60bcd0dd8251689f64fb3ef1"),
    "id" : 4,
    "firstName" : "Ritwik",
    "lastName" : "Mohenthy",
    "hobbies" : [
        "cooking",
        "trekking",
        "listening to music"
    ]
}

 

Let’s add project details to the employee Gopi.

db.employee.updateOne({"firstName" : "Gopi"}, {"$set" : {"projects" : [
    {"projectId" : "ind_cpcs", "name" : "cpcs", "description" : "cabin pressure control system"},
    {"projectId" : "ind_veh_tracker", "name" : "vehicle tracker", "description" : "Track the vehicle"}
]}})

 

> db.employee.updateOne({"firstName" : "Gopi"}, {"$set" : {"projects" : [
...     {"projectId" : "ind_cpcs", "name" : "cpcs", "description" : "cabin pressure control system"},
...     {"projectId" : "ind_veh_tracker", "name" : "vehicle tracker", "description" : "Track the vehicle"}
... ]}})
{ "acknowledged" : true, "matchedCount" : 1, "modifiedCount" : 1 }
> 
> db.employee.find().pretty()
{
    "_id" : ObjectId("60bcd0dd8251689f64fb3eee"),
    "id" : 1,
    "firstName" : "Joel",
    "lastName" : "chelli"
}
{
    "_id" : ObjectId("60bcd0dd8251689f64fb3eef"),
    "id" : 2,
    "firstName" : "Ananad",
    "lastName" : "Bandaru"
}
{
    "_id" : ObjectId("60bcd0dd8251689f64fb3ef0"),
    "id" : 3,
    "firstName" : "Gopi",
    "lastName" : "Battu",
    "projects" : [
        {
            "projectId" : "ind_cpcs",
            "name" : "cpcs",
            "description" : "cabin pressure control system"
        },
        {
            "projectId" : "ind_veh_tracker",
            "name" : "vehicle tracker",
            "description" : "Track the vehicle"
        }
    ]
}
{
    "_id" : ObjectId("60bcd0dd8251689f64fb3ef1"),
    "id" : 4,
    "firstName" : "Ritwik",
    "lastName" : "Mohenthy",
    "hobbies" : [
        "cooking",
        "trekking",
        "listening to music"
    ]
}

 

 

 

Previous                                                    Next                                                    Home

No comments:

Post a Comment