MONGODB Tutorial



MongoDB INSERT


๐Ÿ“ฅ MongoDB Insert Documents

In MongoDB, you insert data as documents into collections. A document is a JSON-like structure that holds data as key-value pairs.

๐Ÿ”น Insert One Document

Use insertOne() to insert a single document into a collection.

db.students.insertOne({
  name: "Rahul Kumar",
  age: 22,
  course: "B.Tech"
});
  

๐Ÿ”น Insert Multiple Documents

Use insertMany() to insert multiple documents at once.

db.students.insertMany([
  { name: "Aisha", age: 21, course: "B.Sc" },
  { name: "Varun", age: 23, course: "MBA" }
]);
  

๐Ÿงพ Note on Document Structure

Each document will automatically be assigned a unique _id field by MongoDB unless you specify one.

๐Ÿง  Remember: Collections are created automatically when you insert a document into a non-existing collection.
๐Ÿ’ก Tip: You can insert documents with different structures in the same collection โ€“ MongoDB is schema-less!

๐ŸŒŸ Enjoyed Learning with Us?

Help others discover Technorank Learning by sharing your honest experience.
Your support inspires us to keep building!

Leave a Google Review