Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- EXP 6:
- MONGODB CRUD
- open cmd type mongosh // if not shows then no mongo is present
- use db;
- //Create collection
- db.createCollection("emp");
- // 1. Create (Insert)
- // Insert One Document
- db.collectionName.insertOne({
- key1: "value1",
- key2: "value2"
- });
- // Insert Multiple Documents
- db.collectionName.insertMany([
- { key1: "value1", key2: "value2" },
- { key1: "value3", key2: "value4" }
- ]);
- // 2. Read (Find)
- // Find One Document
- db.collectionName.findOne({ key: "value" });
- // Find All Documents
- db.collectionName.find({});
- // Find with Query
- db.collectionName.find({ key: "value" });
- // Find with Projection
- db.collectionName.find({ key: "value" }, { key1: 1, key2: 1 });
- // Find with Sorting
- db.collectionName.find({}).sort({ key: 1 });
- // Find with Limiting Results
- db.collectionName.find({}).limit(5);
- // 3. Update
- // Update One Document
- db.collectionName.updateOne(
- { filterKey: "value" },
- { $set: { keyToUpdate: "newValue" } }
- );
- // Update Multiple Documents
- db.collectionName.updateMany(
- { filterKey: "value" },
- { $set: { keyToUpdate: "newValue" } }
- );
- // Replace One Document
- db.collectionName.replaceOne(
- { filterKey: "value" },
- { newKey1: "newValue1", newKey2: "newValue2" }
- );
- // 4. Delete
- // Delete One Document
- db.collectionName.deleteOne({ filterKey: "value" });
- // Delete Multiple Documents
- db.collectionName.deleteMany({ filterKey: "value" });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement