Operation insertOne()
The insertOne() operation in MongoDB allows you to insert a single document into a collection atomically. This means that either the entire document is successfully inserted, or no changes are made to the collection, maintaining consistency.
const { MongoClient } = require("mongodb");
const client = new MongoClient("mongodb://localhost:27017/myapp");
await client.connect();
const db = client.db("myApp");
const answer = await db.collection("office").insertOne({
name: "GFG",
age: 10
});
What is Atomicity in MongoDB and How to Achieve it?
Atomicity ensures that all changes within a single operation are either fully completed or not applied at all, preventing data corruption and inconsistencies.
Table of Content
- Atomicity and How to Achieve it
- Operation Example: insertOne()
- Success or Error
- Atomicity on the Document Level
- Including Embedded Documents
- Single Document Operations
- Multi-Document Transactions
Contact Us