CRUD stands for Create Read Update Delete. Those are fundamental operations used to manipulate data stored in any type of database, SQL and NoSQL.
We must first take a look at some basic commands with database and collection before diving in CRUD documents.
Show all databses
show dbsor with anyone going from MySQL, use the following command but without semicolon at the end
show databases
Check current database
db
Change database
use <databasename>
Show colletions in a database
show collections
Create Collection
Create a new "myMovies" collection in "movieScratch" database.
Insert documents
Insert many documents unordered
db.<CollectionName>.insertMany( [ { "key1": "value11" "key2": "value12" }, { "key1": "value21" "key2": "value22" } ], { ordered: false } )
Take a look at the result: The third document cannot be inserted due to duplicated key error with the first document.
Read documents
Read all documents in a collection with find()
db.<CollectionName>.find()
Count all documents in a document with count()
db.<CollectionName>.count()
Count and filter specific documents in a collection.
Find movies that rated as "PG" and have exactly 10 nominations in "movieDetails" collection of "movie" database.
db.movieDetails.find({"rated":"PG","awards.nominations":10})
To be continued...
Let's learn by blogging, it REALLY takes our times but it is worth it, at least for me.