0% found this document useful (0 votes)
18 views

Lab06 Insert Document

The insert() method allows you to insert data into MongoDB collections. You specify the collection name and pass the document to insert. MongoDB will automatically generate an _id if not specified. You can insert multiple documents by passing an array of documents. The save() method works similarly but will replace the entire document if an _id is specified.

Uploaded by

slides course
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

Lab06 Insert Document

The insert() method allows you to insert data into MongoDB collections. You specify the collection name and pass the document to insert. MongoDB will automatically generate an _id if not specified. You can insert multiple documents by passing an array of documents. The save() method works similarly but will replace the entire document if an _id is specified.

Uploaded by

slides course
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

CHAPTER

10
MongoDB - Insert Document
The insert() Method
To insert data into MongoDB collection, you need to use MongoDB's insert() or save()method.

Syntax
Basic syntax of insert() command is as follows:

>db.COLLECTION_NAME.insert(document)

Example

>db.mycol.insert({
_id: ObjectId(7df78ad8902c),
title: 'MongoDB Overview',
description: 'MongoDB is no sql database',
by: 'tutorials point',
url: 'http://www.tutorialspoint.com',
tags: ['mongodb', 'database', 'NoSQL'],
likes: 100
})

Here mycol is our collection name, as created in previous tutorial. If the collection doesn't exist in the database, then
MongoDB will create this collection and then insert document into it.
In the inserted document if we don't specify the _id parameter, then MongoDB assigns an unique ObjectId for this
document.
_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 incrementer)

To insert multiple documents in single query, you can pass an array of documents in insert() command.

Example

TUTORIALS POINT
Simply Easy Learning Page 21
>db.post.insert([
{
title: 'MongoDB Overview',
description: 'MongoDB is no sql database',
by: 'tutorials point',
url: 'http://www.tutorialspoint.com',
tags: ['mongodb', 'database', 'NoSQL'],
likes: 100
},
{
title: 'NoSQL Database',
description: 'NoSQL database doesn't have tables',
by: 'tutorials point',
url: 'http://www.tutorialspoint.com',
tags: ['mongodb', 'database', 'NoSQL'],
likes: 20,
comments: [
{
user:'user1',
message: 'My first comment',
dateCreated: new Date(2013,11,10,2,35),
like: 0
}
]
}
])

To insert the document you can use db.post.save(document) also. If you don't specify _id in the document
then save() method will work same as insert() method. If you specify _id then it will replace whole data of
document containing _id as specified in save() method.

TUTORIALS POINT
Simply Easy Learning Page 22

You might also like