Skip to main content

Posts

Showing posts with the label mongodb tutorial

How to create database in MongoDB ?

MongoDB   do not provide command to create new database directly. Do not need to create Mongo database manually. It is created when you save the documents into the defined collection and database . The MongoDB  contains “ db.createCollection()” and is use to create collection manually but not the new database. I’m going to share the following steps to create collections and database. Show all Databases  Command When the below show query executed that it return two databases, one is “ admin ” and other is “ local ”. > show dbs admin   0.03125GB local   (empty) Define a database name  Command > use my_new_db switched to db my_new_db > show dbs admin   0.03125GB local   (empty) When the above queries executed, the database “ my_new_db ” is not created yet. Save Command In the last, define a collection named “ customers ” and save a dummy document inside the custome...

Mongodb advantages and disadvantages

Advantages of MongoDB o     MongoDb is document database. o     Lightening is very fast. o     MongoDb provides ACID properties at the document level. o     MongoDb handled failover mechanism is automatically. o     MongoDb supports common authentication mechanisms like LDAP, AD. o     Auto sharding because MongoDB enables horizontal scalability. o     Replication is very easy. o     You can perform rich queries. Disadvantages o     Not support joins operation. o     Not support transaction . o     No single server durability. If query is crashes   that time you lost your all data and after repair you lost approx. 50 to 60% data. o     Indexes take up a lot of RAM (RAM limitation). o     Memory limitations. “ MongoDB  lacks some features of rel...

mongodb if exists update else insert

In MongoDB , use upsert is equal to true ( upsert: true) for check if items is exists then update else insert. By default upsert  is equal to false. Example as db . Users . update ({ name : 'Anil' }, { name : 'Anil' , age : 30 },               { '$inc' :{ 'sequence' : 1 }}, { 'upsert' : true })

MongoDb aggregation if else

Syntax: { $cond : [<expression>, <true>, <false>] } Example as given below db . Customers . aggregate ( [     { "$project": {         "name": 1,         "customer": {             "$cond": {                 "if": { "$eq": [ "$Age", 25 ] },                 "then" : "YG" ,                 "else" : {                     "$cond" : {                         "if" : { "$eq" ...

117 Recommend MongoDB Tutorial for Beginners [MongoDB]

MongoDB Developments a) What is MongoDB? b) Why should you use MongoDB? c) What's New in MongoDB? d) Are MongoDB _id (ObjectID) unique? e) Why MongoDB is different from SQL Server? MongoDB Advantages Disadvantages MongoDB Installation MongoDB Database a) Create Database b) Rename, Backup and Restore Database c) DROP Database MongoDB Collection a) Create Collection b) Change Collection Name c) DROP Collection Join Support a) Does mongodb support join? b) What is Join in MongoDB? MongoDB Conditional Operations a) Mongodb if else Statement b) Mongodb Sort c) Mongodb update if else d) Mongodb if exists update else Insert e) Mongodb aggregation if else f) MongoDB $and and $or Operators g) MongoDB Date format h) MongoDB for loop insert update i) MongoDB switch expression MongoDB Text Search a) Text Search Operations b) Text Indexes Data Models a) Introduction b) Document Validation MongoDB CRUD Conc...

How to sort a collection by date in MongoDb?

In MongoDb , different ways of sorting can perform on a collection and you can use 1 for ‘asc’|ascending and -1 for ‘desc’|descending. Syntax: db . collection . find (). sort ( { column1: 1 or - 1 [, column2: 1 or -1] }); For example, db . customers . find (). sort ({ CreatedDate: 'desc' }). toArray ( function ( error ,   items ) {});                                                             OR db . customers . find (). sort ( 'CreatedDate' , 'desc' ). toArray ( function ( error ,   items ) {});                            ...

Mongodb if else statement

According to MongoDb  docs, $cond "evaluates a Boolean expression to return one of the two specified return expressions". Syntax: { $cond : { if: < expression >,                      then: < true >,                     else: < false >        } } OR { $cond : [<expression>, <true>, <false>] } Example for MongoDb if else statement db . customers . aggregate (    [{          $project:            {              C_Id: 1,              C_Type:     ...