Skip to main content

Insert One Document

Insert one document to the data collection.

How to use

The insertOne method is used to insert a single document into a MongoDB collection.

MongoDB command

db.collection.insertOne(
<document>,
{
writeConcern: <document>
}
)

Node.js - MongoDB native driver

const doc = { title: "LearnDBNow", content: "Welcome to LearnDBNow!" };
const result = await db.collection('books').insertOne(doc);
console.log(
`A document was inserted with the _id: ${result.insertedId}`,
);

Python - MongoDB native driver

db.inventory.insert_one(
{
"title": "LearnDBNow",
"content": "Welcome to LearnDBNow!",
}
)