MongoDb Command
We will execute all mongodb command in One database which name is “ecom_db” and Collection will be “employee” with below fields:
name ,age ,gender ,department,city ,salary status
Start work with mongo db with command type in cmd
- Show all database in cmd
> show dbs;
- How to create database in mongod.
Use dbname : it will create new database if it does not exist, otherwise it will return the existing database .and also used to switched to dbname .means if you want to change the current database.
> use ecom_db
- check current Selected database
> db
Note : without “use ecom_db ” command it shows default database name as “test”.
- Create Collection in database
db.createCollection("employee")
Note : if you don’t create collection it automatically create at insert time.
- How to check created collection
> show collections
- insert data in collection.
There are 2 methods to insert documents into a MongoDB database. a). insertOne() b). insertMany()
> db.employee.insertOne({name:"Sandip",age:20,gender:"Male",department:"HR",city:"nerul",salary:20000 ,status:"active"});
- insert many data in collection at a time. (its 20 data object collection for further use)
db.employee.insertMany([ {name: "Sandip", age: 20, gender: "Male", department: "HR", city: "Nerul", salary: 20000, status: "active"}, {name: "Anjali", age: 25, gender: "Female", department: "Finance", city: "Mumbai", salary: 30000, status: "active"}, {name: "Rajesh", age: 30, gender: "Male", department: "IT", city: "Pune", salary: 40000, status: "inactive"}, {name: "Priya", age: 28, gender: "Female", department: "Marketing", city: "Delhi", salary: 35000, status: "active"}, {name: "Amit", age: 35, gender: "Male", department: "Sales", city: "Bangalore", salary: 45000, status: "active"}, {name: "Neha", age: 22, gender: "Female", department: "HR", city: "Navi Mumbai", salary: 25000, status: "inactive"}, {name: "Vikram", age: 40, gender: "Male", department: "IT", city: "Chennai", salary: 60000, status: "active"}, {name: "Sita", age: 27, gender: "Female", department: "Finance", city: "Hyderabad", salary: 32000, status: "active"}, {name: "Karan", age: 33, gender: "Male", department: "Marketing", city: "Ahmedabad", salary: 38000, status: "inactive"}, {name: "Riya", age: 24, gender: "Female", department: "Sales", city: "Kolkata", salary: 29000, status: "active"}, {name: "Rahul", age: 29, gender: "Male", department: "HR", city: "Jaipur", salary: 31000, status: "active"}, {name: "Pooja", age: 26, gender: "Female", department: "IT", city: "Surat", salary: 34000, status: "inactive"}, {name: "Suresh", age: 38, gender: "Male", department: "Finance", city: "Lucknow", salary: 50000, status: "active"}, {name: "Tina", age: 21, gender: "Female", department: "Marketing", city: "Indore", salary: 23000, status: "active"}, {name: "Deepak", age: 31, gender: "Male", department: "Sales", city: "Nagpur", salary: 42000, status: "inactive"}, {name: "Nisha", age: 23, gender: "Female", department: "HR", city: "Bhopal", salary: 27000, status: "active"}, {name: "Manoj", age: 36, gender: "Male", department: "IT", city: "Visakhapatnam", salary: 55000, status: "active"}, {name: "Kavita", age: 30, gender: "Female", department: "Finance", city: "Coimbatore", salary: 36000, status: "inactive"}, {name: "Ramesh", age: 34, gender: "Male", department: "Marketing", city: "Mysore", salary: 39000, status: "active"}, {name: "Geeta", age: 29, gender: "Female", department: "Sales", city: "Vadodara", salary: 31000, status: "active"} ]);
- how to check or find inserted data
db.employee.find()
- how to display only specific field of document . (projection: 1 means true, 0 means false)
db.collection.find({}, {field1: value2, field2: value2, ..})
display only name and salary so command will be like below :
db.employee.find({},{name:1,salary:1});
name and salary without _id
> db.employee.find({},{name:1,salary:1,_id:0});
- count number of document in a collection
> db.employee.find().count();
- show limited number of data
> db.employee.find().limit(5);
> db.employee.find({},{name:1,_id:0}).limit(5);
-
We can Skip a specified number of documents in the result set.
> db.employee.find().skip(5).limit(5);
it will Skip the first 5 users and return the next 5
-
show data in Assending and dessending Order using sort() method : Assending order : 1 and dessending order : -1
> db.employee.find().sort({name:1});
> db.employee.find().sort({name:1}).limit(5);
> db.employee.find().sort({name:1}).skip(5).limit(5);
- How to update data in momngoDB. 1.updateOne() 2.updateMany() 3.replaceOne()
> db.employee.updateOne({name:"Anjali"},{$set:{age:26}})
we are updating "age " where name is "Anjali".
- how to delete single data in mongodb.
- jkhgf
Last Updated on : 6th Jan 2025 17:55:04 PM
Latest Update
- JavaScript Interview Question MongoDb Command Curd Operation Node MonogoDb Express EJSC Practice Question Example Data Structure with CPPCurd Operation with static array in node js and ejs filejava practice questionCurd operation with localstorage in javascriptComplete curd operation in react with json server and axios and router-domCPP Practice Question Java Pattern Program