Review the following code, which demonstrates how to insert a single document and multiple documents into a collection.
Use
insertOne()
to insert a document into a collection. Within the parentheses of
insertOne()
, include an object that contains the document data. Here's an example:
db.grades.insertOne({
student_id: 654321,
products: [
{
type: "exam",
score: 90,
},
{
type: "homework",
score: 59,
},
{
type: "quiz",
score: 75,
},
{
type: "homework",
score: 88,
},
],
class_id: 550,
})
Use
insertMany()
to insert multiple documents at once. Within
insertMany()
, include the documents within an array. Each document should be separated by a comma. Here's an example:
db.grades.insertMany([
{
student_id: 546789,
products: [
{
type: "quiz",
score: 50,
},
{
type: "homework",
score: 70,
},
{
type: "quiz",
score: 66,
},
{
type: "exam",
score: 70,
},
],
class_id: 551,
},
{
student_id: 777777,
products: [
{
type: "exam",
score: 83,
},
{
type: "quiz",
score: 59,
},
{
type: "quiz",
score: 72,
},
{
type: "quiz",
score: 67,
},
],
class_id: 550,
},
{
student_id: 223344,
products: [
{
type: "exam",
score: 45,
},
{
type: "homework",
score: 39,
},
{
type: "quiz",
score: 40,
},
{
type: "homework",
score: 88,
},
],
class_id: 551,
},
])
Review the following code, which demonstrates how to query documents in MongoDB.
When given equality with an
_id
field, the
find()
command will return the specified document that matches the
_id
. Here's an example:
db.zips.find({ _id: ObjectId("5c8eccc1caa187d17ca6ed16") })
Use the
$in
operator to select documents where the value of a field equals any value in the specified array. Here's an example:
db.zips.find({ city: { $in: ["PHOENIX", "CHICAGO"] } })
Review the following comparison operators:
$gt, $lt, $lte, and $gte
Use the
$gt
operator to match documents with a field greater than the given value. For example:
db.sales.find({ "items.price": { $gt: 50}})
Use the
$lt
operator to match documents with a field less than the given value. For example:
db.sales.find({ "items.price": { $lt: 50}})
Use the
$lte
operator to match documents with a field less than or equal to the given value. For example:
db.sales.find({ "customer.age": { $lte: 65}})
Use the
$gte
operator to match documents with a field greater than or equal to the given value. For example:
db.sales.find({ "customer.age": { $gte: 65}})
Review the following code, which demonstrates how to query array elements in MongoDB.
In the following example, "InvestmentFund" is not enclosed in square brackets, so MongoDB returns all documents within the
products
array that contain the specified value.
db.accounts.find({ products: "InvestmentFund"})
Use the
$elemMatch
operator to find all documents that contain the specified subdocument. For example:
db.sales.find({
items: {
$elemMatch: { name: "laptop", price: { $gt: 800 }, quantity: { $gte: 1 } },
},
})
Using the transactions collection, search for all accounts with a "sell" transaction which amounts to less than or equal to $4500. Note that the transactions field includes a sub document with an amount for each transaction and a transaction code of either sell or buy.
You are now connected to an Atlas cluster and to the sample_analytics database. Use the transactions collection in this lab.
Create a query that matches all documents with a transactions sub document that contains an amount less than or equal to $4500 and a transaction_code of "sell" in the transactions collection.
db.transactions.find({
transactions: {
$elemMatch: { amount: { $lte: 4500 }, transaction_code: "sell"}
},
})
Review the following logical operators: implicit $and, $or, and $and
Use implicit
$and
to select documents that match multiple expressions. For example:
db.routes.find({ "airline.name": "Southwest Airlines", stops: { $gte: 1 } })
Use the
$or
operator to select documents that match at least one of the included expressions. For example:
db.routes.find({
$or: [{ dst_airport: "SEA" }, { src_airport: "SEA" }],
})
Use the
$and
operator to use multiple
$or
expressions in your query.
db.routes.find({
$and: [
{ $or: [{ dst_airport: "SEA" }, { src_airport: "SEA" }] },
{ $or: [{ "airline.name": "American Airlines" }, { airplane: 320 }] },
]
})
In this lab, you will find all sales by customers who used a coupon online and are 25 years old or younger.
You are now connected to an Atlas cluster and to the sample_supplies database. Use the sales collection in this lab.
Find every document in the sales collection that meets the following criteria:
Purchased online
Used a coupon
Purchased by a customer 25 years old or younger
Sample Document
{
_id: ObjectId("5bd761dcae323e45a93ccfec"),
saleDate: ISODate("2017-12-03T18:39:48.253Z"),
items: [
{
name: 'backpack',
tags: [ 'school', 'travel', 'kids' ],
price: Decimal128("127.59"),
quantity: 3
},
{
name: 'notepad',
tags: [ 'office', 'writing', 'school' ],
price: Decimal128("17.6"),
quantity: 4
},
{
name: 'binder',
tags: [ 'school', 'general', 'organization' ],
price: Decimal128("18.67"),
quantity: 2
},
{
name: 'pens',
tags: [ 'writing', 'office', 'school', 'stationary' ],
price: Decimal128("60.56"),
quantity: 3
},
{
name: 'notepad',
tags: [ 'office', 'writing', 'school' ],
price: Decimal128("28.41"),
quantity: 1
},
{
name: 'envelopes',
tags: [ 'stationary', 'office', 'general' ],
price: Decimal128("15.28"),
quantity: 7
},
{
name: 'laptop',
tags: [ 'electronics', 'school', 'office' ],
price: Decimal128("1259.02"),
quantity: 3
}
],
storeLocation: 'London',
customer: { gender: 'M', age: 40, email: 'dotzu@ib.sh', satisfaction: 4 },
couponUsed: false,
purchaseMethod: 'In store'
}
Query:
db.sales.find({ $and: [{ purchaseMethod: "Online" }, { couponUsed: true }, {"customer.age": {$lte:25}} ] });
In this lab, you’ll find every sale which contains every item with a writing tag or a name of pens.
You are now connected to an Atlas cluster and to the sample_supplies database. Use the sales collection in this lab.
Return every document in the sales collection that meets one of the following criteria:
Item with the name of pens
Item with a writing tag
Sample same as above
db.sales.find({$or: [{"items.name":"pens"}, {"items.tags":"writing"}]});
or
db.sales.find({ $or: [{ "items.name": "pens" }, { "items.tags": "writing" }], })