SecPod

Learn Search

Search across all Learn content

← Back to Expressions & POVs
MongoDB – Merging Data into an Existing Document

MongoDB – Merging Data into an Existing Document

The world is huMONGOous, as is the amount of data we possess.

Mar 21, 2016By Preeti Subramanian4 min read

The world is huMONGOous, as is the amount of data we possess.

The above line explains where the word ‘Mongo’ originated and why we need MongoDB.  MongoDB is a cross-platform document orientation database. Document databases pair each key with a complex data structure known as a document. Documents can contain many key-value pairs, key-array pairs, or even nested ones. MongoDB will also have vulnerabilities that we can solve using a Vulnerability Management Tool.

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. A schema need not be defined before inserting data. This is helpful in agile development where there are frequent iterations in development, data changes its form, data types are polymorphic, and the volume of data is massive.

Challenges with massive data and MongoDB Vulnerabilities

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? Programmatically, should we fetch the whole document into memory and traverse to the key at which that value needs to be changing? Isn’t it expensive? What are the vulnerabilities?

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

If one of the network interfaces changes in a system, we intend to update this information in our database. However, The simplest way to do this is to 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. To manage vulnerabilities on multiple systems, a good vulnerability management software will be helpful.

Solution

We use the update command to add or modify an existing document in MongoDB.

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
MDB Image 2

Removing an existing item in an array

MDB Image 3
MDB Image 3

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”}}});

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 system info, 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
MDB Image 4

Such modifications especially aid in cases where IPs are assigning dynamically, and a change in the database is necessary from time to time.

Summarizing, the above examples avoid 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. Therefore I hope this helps you understand the Mongodb. vulnerability!

Preeti Subramanian

Featured Posts

Open The Future of Cloud Security Runs Through Containers
The Future of Cloud Security Runs Through Containers

Point of View

The Future of Cloud Security Runs Through Containers

Cloud security is moving closer to the workload. As containers and Kubernetes become standard infrastructure, security programs need to follow applications from build to runtime.

Nov 6, 2026

Open Attack Surface Risk Management: How to Prioritize Exposures
Attack Surface Risk Management: How to Prioritize Exposures

Point of View

Attack Surface Risk Management: How to Prioritize Exposures

Finding exposed assets is only the beginning. See how security teams can rank exposures using threat evidence, reachability, privilege, attack paths, and business context.

Nov 6, 2026

Open What Is an Attack Surface? Types and Examples
What Is an Attack Surface? Types and Examples

Point of View

What Is an Attack Surface? Types and Examples

An attack surface includes the systems, identities, services, interfaces, and physical access points an adversary could use to gain access or reach sensitive resources.

Nov 6, 2026

Open How to Move From Kubernetes Risk Discovery to Risk Reduction
How to Move From Kubernetes Risk Discovery to Risk Reduction

Point of View

How to Move From Kubernetes Risk Discovery to Risk Reduction

Kubernetes security teams can find thousands of misconfigurations, vulnerabilities, and permission issues. Risk reduction depends on knowing which ones matter, fixing the underlying condition, and verifying the result.

Nov 2, 2026