Showing posts with label MONGODB. Show all posts
Showing posts with label MONGODB. Show all posts

Thursday, October 28, 2021

Mongodb general commands

 //cheatsheet


/* key words */

collection = table



/* for databses */


view all databases

-show dbs


create a new or switch databases

-use <databse_name>


view current database

-db


delete database

-db.dropDatabase()



/* for collections */


Show Collections

-show collections


create a collection name 'comments'

-db.createCollection('comments')


delete collections

-db.<collection_name>.drop()


/* mongodb command for rows*/


Show All Rows In a Collection

-db.<collection_name>.find()

-db.<collection_name>.find().pretty() //for preetyfied view

-db.<collection_name>.findOne() // for finding 1st row


Insert One Rows

-db.<collection_name>.insert({'name':'Gyanendra','lang':'javaScript','age':'24'})


Insert Many Rows

-db.<collection_name>.insertMany([{'name':'Gyanendra','lang':'javaScript','age':'24'},{'name':'Gyanendra','lang':'javaScript','age':'24'},{'name':'Arvind','lang':'javaScript','age':'24'},])


Search In a Mongodb Database

-db.<collection_name>.find({'name':'Arvind'})


Limit The Number of Rows In Output

-db.<collection_name>.find().limit(10)


Count Rows

-db.<collection_name>.count()

or 

-db.<collection_namee>.limit(3).count()

//these two has same output because limit only limits the output data not the whole data



JavaScript Functions as JavaScript Variables

In Javascript instead of declaring and executing a function in two different steps. for example step 1 -  function add(a,b){return a+b;} ste...