World is huMONGOous and so is the amount of data that we possess in it.

The above line explains where did the word ‘Mongo’ originate from and why do we need MongoDB.  MongoDB is a cross-platform document-oriented database. Document databases pair each key with a complex data structure known as a document. Documents can contain many different key-value pairs, or key-array pairs, or even nested documents.

Known as a No-SQL database, it does not have traditional table structures and supports JSON-like data structure which is easy to read and understand. Schema need not be defined before inserting data. This is helpful in agile ways of development where there are frequent iterations in development, where data changes its form, data types are polymorphic and the volume of data is massive.

Challenges with massive data and MongoDB 

How do we make a minor change in the document in the database? How do we merge or replace new data into an existing document? Programatically, should we fetch the whole document into memory and traverse to the key at which that value needs to be changed? Isn’t it expensive?

Consider a database containing network interfaces of each system in the organization, relatively a smaller example for the ease of understanding:

MDB Image 1

If one of the network interfaces changes in a system, we intend to update this information in our database. The simplest way to do this is replace the entire document if we maintain a list of all existing interfaces. Often this is not the case.

When a new network interface is added or an existing network interface is modified, a system event occurs with the details of that network interface.  Typically, systems would request to update this information into the server. Additionally, we would need to handle similar requests from multiple machines in parallel to update their information.

Solution

To add or modify an existing document in MongoDB, we use update command.

Adding an item in an array

In an existing document in a collection, you could use $push to add a new network interface. Instead of $push, $addToSet can also be used, however we will see usage $push in the upcoming examples.

{ $push: { <field1>: <value1>, … } }

Example:

In an existing document in a collection say systeminfo,

db.systeminfo.update({“system-information.host_name”:”secpod-desktop”},{“$push”:{“system-information.interfaces.interface”:{“interface_name” : “VMware Virtual Ethernet Adapter for VMnet2”, “ip_address” : “192.168.88.2”, “mac_address” : “EE-EE-EE-EE-EE-EE”}}});

As a result, a new network interface gets added in the array of interfaces,

MDB Image 2

Removing an existing item in an array

In an existing document in a collection, you could use $pull to remove a new network interface.

{ $pull: { <field1>: <value1>, … } }

Example:

In an existing document in a collection say systeminfo,

db.systeminfo.update({“system-information.host_name”:”secpod-desktop “},{“$pull”:{“system-information.interfaces.interface”:{“interface_name” : ” Intel(R) 82574L Gigabit Network Connection”}}});

MDB Image 3

Modifying an existing item in an array

In an existing document in a collection, you could use a combination of $pull and $push to modify a new network interface.

{ $pull: { <field1>: <value1>, … } }

{ $push: { <field1>: <value1>, … } }

Example:

Considering the prior example, in an existing document in a collection say systeminfo, run the following commands

db.systeminfo.update({“system-information.host_name”:”secpod-desktop”},{“$pull”:{“system-information.interfaces.interface”:{“interface_name” : ” Intel(R) 82574L Gigabit Network Connection”}}});

db.systeminfo.update({“system-information.host_name”:”secpod-desktop “},{“$push”:{“system-information.interfaces.interface”:{“interface_name” : ” Intel(R) 82574L Gigabit Network Connection”, “ip_address” : “192.168.1.30”, “mac_address” : “DD-DD-DD-DD-DD-DD”}}});

As a result, the network interface gets modified in the array of interfaces,

MDB Image 4

Such modifications especially aid in cases where IPs are assigned dynamically and a change in database is required from time-to-time.

Summarizing, the above examples avoids the constraints and frustration of modifying data objects in memory. In a scenario of documents containing nested sub-documents or arrays, such as system details containing a huge set of file data, installed patches and applications, processes etc., we might not be able to handle huge data in memory. Hope this helps!

Preeti Subramanian

spreeti@secpod.com

Summary
MongoDB – Merging Data into an Existing Document
Article Name
MongoDB – Merging Data into an Existing Document
Author
Publisher Name
SecPod Technologies
Publisher Logo
Share on FacebookShare on Google+Tweet about this on TwitterShare on LinkedIn
Loading Facebook Comments ...

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>