5. MongoDB : Work with (Database, Collection and Documents)
********************************************************************db.createCollection(...)
db.items.insert(...); //{....}
db.items.find(...); // {....}, $lt, $lte, $gte, $in, $or, /..., $slice, $elemMatch,
db.items.update(...) // {....}. $set
********************************************************************
show dbs; //show all existing db's
use test; //to switch to test db, if test does not exist then it creates and then switch
show collections;
db.createCollection("items");
//insert ####################################################
db.items.insert({"name":"Sumit","courses":["mongodb","AWS","GCP"]}); //create document inside collection
db.items.find();
db.items.find({"name":"Sumit"});
db.items.find({"name":"Sumit"}).pretty();
//Bulk insert, if we dont give _id -> then it mongodb takes some unique id
var bulk = db.items.initializeOrderedBulkOp();
bulk.insert({_id:1, item:"pen", available:true, soldQty:700});
bulk.insert({_id:2, item:"pencil", available:false, soldQty:900});
bulk.insert({_id:3, item:"books", available:true, soldQty:700});
bulk.execute();
var bulk = db.items.initializeUnorderedBulkOp();
bulk.insert({_id:4, item:"pen", available:true, soldQty:700});
bulk.insert({_id:5, item:"pencil", available:false, soldQty:900});
bulk.insert({_id:6, item:"books", available:true, soldQty:700});
bulk.execute();
db.items.find();
db.items.find({available:true}).limit(5); //limit(5) = row=5
db.items.findOne();
db.items.findOne({available:true});
db.items.find({available:true}).pretty();
db.items.find({soldQty: {$lt:800}}); //lessthan 800
db.items.find({soldQty: {$lte:800}, available:true}); //lessthan or equal to
db.items.find({available:{$in:[true,false]}}); //IN
db.items.find({available:true,soldQty:{$lt:900}}); //AND or ,
db.items.find({ $or: [{available:true},{soldQty:700}] }); //OR condition, note: within that mention in {},{}
//regular expression ####################################################
//regular expression match, eg: in website search_______ anything in website
db.items.find({item:/pe/i}); //caseinsensitive //search anything which has 'pe' or 'i' in it
db.items.find({item:/Pen?/i}); //caseinsensitive //search anything which has 'pe' or 'i' in it
//array serch ####################################################
db.itesm.find({country_codes:5}); //if country_codes is array - then search for '5' inside that.
db.itesm.find({country_codes.0:5});
//projection quries. (Only for Arrays : $elemMatch, $slice)
//here country_codes is array.
db.items.find({_id:5},{country_codes:{$slice:2}}); //shows only 1st 2 array elements,
note: if you give {$slice:-2} then it shows last 2 elements of array.
db.itesm.find({country_codes:{$elemMatch:{$gte:3,$lte:6}}}); //between 3 and 6, inside an array elemMatch
//javascript inside query ####################################################
//javascript inside query - using "$where"
db.items.find({"$where":function().......});
db.items.find(({"$where": "this.x + this.y == 10"});
db.items.find(({"$where": "function() {return this.x + this.y == 10;}"});
//cursor, ####################################################
//cursor, light way of handling on dataset query.
var cursor = db.collection.find();
while(cursor.hasNext()) {
....obj= cursor.next();
.....
}
db.c.find.skip(3);
db.c.find().soft({username:1,age:-1})
//update document where item=Book
//Note: below taken is different example of "items" collection.
db.items.update({item:"Book"},{$set: {category: 'NoSQL', details: {ISDN: "1234", publisher:"XYZ"}}});
//update an embedded field
db.items.update({item: "Pen"},{$set: {"details.model":"14Q2"}});
//update multiple documents. (multi:true)
db.items.update({item: "Pen"},{$set:{catogory:"stationary"},$currentDate:{lastModified:true}},{multi:true});
//UPDATE RECORD ####################################################
//update document where item=Book
//Note: below taken is different example of "items" collection.
db.items.update({item:"Book"},{$set: {category: 'NoSQL', details: {ISDN: "1234", publisher:"XYZ"}}});
//update an embedded field
db.items.update({item: "Pen"},{$set: {"details.model":"14Q2"}});
//update multiple documents. (multi:true)
db.items.update({item: "Pen"},{$set:{catogory:"stationary"},$currentDate:{lastModified:true}},{multi:true});
No comments:
Post a Comment