Sunday, June 18, 2017

MongoDB Commands on Windows

1.cd mongodb\bin
2.mongod --directoryperdb --dbpath c:\mongodb\data\db --logpath c:\mongodb\log\mongo.log -- logappend --rest --install
3.net start MongoDB
4.mongo
5.show dbs
6.use mycustomers
7.db
8.db.createUser(
   {
     user: "accountUser",
     pwd: "password",
     roles: [ "readWrite", "dbAdmin" ]
   }
)
9. db.createCollection("customers");
10.show collections
11. db.customers.insert({first_name:"John", last_name:"Doe"});
12.db.customers.find();
13.db.customers.insert([{first_name:"John", last_name:"Johnson", gender:"female"},{first_name:"Steven", last_name:"Smith"}]);
14.db.customers.find().pretty();
15.db.customers.update({first_name:"John"},{first_name:"John", last_name:"Doe", gender:"Male"});
16.db.customers.update({first_name:"Steven"},{$set:{gender:"male"}});
17.sb.customers.update({first_name:"Steven"},{$set:{age:45}});
18.db.customers.update({first_name:"Steven"},{$inc:{age:5}});
19.db.customers.update({first_name:"Steven"},{$unset:{age:1}});
20.db.customers.update({first_name:"Mary"},{first_name:"Mary", last_name:"Samson"},{upsert:true});// if Mary not exist, insert into db;
21.db.customers.update({first_name:"Steven"},{$rename:{"gender":"sex"}});
22.db.customers.remove({first_name:"Steven"});
23.db.customers.remove({first_name:"Steven"},{justOne:true});
24.db.customers.find({first_name:"Sherron"});
25.db.customers.find({$or:[{first_name:"Sherron"},{first_name:"Steven"}]});
26.db.customers.find({gender:"male"});
27.db.customers.find({age:{$lt:40}});
28.db.customers.find({"address.city":"Boston"});
29.db.customers.find({memberships:"mem1"});//find array elements
30.db.customers.find().sort({last_name:1});//ascending order
31.db.customers.find().count();
32.db.customers.find(gender:"male").count();
33.db.customers.find().limit(4);
34.db.customers.find().limit(4).sort({last_name:1});
35.db.customers.find().forEach(function(doc){print("Customer Name:"+doc.first_name)});





No comments:

Post a Comment