insertOne Function in MongoDB and Nodejs: Ultimate Guide

InsertOne Function Featured Image

Today’s MongoDB tutorial walks you through the insertOne function in MongoDB as well as Nodejs.

Today, we will talk about the insertOne function. This is the most basic of the functions that we come across when using MongoDB. It allows us to insert a single document at a time. Moreover, you can even create a collection and add a single document using the insertOne function.

We do not need to specify an ’_id’ property for the document we add, as the insertOne function adds it automatically.

This beginner’s guide on the insertOne function shall give you a walkthrough with examples on how to use this function in the mongo shell and in a Nodejs application. So, let us get started.

The MongoDB insertOne Function Syntax

Let us take a quick look at the insertOne function in MongoDB:

db.Collection_name.insertOne(
document,
{
    writeConcern: document
})

Using the insertOne Function in MongoDB and Nodejs

From here on, I will show you how to use the insertOne function in MongoDB and Nodejs.

1. The insertOne Function in Mongo Shell

In this example, we will use the insertOne function in the mongo shell.

  • Start up your MongoDB service
  • Pick the database you want to use and navigate into it:
show dbs
use droneSpree
  • Let us take a look at all our existing documents in our collections:
> db.drones.find({}).pretty()

{
        "_id" : ObjectId("6161c46c13ca7f1ba14cfe1e"),
        "utility" : [
                "Videography",
                "Combat",
                "Rescue",
                "Construction"
        ],
        "onSale" : false,
        "name" : "Shadow X-copter C32",
        "price" : 69500,
        "weight" : "65 kilograms",
        "additionalDetails" : {
                "material" : "glass fiber",
                "moreUses" : [
                        "Delivery",
                        "Monitoring or Inspection",
                        "Recreation"
                ]
        }
}
{
        "_id" : ObjectId("6161c46c13ca7f1ba14cfe1f"),
        "utility" : [
                "Videography",
                "Combat",
                "Rescue",
                "Construction"
        ],
        "onSale" : false,
        "name" : "Tranzm8 Dree RFD",
        "price" : 78500,
        "weight" : "13 kilograms",
        "additionalDetails" : {
                "material" : "carbon fiber",
                "moreUses" : [
                        "Land Inspection",
                        "Water Inspection"
                ]
        }
}
{
        "_id" : ObjectId("6161c46c13ca7f1ba14cfe20"),
        "utility" : [
                "Photography",
                "Combat",
                "Rescue",
                "Construction"
        ],
        "onSale" : false,
        "name" : "Sierra Gane’s Qatacopter J90",
        "price" : 89500,
        "weight" : "10 kilograms",
        "additionalDetails" : {
                "material" : "aluminum",
                "moreUses" : [
                        "Monitoring or Inspection",
                        "Recreation"
                ]
        }
}
  • Now, we will insert a new document into our collection using the insertOne function:
> db.drones.insertOne({
...         "utility" : [
...                 "Photography",
...                 "Combat",
...                 "Rescue",
...                 "Construction"
...         ],
...         "onSale" : false,
...         "name" : "Jennifer Steele’s Pegacopter PU-6",
...         "price" : 29500,
...         "weight" : "14 kilograms",
...         "additionalDetails" : {
...                 "material" : "lithium",
...                 "moreUses" : [
...                         "Videography",
...                         "Recreation"
...                 ]
...         }
... })

This is what the insertOne function returns us:

{
        "acknowledged" : true,
        "insertedId" : ObjectId("6161dcb013ca7f1ba14cfe21")
}
  • Let us look at what our collection now looks like:
> db.drones.find({}).pretty()

{
        "_id" : ObjectId("6161c46c13ca7f1ba14cfe1e"),
        "utility" : [
                "Videography",
                "Combat",
                "Rescue",
                "Construction"
        ],
        "onSale" : false,
        "name" : "Shadow X-copter C32",
        "price" : 69500,
        "weight" : "65 kilograms",
        "additionalDetails" : {
                "material" : "glass fiber",
                "moreUses" : [
                        "Delivery",
                        "Monitoring or Inspection",
                        "Recreation"
                ]
        }
}
{
        "_id" : ObjectId("6161c46c13ca7f1ba14cfe1f"),
        "utility" : [
                "Videography",
                "Combat",
                "Rescue",
                "Construction"
        ],
        "onSale" : false,
        "name" : "Tranzm8 Dree RFD",
        "price" : 78500,
        "weight" : "13 kilograms",
        "additionalDetails" : {
                "material" : "carbon fiber",
                "moreUses" : [
                        "Land Inspection",
                        "Water Inspection"
                ]
        }
}
{
        "_id" : ObjectId("6161c46c13ca7f1ba14cfe20"),
        "utility" : [
                "Photography",
                "Combat",
                "Rescue",
                "Construction"
        ],
        "onSale" : false,
        "name" : "Sierra Gane’s Qatacopter J90",
        "price" : 89500,
        "weight" : "10 kilograms",
        "additionalDetails" : {
                "material" : "aluminum",
                "moreUses" : [
                        "Monitoring or Inspection",
                        "Recreation"
                ]
        }
}
{
        "_id" : ObjectId("6161dcb013ca7f1ba14cfe21"),
        "utility" : [
                "Photography",
                "Combat",
                "Rescue",
                "Construction"
        ],
        "onSale" : false,
        "name" : "Jennifer Steele’s Pegacopter PU-6",
        "price" : 29500,
        "weight" : "14 kilograms",
        "additionalDetails" : {
                "material" : "lithium",
                "moreUses" : [
                        "Videography",
                        "Recreation"
                ]
        }
}

