Introduction
Insert operations add new information to a database. Without inserting data, the database would be empty and not useful.
Jump into concepts and practice - no test required
Insert operations add new information to a database. Without inserting data, the database would be empty and not useful.
db.collection.insertOne(document)
db.collection.insertMany([document1, document2, ...])insertOne to add a single document.insertMany to add multiple documents at once.db.users.insertOne({ name: "Alice", age: 25 })db.products.insertMany([
{ name: "Pen", price: 1.5 },
{ name: "Notebook", price: 3.0 }
])This example switches to the shopDB database, inserts one customer, then inserts two orders.
use shopDB // Insert one customer db.customers.insertOne({ name: "John Doe", email: "john@example.com" }) // Insert multiple orders db.orders.insertMany([ { orderId: 101, product: "Book", quantity: 2 }, { orderId: 102, product: "Pen", quantity: 5 } ])
Inserted documents get a unique _id field automatically if not provided.
Insert operations are fast and essential for building up your data.
Insert operations add new data to your database.
Use insertOne for single entries and insertMany for multiple entries.
Without inserts, your database would have no information to work with.
users?insertOne is used to insert a single document.insertMany is for multiple documents, insert is deprecated, and addOne is invalid.db.products.insertMany([
{name: 'Pen', price: 1.5},
{name: 'Notebook', price: 3.0}
])db.products.find().toArray() return?db.orders.insertOne({orderId: 101, item: 'Book', quantity: 2})email. Which approach best uses insert operations to avoid duplicates?email prevents duplicate emails in the collection.