Mongo DB Notes

Table of contents
Mongo DB ?
Mongodb manages the conversion of JSON-BSON and BSON-JSON automatically.It is case sensitive and not necessary to use semi-coolon “;” .
What table is for RDBMS is collections for mongodb.One record in a collection is called as Document. What row is for RDBMS is document for mongodb.
Commands :
Write mongosh in your terminal, and it will open the mongodb console.
show databases; Or show dbs; - To list all the databases.
use name_of_database; - To select a particular db and start querying on it.
show collections; - To print all the collections.
Make sure, we run show collections; command after executing use some_db otherwise we wont be having db selected.
db.collectionname.find(); - To print all the documents of a collection.
use new_database_name; - To create a new database.
db.createCollection("name_of_the_collection") - To create a new collection.Make sure we execute this command after use some_db_name.
db.collectionName.insertOne({key1: value1, key2: value2 ....}) - To add a new record.
db.collectionName.insertMany([ { key1: value1, key2: value2 }, { key1: value1, key2: value2, key3: value3, . . . }, { key5: value5,key7: value7, . . } ]);- To add multiple records at once.
We can simply use [] to add an array. For example,
db.collectionName.find().count();- This return total no. of document.
db.collectionName.find().limit(no_of_records_to_fetch);- To get a certain number of documents.
db.collectionName.find().skip(5).limit(3);- We can set an offset using the skip function. Using the skip function we can skip some records and then start fetching records post the skip.
db.collectionName.find({key1: value1, key2: value2});- To filter the records, we can pass an object in the arguments of the find function, where we can add out conditions.
Projections- If we want to not get all the properties of the JSON, and instead get some specific key-value pairs, this process is called Projection.
db.collectionName.find({filter1: value1...}, {property1: true, property2: true....});- This how we do projection.
db.collectionName.deleteOne({filter1: value1, filter2: value2..});- Delete one document.
db.collectionName.deleteMany({filter1: value1, filter2: value2..});- to delete all documents that match a specific filter.
db.collectionName.updateOne({filter1: value1}, {$operator: {key1: value1, key2: value2...}});- To Update single record. where $operator is: $set,$inc,etc.
db.users.updateMany( { country: "India" }, { $set: { verified: true } } );- to update all documents that match a given filter.
db.collectionName.distinct("key");- To get distinct values.The
key
is the field name for which you want to get distinct (unique) values from the collection.db.collectionName.find({property: {$operator: value}});- This is used to filter documents in MongoDB using comparison or logical operators. Operators like $ne -> not equals $eq -> equals $lt -> less than $lte -> less than or equal to $gt -> greater than $gte -> greater than or equal to $and -> logical and $or -> logical or $not -> logical not $nor -> logical nor $in -> for checking in the array $nin -> not in the array.
db.weather_data.find({type: 'SAO'}).explain("executionStats");- We can analyse our queries using the explain function.
Indexing is a mechanism, used for faster search.
db.collection.createIndex({ fieldName: 1 });- To create index.
db.collection.dropIndex({ fieldName: 1 });- To delete index.
How to import sample DB in MongoDB?
There are two method: i) MongoDBCompass ii) CMD or Terminal
Subscribe to my newsletter
Read articles from Unknown Truth directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
