Download presentation
Presentation is loading. Please wait.
1
Write Operations
2
Agenda Create Collection Insert Update Remove
3
Create Collection db.createCollection(“COLLECTION_NAME”)
You can also insert into a collection that doesn’t exist yet. This will create a new collection db.people.insertOne( { user_id: "abc123", age: 55, status: "A" } )
4
Insert MongoDB db.collection.insert()
Inserts a new doc with the age, name, status, and _id fields
5
Insert Mysql
6
Insert Adding a doc without an _id field is ok. MongoDB will auto generate a unique id. If you specify the _id field, it must be unique to the current collection.
7
Insert One db.collection.insertOne() inserts a single document into a collection db.inventory.insertOne( { item: "canvas", qty: 100, tags: ["cotton"], size: { h: 28, w: 35.5, uom: "cm" } } ) Query the database to find the document that you just inserted: db.inventory.find( { item: "canvas" } )
8
Insert Multiple db.collection.insertMany() inserts multiple documents.
Pass an array of docs to insertMany db.inventory.insertMany([ { item: "journal", qty: 25, tags: ["blank", "red"], size: { h: 14, w: 21, uom: "cm" } }, { item: "mat", qty: 85, tags: ["gray"], size: { h: 27.9, w: 35.5, uom: "cm" } }, { item: "mousepad", qty: 25, tags: ["gel", "blue"], size: { h: 19, w: , uom: "cm" } } ])
9
Update MongoDB Updates a document that matches the criteria
Updates are atomic within a single document
10
Update Mysql
11
Update One db.inventory.updateOne( { item: "paper" }, { $set: { "size.uom": "cm", status: "P" } } )
12
Update Many db.books.updateMany( { stock: { $lte: 10 } }, { $set: { reorder: true } }, { multi: true } )
13
Upsert An update that, if the doc exists, it will do an update
If the doc does not exist, it will do an insert db.people.update( { name: "Andy" }, { name: "Andy", rating: 1, score: 1 }, { upsert: true } )
14
Update Operators Lots of different update operators
Only concerned with $set db.products.update( { _id: 100 }, { $set: { "details.make": "zzz" } } )
15
Remove MongoDB Deletes doc from collection
Accepts a query criteria to determine which docs to remove
16
Remove Mysql
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.