MongoDB
"MongoDB is a general purpose, document-based, distributed database built for modern application developers and for the cloud era. No database makes you more productive."
Starting and Stopping
brew services start mongodb-community@4.4
brew services stop mongodb-community@4.4
MongoDB Atlas
"MongoDB Atlas is the global cloud database service for modern applications. Deploy fully managed MongoDB across AWS, Azure, or GCP. Best-in-class automation and proven practices guarantee availability, scalability, and compliance with the most demanding data security and privacy standards. Use MongoDB's robust ecosystem of drivers, integrations, and tools to build faster and spend less time managing your database."
https://www.mongodb.com/cloud/atlas
Element Operators
- $exists - checks for the existence of a given key
- $type - returns the type of a given a key
Connecting to the Sample Databases
Connecting to the Mongo University sample datasets in either Compass or via the shell...
mongo "mongodb+srv://cluster0-jxeqq.mongodb.net/test" --username m001-student -password m001-mongodb-basics
mongo "mongodb://cluster0-shard-00-00-jxeqq.mongodb.net:27017,cluster0-shard-00-01-jxeqq.mongodb.net:27017,cluster0-shard-00-02-jxeqq.mongodb.net:27017/test?replicaSet=Cluster0-shard-0" --authenticationDatabase admin --ssl --username m001-student --password m001-mongodb-basics
Connecting to my Cluster
dbUser PvCcAnLlJM5EIAoz mongo "mongodb+srv://cluster0-vafgz.mongodb.net/test" --username dbUser
mongodb+srv://dbUser:PvCcAnLlJM5EIAoz@cluster0-vafgz.mongodb.net/test
Guides
https://university.mongodb.com/exam/guide#crud
Data Types
- integer
- double
- string
- bool
- date
- object (document)
- array
Nesting documents
You can go as many levels deep with nested documents
Geospatial data
Note that geospatial is a type per se (e.g. it's just an array type with two doubles) there's a ton of support build into MongoDB, Compass, etc for working with it.
Querying
All about filtering...lots of filtering types, e.g. equality, range, etc.
- eq - equality
- gte - greater than or equal to
- lte - less than or equal to
Example queries
{'end station name': 'Broadway & E 22 St', 'birth year': {$gte: 1960,$lt: 1965}
{tripduration: {'$gte': 60, '$lt': 65}}
Note that $gte and $lte are called operators.
If you pass multiple criteria in the same query document they are implicitly “ANDED” together.
Querying Embedded Documents
Dot notation is used to query nested documents.
dbName.collectionName.find({wind.type: “N”})
Querying Against Arrays
A few points to note:
- For searching for just a single element it’s the same syntax of that of a document
- For multiple elements, you can pass in an array (e.g.
{ "cast": [“Jeff Bridges", “John Locke”] }
) although not this will look for these elements in this exact order. - Is you want to search for an element for at a specific position / index in an array you can use the following:
{ "cast.0": "Jeff Bridges" }
Projections
Projections allow you to explicitly specify the fields you’d like returns. You pass a second argument in the .find function and then pass a 0 or 1 against the field names to exclude or include respectively.
CRUD
Create, Read, Update, Delete
Navigating dbs and collections
show dbs
use xyzzy
show collections
Insertion
collectionName.insertOne({...})
Note that every document has an _id automatically created for it. Guaranteed to be unique and “increasing”. Note that you can provide your own _id too.
collectionName.insertMany([{...},{...}], {“ordered”, true | false})
Insert many allows you to insert multiple documents simultaneously. Note that the optional ordered parameter determines whether it stops if it encounters duplicates.
Misc.
mongo “mongodb://cluster0-shard-00-00-jxeqq.mongodb.net:27017,cluster0-shard-00-01-jxeqq.mongodb.net:27017,cluster0-shard-00-02-jxeqq.mongodb.net:27017/aggregations?replicaSet=Cluster0-shard-0” —authenticationDatabase admin —ssl -u m121 -p aggregations —norc …. showcollections
https://university.mongodb.com/mercury/M121/2020_May_19/chapter/Chapter_1_Basic_Aggregation_-_match_and_project/lesson/5970d8b78cf2c5580eef7d8b/problem
mongo “mongodb+srv://cluster0-vafgz.mongodb.net:27017/test” —username dbUser —password PvCcAnLlJM5EIAoz
MongoDB delete example … load I. The videos dB … https://s3.amazonaws.com/edu-downloads.10gen.com/M001/2020/May/static/handouts/loadReviewsDataset.zip
sudo apt-get install -y mongodb
MongoDB screenshots folder in Photos / visit the Mongo docs for update operators and try a few (and add to notes)…also refactor the CRUD section /// updateMany useful for data cleaning
MongoDB - UpdateOne vs ReplaceOne