You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
db.createCollection('posts')
// if you insert into a non-existant collection it will be created automatically
db.createCollection("students")
// https://docs.mongodb.com/manual/reference/method/db.createCollection/index.html
db.createCollection("students", {options})
db.posts.insertMany([
{
title: 'Post Two',
body: 'Body of post two',
category: 'Technology',
date: Date()
},
{
title: 'Post Three',
body: 'Body of post three',
category: 'News',
date: Date()
},
{
title: 'Post Four',
body: 'Body of post three',
category: 'Entertainment',
date: Date()
}
])
Get All Rows
db.posts.find()
Get All Rows Formatted
db.find().pretty()
Find Rows
db.posts.find({ category: 'News' })
// Find all students
db.students.find( {} )
// Find the first 3 students
db.stuents.find( {} ).limit(3)
// Find all students and sort by name in ascending order
db.students.find( {} ).sort( {name: 1} )
// Find all students and sort by name in ascending order
db.students.find( {} ).sort( {gpa: -1, name: 1} )
// Find all biology majors
db.students.find( {major: "Biology"} )
// Find all student's with a phone number 333-3333
db.students.find( {contact: {phone: "333-3333", email: "student@school.edu"} } )
// Find all biology majors named Jack
db.students.find( {name: "Jack", major: "Biology"} )
// Final all students who are chemistry majors or named Jack
db.students.find( { $or: [ {name: "Jack"}, {major: "Chemistry"} ] } )
// Final all students with a gpa above 3.5
db.students.find( {gpa: {$gt: 3.5} } )
// Find all students with a gpa less than or equal to 3.2
db.students.find( {gpa: {$lte: 3.2} } ).sort({gpa: -1}) // $eq, $ne, $lt, $lte, $gt, $gte
// Find all students with names in the array
db.students.find( {name: {$in: ["Kate", "Claire"]} } ) // $in, $nin
// Find all students who have awards
db.students.find( {awards: {$exists: true} } ) // false
// Find all db entries where the name is a string
// Type list - https://docs.mongodb.com/manual/reference/bson-types/
db.students.find({name: {$type: 2} })
// Find all students who's first grade is a 90
db.students.find( {"grades.0": 90 } )
// Find all students who have a grade greater than 80
db.students.find( {grades: {$elemMatch: { $gte: 80} } } )
// Find all students who have 4 grades recorded
db.students.find( {grades: {$size: 4 } } )
//first field is criteria, second field is data to update, thrid field is insert if absent
// notice this operator works more like replace, compared to the next operator, so second field has to have all the data for that row, plus new data. If there is no old data for that row, it will replace the data with new data.
db.posts.update({ title: 'Post Two' },
{
title: 'Post Two',
body: 'New body for post 2',
date: Date()
},
{
upsert: true
})
// same filters as inserting
db.stuents.updateOne(<filter>, <update>, <options>)
// Do this twice so we can change it back with updateMany
db.students.updateOne(
{major: "Biology"},
{
$set:
{major: "Bio"}
}
)
db.students.updateMany(
{major: "Bio"},
{
$set:
{major: "Biology"}
}
)
// replaceMany()
db.students.replaceOne(
{major: "Bio"},
{name: "new name", major: "new major", gpa: 4.0}
)
// Delete all documents
db.students.deleteMany({})
db.students.deleteOne({major: "Biology"})
db.students.deleteMany({gpa: {$gte: 3.5}})
Update Specific Field
db.posts.update({ title: 'Post Two' },
{
$set: {
body: 'Body for post 2',
category: 'Technology'
}
})
db.purchase_orders.insertMany(
[
{product: "toothbrush", total: 4.75, customer: "Mike"},
{product: "guitar", total: 199.99, customer: "Tom"},
{product: "milk", total: 11.33, customer: "Mike"},
{product: "pizza", total: 8.50, customer: "Karen"},
{product: "toothbrush", total: 4.75, customer: "Karen"},
{product: "pizza", total: 4.75, customer: "Dave"}
{product: "toothbrush", total: 4.75, customer: "Mike"},
]
)
// find out how many toothbrushes were sold
db.purchase_orders.count({product: "toothbrush"})
// Find list of all products sold
db.purchase_orders.distinct("product")
// Find the total amount of money spent by each customer
db.purchase_orders.aggregate(
[
{$match: {} },
{$group: {_id: "$customer", total: { $sum: "$total"} } }
]
)
// Find how much has been spent on each product and sort it by price
db.purchase_orders.aggregate(
[
{$match: {} },
{$group: {_id: "$product", total: { $sum: "$total"} } },
{$sort: {total: -1}}
]
)
// Find how much money each customer has spent on toothbrushes and pizza
db.purchase_orders.aggregate(
[
{$match: {product: {$in: ["toothbrush", "pizza"]} } },
{$group: {_id: "$product", total: { $sum: "$total"} } },
]
)
// https://docs.mongodb.com/manual/reference/operator/aggregation/
// Show the list of all pipeline operators