Lab-MongoDB CRUD
Lab-MongoDB CRUD
2. To display the database you are using, enter db (MongoDB commands are case-sensitive)
db.inventory.insertMany( [
{ item: "canvas", qty: 100, size: { h: 28, w: 35.5, uom: "cm" }, status: "A" },
{ item: "journal", qty: 25, size: { h: 14, w: 21, uom: "cm" }, status: "A" },
{ item: "mat", qty: 85, size: { h: 27.9, w: 35.5, uom: "cm" }, status: "A" },
{ item: "mousepad", qty: 25, size: { h: 19, w: 22.85, uom: "cm" }, status: "P" },
{ item: "notebook", qty: 50, size: { h: 8.5, w: 11, uom: "in" }, status: "P" },
{ item: "paper", qty: 50, size: { h: 8.5, w: 11, uom: "in" }, status: "A" },
{ item: "paper", qty: 100, size: { h: 8.5, w: 11, uom: "in" }, status: "D" },
{ item: "planner", qty: 75, size: { h: 22.85, w: 30, uom: "cm" }, status: "D" },
{ item: "postcard", qty: 45, size: { h: 10, w: 15.25, uom: "cm" }, status: "A" },
{ item: "postcard", qty: 70, size: { h: 10, w: 15.25, uom: "cm" }, status: "D" },
{ item: "sketchbook", qty: 80, size: { h: 14, w: 21, uom: "cm" }, status: "A" },
{ item: "sketch pad", qty: 95, size: { h: 22.85, w: 30.5, uom: "cm" }, status: "A" }
] );
You can copy the above command and right-click your mouse in the mongo shell to paste
and run the command. You may use the following command to delete all documents from
the inventory collection if you run the command more than once
db.inventory.deleteMany( {} )
7. Enter show dbs. Can you see the testDB database now?
Combination of AND/OR
db.inventory.find( { status: "A", $or: [ { qty: { $lt: 30 } }, { item: /^p/ } ] } )
(What does the above command do?)
This is equivalent to
db.inventory.find( { $or: [ { status: "A"}, { status: "D" } ] } )
13. Update a single document that matches a condition
First, what is the command to find all paper records?
db.inventory.find( { item: "paper" } )
16. Delete all documents from the inventory that match a condition
db.inventory.deleteMany ( { status : "A" } )
References
https://docs.mongodb.com/manual/
The course materials are only for the use of students enrolled in the course CSIS 3300 at Douglas
College. Sharing this material to a third-party website can lead to a violation of Copyright law.