MongoDB (CRUD)

NOSQL:-[written in c++]

table    collection[no structure schema]

row     document

column  field

table join embeded document



 open cmd and type mongo to run mongodb database.

show dbs

use databasename

show collections

db.createCollection("mycollection")
db.dropDatabase()//make sure u have done use database then drop
db.COLLECTION_NAME.drop()

Commands:-------

use databasename.....to create database if present it will switch to database otherwise it will create new.(database name is Case Sensitive)


To remove database:   first check by using db  in which databse you are 

then db.dropDatabase();


Then at least one collection(table) we have to create for :-

show dbs(show databases) command to show database.

   or     show databases;

//minimum one collection is mandatory[ one table is mandatory]


creating and Dropping collection: 

db.createCollection("name of collection"); it has to be in "" double code

db.collectionName.drop();


Data Types:--

Bson                           Object

Json                           Null

Integer                        Date

Boolean                     Timestamp

Double                       ObjectId

Arrays                         code



insert will  be in form of BSON

binary extended json [it supprots addtional data typess date timestamp object id]


.......insertOne({}) and insertMany....([{},{}])

> db.record.insertMany([{'rollno':2,'name':'boby'},{'rollno':3,'name':'raj'}]);

db.collection(tablename).insertOne({}}


_id  24 character  its like primary key


type... db....for checking active database.

show collections...to find tables in the database....

db.tablename(collectionname).find()...for printing data in database..

db.tablename.find().pretty()...for printing data in proper form..

quit()../ctrl+c to quitting from Mongo.


Read/Fetch:---

db.collection.find(query,projection)....

db.collection.find({name:"gaurav"}).pretty().... finding data which has name gaurav

db.collection.find({name:"gaurav"},{name:1}).pretty()...finding data which has name gaurav only that name data...but id will include here..

db.collection.find({name:"gaurav"},{name:1,_id:0}).pretty()...

finding data which has name gaurav only that name data...but id will not include ..

db.collection.find().limit(1).pretty()...... find 1 st data....

db.collection.find().limit(1).skip(1).pretty()...find 2nd data after skip 1st/skip can skip any no of data

findOne():-find first document in collection

findOneAnd Replace:-({key1:value,"key2":value},<replacemnet>)

findOneAndDelete:({"key1":value,"key2":value})

find with name and rollno:

db.collectionname.find({name:"",rollno: })


update in mongo db:-

updateOne():-db.collection_name.updateOne(<filter>,<update>)

updateMany():-db.collection_name.updateMany(<filter>,<update>)

db.example.updateOne({name:"gaurav"},{$set:{name:"raj"}})....replaced name gaurav with name raj.


Delete in mongodb:-

deleteMany():-db.Collectionname.deletemany(deletion creitera)//if criteria not given all data will be removed... 

db.record.deleteMany({name:'raj'})








Comments

Popular posts from this blog

interview questions js[ Anurag Singh ProCodrr]

reactnative_creation