You can also notice that the insertOne function has automatically generated the ‘_id’ for us.

Using the MongoDB insertOne Function in Nodejs

In this example, we will be using the MongoDB insertOne function in a Nodejs application. For this, you will need a Nodejs application.

  • Let us create or insert a new document in our collection via Nodejs. Assume we have written this code in a file named insertDoc.js:
await db.drones.insertOne({
        "utility" : [
                "Security",
                "Combat",
                "Rescue",
                "Construction"
        ],
        "onSale" : false,
        "name" : "Tony Blaire Hawk Grim 4590",
        "price" : 87600,
        "weight" : "13 kilograms",
        "additionalDetails" : {
                "material" : "polysterene",
                "moreUses" : [
                        "Land Inspection",
                        "Photography"
                ]
        }
})
  • Now, run insertDoc.js. Also, make sure your MongoDB server is up and running:
node insertDoc.js
  • Switch to your mongo shell and print all the documents to see if it has been inserted:
> db.drones.find({}).pretty()

{
        "_id" : ObjectId("6161c46c13ca7f1ba14cfe1e"),
        "utility" : [
                "Videography",
                "Combat",
                "Rescue",
                "Construction"
        ],
        "onSale" : false,
        "name" : "Shadow X-copter C32",
        "price" : 69500,
        "weight" : "65 kilograms",
        "additionalDetails" : {
                "material" : "glass fiber",
                "moreUses" : [
                        "Delivery",
                        "Monitoring or Inspection",
                        "Recreation"
                ]
        }
}
{
        "_id" : ObjectId("6161c46c13ca7f1ba14cfe1f"),
        "utility" : [
                "Videography",
                "Combat",
                "Rescue",
                "Construction"
        ],
        "onSale" : false,
        "name" : "Tranzm8 Dree RFD",
        "price" : 78500,
        "weight" : "13 kilograms",
        "additionalDetails" : {
                "material" : "carbon fiber",
                "moreUses" : [
                        "Land Inspection",
                        "Water Inspection"
                ]
        }
}
{
        "_id" : ObjectId("6161c46c13ca7f1ba14cfe20"),
        "utility" : [
                "Photography",
                "Combat",
                "Rescue",
                "Construction"
        ],
        "onSale" : false,
        "name" : "Sierra Gane’s Qatacopter J90",
        "price" : 89500,
        "weight" : "10 kilograms",
        "additionalDetails" : {
                "material" : "aluminum",
                "moreUses" : [
                        "Monitoring or Inspection",
                        "Recreation"
                ]
        }
}
{
        "_id" : ObjectId("6161dcb013ca7f1ba14cfe21"),
        "utility" : [
                "Photography",
                "Combat",
                "Rescue",
                "Construction"
        ],
        "onSale" : false,
        "name" : "Jennifer Steele’s Pegacopter PU-6",
        "price" : 29500,
        "weight" : "14 kilograms",
        "additionalDetails" : {
                "material" : "lithium",
                "moreUses" : [
                        "Videography",
                        "Recreation"
                ]
        }
}
{
        "_id" : ObjectId("6161e33213ca7f1ba14cfe22"),
        "utility" : [
                "Security",
                "Combat",
                "Rescue",
                "Construction"
        ],
        "onSale" : false,
        "name" : "Tony Blaire Hawk Grim 4590",
        "price" : 87600,
        "weight" : "13 kilograms",
        "additionalDetails" : {
                "material" : "polysterene",
                "moreUses" : [
                        "Land Inspection",
                        "Photography"
                ]
        }
}

Splendid! The MongoDB insertOne function has inserted the specified document.

Read more: Insert a Document into MongoDB Using Mongoose – Quick 2021 Guide

Conclusion

In this article, we’ve discussed how to use the insertOne function in the Mongo shell and in a Nodejs application.

Noteworthy References

MongoDB