Notes from MongoDB Basics Course

{
<field> : <value>,
"name" : "Mehmet",
"surname" : "Akçay",
"age" : 31
}
mongodump --uri "mongodb+srv://<your username>:<your password>@<your cluster>.mongodb.net/sample_supplies"  mongoexport --uri="mongodb+srv://<your username>:<your password>@<your cluster>.mongodb.net/sample_supplies" --collection=sales --out=sales.json  mongorestore --uri "mongodb+srv://<your username>:<your password>@<your cluster>.mongodb.net/sample_supplies"  --drop dump  mongoimport --uri="mongodb+srv://<your username>:<your password>@<your cluster>.mongodb.net/sample_supplies" --drop sales.json
show dbs  -- lists databasesuse sample_training  --chooses a database

show collections
db.zips.find({"state": "NY"})
-- finds documents with query details
--db points to sample_training since we chose to use it above
db.inspections.insert([{ "_id": 1, "test": 1 },{ "_id": 1, "test": 2 }, { "_id": 3, "test": 3 }],{ "ordered": false })
--ordered part is optional
db.zips.updateMany({ "city": "HUDSON" }, { "$inc": { "pop": 10 } })
db.zips.updateOne({ "zip": "12534" }, { "$set": { "pop": 17630 } })
db.grades.updateOne({ "student_id": 250, "class_id": 339 },                     { "$push": { "scores": { "type": "extra credit",                                              "score": 100 }                                 }                      })
db.inspections.deleteMany({ "test": 1 })
db.inspections.deleteOne({ "test": 3})
db.inspection.drop()
╔══════════╦══════════════════════════╗
║ Operator ║ Explanation ║
╠══════════╬══════════════════════════╣
║ $eq ║ Equal to ║
║ $neq ║ Not equal to ║
║ $lt ║ Less than ║
║ $lte ║ Less than or equal to. ║
║ $gt ║ Greater than ║
║ $gte ║ Greater than or equal to ║
╚══════════╩══════════════════════════╝
db.trips.find({ "tripduration": { "$lte" : 70 },                         
"usertype". : { "$ne": "Subscriber" } })
.pretty()
╔══════════╦═══════════════════════════════════════════════════╗
║ Operator ║ Use ║
╠══════════╬═══════════════════════════════════════════════════╣
║ $and ║ ║
║ $or ║ { <operator> : [{statement1}, {statement2}, ...]} ║
║ $nor ║ ║
╠══════════╬═══════════════════════════════════════════════════╣
║ $not ║ { $not : {statement}} ║
╚══════════╩═══════════════════════════════════════════════════╝
db.trips.find({ "$expr": { "$eq": [ 
"$end station id", "$start station id"] } }).count()

Array Operations

{<array_field> : { "$size" : <number>}}
{<array_field> : { "$all" : <array>}}

Projection

In order to project a document to a subset of its fields, projections are used as follows:

db.<collection>.find({<query>},{<field1>:1, <field2>:1})
db.grades.find({ "class_id": 431 },                
{ "scores": { "$elemMatch": { "score": { "$gt": 85 } } } }).pretty()

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Mehmet Akcay

Mehmet Akcay

3 Followers

a geek who loves to understand the reasons behind things... and colors... Colors are cool.