0% found this document useful (0 votes)
5 views

mongodb

This document provides a guide on basic MongoDB commands for database and collection management, data insertion, querying, updating, and aggregation. Key commands include connecting to the database, creating collections, inserting multiple records, and using various operators for filtering data. It also covers how to limit and skip records, count documents, and perform aggregations to calculate totals based on specific criteria.
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

mongodb

This document provides a guide on basic MongoDB commands for database and collection management, data insertion, querying, updating, and aggregation. Key commands include connecting to the database, creating collections, inserting multiple records, and using various operators for filtering data. It also covers how to limit and skip records, count documents, and perform aggregations to calculate totals based on specific criteria.
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

* mongodb://localhost:27017 --> to connect to mongodb

* show dbs --> to show dbs

* use employeeDb --> it will create db if there is exixting db on same name it will
switch to that db

* db.createCollection("<name of the name collection>") --> it will create


collection.

** The collection name should be same as db name


Ex:
---
dbname : employeedb
collection name: employee

*db.employee.insertMany([{empname:"anish",salary: 230000,city:"Hyd"},
{empname:"Rama",salary: 23000000,city:"Hyd"}]) --> To insert data

* db.employee.find() --> To fetch records

*db.employee.upadte({empname:"Anish"},{$set:{name:"seetha"}) -->update

* db.employee.remove({},{empname:""})

* To print only empname:


db.employee.find({empname:1})

** Here 1 --> to show data


0 --> to hide the data

** $gt --> Greater than


** $lt --> Less Than
** $gte --> Greater than or equal to
** $lte --> Less than or equal to
** $ne --> Not equal to

*db.employee.find({salary:{$get:30000}},{empname:1,salary:1,_id:0})

--> it will find the details more than 300000 and id should be hide.

* If we want only two records means


db.employee.find().limit(2)

* If we want to skip two records means


db.employee.find().skip(2)

* db.employee.find({city:"Chennai"}).count() --> To know the count.

* db.employee.aggregate([{$group:{_id:"$city",Totalsalary:{$sum:"$salary"}}}]) --
>To get total salary

* db.employee.aggregate([{$match:{empname:"sam"}},{$project:
{empname:1,age:1,city:1}}]) -->To find the matching person.

db.employee.aggregate([{$match:{department:"IT"}},{$project:
{empname:1,age:1,city:1,department:1}}])

index quick retrieval of data


sorting
pipeline

You might also like