MongoDB - How to Insert Documents in MongoDB using Robo 3T - TechDB

Latest

All about Database Programming, Performance Tuning and Best Practices.

BANNER 728X90

Thursday, 19 December 2019

MongoDB - How to Insert Documents in MongoDB using Robo 3T



Insert a Document or Documents into a Collection Is same as Insert a record or records in a table in RDBMS.

1 Document in MongoDB Collection   1 Row in RDBMS Table

There are three methods available to insert documents into collection.

1 - Insert() - Inserts a document or documents into a collection

Syntax:
db.collection_name.insert(
   [ <document 1> , <document 2>, ... ]
)



Example: Let’s take an example to show how to insert a document or documents into a collection. In this example we are going to create a collection named “Book” in TechDB Databse.

Insert only one document

db.Book.insert(
{
    Chapter: "mongodb",
     details: {
                   title: "MongoDB Insert documents ",
                    Author: "Sanjay"
                 }
}
)

Insert many documents

db.Book.insert(
[
{
    "Chapter" : "mongodb-Many",
    "details" : {
        "title" : "Insert Many documents",
        "Author" : "Sanjay"
    }
},
{
    "Chapter" : "Robo3T-Many",
    "details" : {
        "title" : "Install Robo3T",
        "Author" : "Manoj"
    }
}
]
)

Note - Insert documents into collection. If the collection does not exist, the insert() method creates the collection. In the above example “Book” is collection name.

Or you can also Create Collection without adding document using below command.

db.createCollection("Book")


In this method no document returned by application. It show only a message for successfully inserted.


2 - InsertOne()- Inserts a document into a collection. It allows you to insert exactly 1 document in the collection. Its syntax is the same as that of single insert in insert().


Syntax:
db.collection.insertOne(
   { <document 1> }
)

db.collection.insertOne(Document)

Returns the following document:

a)    A boolean acknowledged as true
b)    An array of _id for each successfully inserted documents


3 - InsertMany() - Inserts multiple documents into a collection.


Syntax:
db.collection.insertMany(
   [ <document 1> , <document 2>, ... ]
)


Returns the following document:

c)    A boolean acknowledged as true
d)    An array of _id for each successfully inserted documents


Let check the documents created on Collection “Book” using below command.

db.Book.find()


db – refer to connected database. In our case connected Databse name is “TechDB”


We have added few documents into collection named Book. Robo 3T provide facility to view the result in 3 different mode.


View results in Tree mode


View results in Table mode


View results in Text mode


In every documents you can see an additional field called “_id

If the document does not specify an _id field, then MongoDB will add the _id field and assign a unique ObjectId for the document before inserting. If the document contains an _id field, the _id value must be unique within the collection to avoid duplicate key error.

_id is 12 bytes hexadecimal number unique for every document in a collection. 12 bytes are divided as follows –
_id: ObjectId(
                       4 bytes timestamp,
                       3 bytes machine id,
                       2 bytes process id,
                       3 bytes incrementor
                    )

Find() – This method used for finding the documents with some condition. Similar to RDBMS WHERE condition. If nothing mentioned in find() then it will return all the documents for that collection.

Pretty() – This method beautify the JSON documents or the collections within in the Mongo shell.

You can also Insert Documents by using either Right Click on Collection in the left side tree of Database as mentioned below or Right click on list of documents shown in result after passing find() method in Tree mode or Table mode.


You can create new document and Save after validate the same.

No comments:

Post a Comment