mongoDB notes
used this tutorial
Basics
show dbs
shows databases
use appdb
switch to appdb
show collections
collections are like tables in sql, where you store your data
db.dropDatabase()
still can be in deleted database
db.users.insertOne({name: "John"})
db.users.find()
id is generated everytime you add something
no need to define schema - this is a nosql database
every object you store is called a document
add anything you inside a collection
appdb> db.users.find()
[
{ _id: ObjectId('66c7534c346e817e36607e64'), name: 'John' },
{
_id: ObjectId('66c753cc346e817e36607e65'),
name: 'Sally',
age: 19,
address: { street: '987 N St.' },
hobbies: [ 'Running' ]
}
]
db.users.insertMany([{}, {}])
insert an array of objects
Querying
db.users.find().limit(2)
db.users.find.sort({name: 1}).limit(2) //could be -1 to go opposite order
db.users.find.sort({age: -1, name: 1}).limit(2) //could be -1 to go opposite order
db.users.find.find().skip(1).limit(2)
// skips the first one
db.users.find({name: "Kyle"})
db.users.find({age: 26})
db.users.find({name: "Kyle"}, {name: 1, age: 26, _id: 0})
// complex query
db.users.find({ name: { $eq: "Sally" }}) // equal
db.users.find({ name: { $ne: "Sally" }}) // not equal
// $gt: greater than
// $gte: greater than or equal to
// $lte: less than or equal to
// $in: pass in array of items and check if its in the list
// $nin: opposite of nin
db.users.find({ age: { $exists: true }}) // return users that have an age field
// will return even if field is "null"
db.users.find({ age: { $gte: 20, $lte: 40 }}) // 20 < x < 40
db.users.find({ age: { $gte: 20, $lte: 40 }, name: "Kyle" }) // 20 < x < 40 and named kyle
db.users.find({ $and: [{age: 26}, {name: "Kyle"}]}) // dont need to use $and very much
db.users.find({ $or: [{age: {$lte: 20}}, {name: "Kyle"}]}) // return less than 20 or someone named kyle
db.users.find({age: {$not: {$lte: 20}}}) // all of users that are not less than or equal to 20
db.users.find({$expr: {$gt: ["$debt", "$balance"]}}) // $expr: expression
// checking if the debt is greater than the balance
db.users.find({ "address.street": "123 Main St"}) // check objects within objects
db.users.findOne() // just find one object that matches the query
Updating
db.users.updateOne({age: 26}, {$set: {age: 27}})
// find update
db.users.updateOne({_id: ObjectId('66c7534c346e817e36607e64')}, {$set: {name: "new name"}})
// can use $inc instead of $set to increment a number
// can use $rename to rename a property.
db.users.updateOne({_id: ObjectId('66c7534c346e817e36607e64')}, {$rename: {name: "firstName"}})
// this renames the "name" tag to firstName
db.users.updateOne({_id: ObjectId('66c7534c346e817e36607e64')}, {$unset: {age: ""}})
// $unset completely removes the age property
// $push adds to an array
db.users.updateOne({_id: ObjectId('66c7534c346e817e36607e64')}, {$push: {hobbies: "bowling"}})
// $pull removes from an array
db.users.updateOne({_id: ObjectId('66c7534c346e817e36607e64')}, {$pull: {hobbies: "bowling"}})
//update many users
db.users.updateMany({ address: {$exists: true}}, {$unset: {address: ""}})
// remove address from all users that have an address
db.users.replaceOne({ age: 30}, {name: "John"})
// find at that location, then replaces it completely with the object
// use update instead of replace
db.users.deleteOne({})
// takes in the same params as find
db.users.deleteMany({})
// takes in same paramss as find