Add New Field in MongoDB Document Array

Have a Database Problem? Speak with an Expert for Free
Get Started >>

Introduction

MongoDB is a NoSQL database. That means there are no tables in MongoDB. MongoDB stores record as BSON documents. The binary representation of JSON is called BSON. It contains more data types than JSON. One of these data types provided by BSON is an array. An array can contain any set of values. We can store string, integer, boolean, objects, and many more in arrays. In this article, I will explain how to add a new field in a MongoDB document array.

The Demo – Adding A New Field to a MongoDB Document Array

Suppose we have database which contain data of a few agents working for a company in New York. It consists of their names, age, city and an array of objects that contains the connections of that member. I have already created a database named members and a collection within it named details. In the details collection, there are three documents containing details about John, Peter, and Sam, all being members. Lets see what are the details of John.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
> db.details.find({"name":"John"},{}).pretty()
{
"_id" : ObjectId("5d375a4199dae11e17f28a0d"),
"name" : "John",
"age" : 21,
"city" : "New York",
"connections" : [
{
"name" : "William",
"age" : 22,
"city" : "Detroit"
},
{
"name" : "Lisa",
"age" : 23,
"city" : "California"
}
]
}
>

Observe the above code. There is a field named connections which is an array of objects. Each object holds the details about each connection. John has two connections. But let’s say John has got made a new connection whose details are as follows:

1
2
3
4
5
{
"name" : "Jim",
"age" : 29,
"city: : "Los Angeles"
}

How could we insert this object to the connections array? The answer is by using update operation and $push operator. To insert this new connection into John’s document, we do the following:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
> var newConnection = {name: "Jim", age: 29, city: "Los Angeles"}
> db.details.update({name:"John"},{$push:{"connections" : newConnection}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.details.find({"name":"John"},{}).pretty()
{
"_id" : ObjectId("5d375a4199dae11e17f28a0d"),
"name" : "John",
"age" : 21,
"city" : "New York",
"connections" : [
{
"name" : "William",
"age" : 22,
"city" : "Detroit"
},
{
"name" : "Lisa",
"age" : 23,
"city" : "California"
},
{
"name" : "Jim",
"age" : 29,
"city" : "Los Angeles"
}
]
}
>

In the above result, we can see a new connection is added to John’s connection array. The update operation is used for this. The update operation has two parameters enclosed in curly brackets.

The first parameter is {“name” : “John”}. It specifies the condition of the query. I wanted to update John’s document, so I used his name as a condition.

The second parameter is {$push:{“connections” : newConnection}}. $push is an operator that is used to push data inside the array. In my query, I pushed newConnection into the connections array of John’s document. The newConnection variable holds the data of the new connection which is to be added inside John’s connections array (Check the first line of the above terminal commands).

Now John has three connections. What if there are multiple new connections for John? We use the $each operator inside the $push operator. Suppose John has following two connections.

1
2
3
4
5
6
7
8
9
10
{
"name" : "Randy",
"age" : 31,
"city: : "Texas"
},
{
"
name" : "Mark",
"
age" : 26,
"
city: : "Chicago"
}

I will use the following commands to insert these new connections into the John’s connections array.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
> var newConnection1 = {name: "Randy", age: 31, city: "Texas"}
> var newConnection2 = {name: "Mark", age:26, city: "Chicago"}
> db.details.update({},{$push: {"connections": {$each: [newConnection1, newConnection2]}}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.details.find({"name":"John"},{}).pretty()
{
"_id" : ObjectId("5d3762a799dae11e17f28a10"),
"name" : "John",
"age" : 21,
"city" : "New York",
"connections" : [
{
"name" : "William",
"age" : 22,
"city" : "Detroit"
},
{
"name" : "Lisa",
"age" : 23,
"city" : "California"
},
{
"name" : "Jim",
"age" : 29,
"city" : "Los Angeles"
},
{
"name" : "Randy",
"age" : 31,
"city" : "Texas"
},
{
"name" : "Mark",
"age" : 26,
"city" : "Chicago"
}
]
}
>

In the above result, I used the $each opertaor to add two new connections(newConnection1 and newConnection2) inside John’s connection array. The argument to the $each operator is an array of all new data we want to insert.

Conclusion

So, Adding a new field to an array is possible by using the update operation. $push operator is used to insert the field inside the array and if we want to add multiple fields, use $each inside $push.

Pilot the ObjectRocket Platform Free!

Try Fully-Managed CockroachDB, Elasticsearch, MongoDB, PostgreSQL (Beta) or Redis.

Get Started

Keep in the know!

Subscribe to our emails and we’ll let you know what’s going on at ObjectRocket. We hate spam and make it easy to unsubscribe.