In MongoDB, you insert data as documents into collections. A document is a JSON-like structure that holds data as key-value pairs.
Use insertOne()
to insert a single document into a collection.
db.students.insertOne({ name: "Rahul Kumar", age: 22, course: "B.Tech" });
Use insertMany()
to insert multiple documents at once.
db.students.insertMany([ { name: "Aisha", age: 21, course: "B.Sc" }, { name: "Varun", age: 23, course: "MBA" } ]);
Each document will automatically be assigned a unique _id
field by MongoDB unless you specify one.
Help others discover Technorank Learning by sharing your honest experience.
Your support inspires us to keep building!