You are on page 1of 1

MongoDB (Mar-2021) 1 EXERCISES

1 Exercises
Copy the questions, paste them to a text file (using gedit or notepad++).
Execute the commands and analyze the result.
Copy the results and append them to the same text file.
Comment the results.
At the end of the day, send a copy of the resulting file to the email address at the footer of this document.

1.1 Group-03
Exercise 1 (Find documents)
Retrieve all documents in the inventory collection where the status equals "A" and qty is less than ($lt)
30. Analyze and comment what you’ve done.
db.inventory.find({ status: "A", qty: { $lt: 30 } })

En-MongoDB-Ex011-FindCondition

Exercise 2 (Find documents)


Retrieve all documents in the collection where the status equals "A" or qty is less than ($lt) 30. Analyze
and comment what you’ve done.
db.inventory.find({ $or: [ { status: "A" }, { qty: { $lt: 30 } } ] })

En-MongoDB-Ex012-FindCondition

Exercise 3 (Find documents)


Retrieve all documents in the collection where the status equals "A" and either qty is less than ($lt) 30
or item starts with the character "p". Analyze and comment what you’ve done.
db.inventory.find({
status: "A",
$or: [ { qty: { $lt: 30 } }, { item: /^p/ } ]
})

En-MongoDB-Ex013-FindCondition

Exercise 4 (Find documents)


Retrieve all documents where the field size equals the document h: 14, w: 21, uom: "cm" . Analyze and
comment what you’ve done.
db.inventory.find({ size: { h: 14, w: 21, uom: "cm" } })

Note
Equality matches on the whole embedded document require an exact match of the specified <value> document, including
the field order.
For example, the following query does not match any documents in the inventory collection:
db.inventory.find(size: w: 21, h: 14, uom: "cm" )
En-MongoDB-Ex014-FindCondition

Exercise 5 (Find documents)


Retrieve all documents where the field uom nested in the size field equals "in". Analyze and comment
what you’ve done.
db.inventory.find({ "size.uom": "in" })

En-MongoDB-Ex015-FindCondition

Agostinho Monteiro monteiro.agostinho@gmail.com Page 1 of 1

You might also like