MongoDB
Add MongoDB Array using insert() with Example
Adding an array of documents --> The "insert" command can also be used to insert multiple...
In MongoDB, the first basic step is to have a database and collection in place. The database is used to store all of the collections, and the collection in turn is used to store all of the documents. The documents in turn will contain the relevant Field Name and Field values.
The snapshot below shows a basic example of how a document would look like.
The Field Names of the document are "Employeeid" and "EmployeeName" and the Field values are "1" and "Smith' respectively. A bunch of documents would then make up a collection in MongoDB.
In this article, you will learn -
Creating a database in MongoDB is as simple as issuing the "using" command. The following example shows how this can be done.
Code Explanation:
If the command is executed successfully, the following Output will be shown:
Output:
MongoDB will automatically switch to the database once created.
The easiest way to create a collection is to insert a record (which is nothing but a document consisting of Field names and Values) into a collection. If the collection does not exist a new one will be created.
The following example shows how this can be done.
db.Employee.insert
(
{
"Employeeid" : 1,
"EmployeeName" : "Martin"
}
)
Code Explanation:
As seen above, by using the "insert" command the collection will be created.
MongoDB provides the insert () command to insert documents into a collection. The following example shows how this can be done.
Step 1) Write the "insert" command
Step 2) Within the "insert" command, add the required Field Name and Field Value for the document which needs to be created.
Code Explanation:
If the command is executed successfully, the following Output will be shown
Output:
The output shows that the operation performed was an insert operation and that one record was inserted into the collection.
Adding an array of documents --> The "insert" command can also be used to insert multiple...
{loadposition top-ads-automation-testing-tools} MongoDB is an open source NoSQL DBMS which uses a...
Download PDF Following are frequently asked questions in interviews for freshers as well...
What is NoSQL? NoSQL Database is a non-relational Data Management System, that does not require a...
Training Summary MongoDB is a document-oriented NoSQL database used for high volume data storage....
One of the key concepts in MongoDB is the management of databases. Important aspects such as...