Tutorialsteacher

Follow Us

Articles
  • C#
  • C# OOP
  • ASP.NET Core
  • ASP.NET MVC
  • LINQ
  • Inversion of Control (IoC)
  • Web API
  • JavaScript
  • TypeScript
  • jQuery
  • Angular 11
  • Node.js
  • D3.js
  • Sass
  • Python
  • Go lang
  • HTTPS (SSL)
  • Regex
  • SQL
  • SQL Server
  • PostgreSQL
  • MongoDB
  • MongoDB - Get Started
  • What is MongoDB?
  • Install MongoDB
  • MongoDB Server
  • MongoDB Shell
  • MongoDB Shell Commands
  • MongoDB Compass
  • MongoDB Database
  • MongoDB Collections
  • MongoDB Documents
  • Insert Single Document
  • Insert Multiple Documents
  • Import Data into Collection
  • Find Single Document
  • Find Multiple Documents
  • MongoDB Cursor
  • MongoDB Sort Documents
  • Update Single Document
  • Update Multiple Documents
  • Update Arrays
  • Update Embedded Documents
  • Delete Documents
  • Relations in MongoDB
  • Aggregation in MongoDB
Entity Framework Extensions - Boost EF Core 9
  Bulk Insert
  Bulk Delete
  Bulk Update
  Bulk Merge

MongoDB - Insert Single Document in a Collection using insertOne()

In MongoDB, a collection represents a table in RDBMS and a document is like a record in a table. Learn how to insert a single document into a collection.

MongoDB provides the following methods to insert documents into a collection:

  1. insertOne() - Inserts a single document into a collection.
  2. insert() - Inserts one or more documents into a collection.
  3. insertMany() - Insert multiple documents into a collection.

insertOne()

Use the db.<collection>.insertOne() method to insert a single document in a collection.db points to the current database, <collection> is existing or a new collection name.

Syntax:

db.collection.insertOne(document, [writeConcern])

Parameters:

  1. document: A document to insert into the collection.
  2. writeConcern: Optional. A document expressing the write concern to override the default write concern.

The following inserts a document into employees collection.

Example: insertOne()
db.employees.insertOne({ 
    firstName: "John",
    lastName: "King",
    email: "[email protected]"
})
Output
{
  acknowledged: true,
  insertedId: ObjectId("616d44bea861820797edd9b0")
}

In the above example, we passed a document to the insertOne() method. Notice that we haven't specified _id field. So, MongoDB inserts a document to a collection with the auto-generated unique _id field. It returns an object with a boolean field acknowledged that indicates whether the insert operation is successful or not, and insertedId field with the newly inserted _id value.

The following shows the insert operation in the mongosh shell:

InsertOne() in mongosh Shell

Use the find() to list all data of a collection, and the pretty() method to format resulted data.

db.employees.find().pretty()
Output
{
    _id: ObjectId("616d44bea861820797edd9b0"),
    firstName: "John",
    lastName: "King",
    email: "[email protected]"
}

MongoDB is NoSQL database. So, it does not enforce schema to any collection. It means you can insert a document with any fields to a collection. For example, the following inserts a document with different fields to the employees collection.

Example: Insert a Document
db.employees.insertOne({ 
    fName: "John",
    lName: "King",
    emailid: "[email protected]"
})
Output
{
  acknowledged: true,
  insertedId: ObjectId("546d44bea861820797ed214")
}

It is recommended to keep the field names same in all the documents of a collection to manage them easily.

Insert _id Manually

It is not necessary to insert auto-generated _id value. You can manually specify a unique value for the _id field, as shown below.

Example: Insert a Document
db.employees.insertOne({ 
    _id:"1",
    firstName: "John",
    lastName: "King",
    email: "[email protected]"
})
Output
{
  acknowledged: true,
  insertedId: 1
}

Note that while adding your custom value to _id field, a value must be unique; otherwise, it will throw an error. The following tries to add the same _id value.

Example: Insert a Document
db.employees.insertOne({ 
    _id:"1",
    firstName: "John",
    lastName: "King",
    email: "[email protected]"
})
Output
MongoServerError: E11000 duplicate key error collection: humanResourceDB.employees index: _id_ dup key: { _id: "1" }
TUTORIALSTEACHER.COM

TutorialsTeacher.com is your authoritative source for comprehensive technologies tutorials, tailored to guide you through mastering various web and other technologies through a step-by-step approach.

Our content helps you to learn technologies easily and quickly for learners of all levels. By accessing this platform, you acknowledge that you have reviewed and consented to abide by our Terms of Use and Privacy Policy, designed to safeguard your experience and privacy rights.

[email protected]

ABOUT USTERMS OF USEPRIVACY POLICY
copywrite-symbol

2024 TutorialsTeacher.com. (v 1.2) All Rights Reserved.