Вы находитесь на странице: 1из 5

> show dbs local (empty) test (empty) > use newbdR switched to db newbdR > show dbs

local (empty) test (empty) > db newbdR > show dbs local (empty) test (empty) > db newbdR > use newdbR switched to db newdbR > show dbs local (empty) test (empty) > db newdbR > emp1={name:"Ram",age:"21",sex:"male"} { "name" : "Ram", "age" : "21", "sex" : "male" } > emp2={name:"laxman",age:"22",sex:"male"} { "name" : "laxman", "age" : "22", "sex" : "male" } > newdbR.Test.insert(emp1); Tue Mar 04 15:20:13 ReferenceError: newdbR is not defined (shell):1 > > show dbs local (empty) test (empty) > use newbdR switched to db newbdR > show dbs local (empty) test (empty) > db newbdR > show dbs local (empty) test (empty) > db newbdR > use newdbR switched to db newdbR > show dbs local (empty) test (empty) > db newdbR > emp1={name:"Ram",age:"21",sex:"male"} { "name" : "Ram", "age" : "21", "sex" : "male" } > emp2={name:"laxman",age:"22",sex:"male"} { "name" : "laxman", "age" : "22", "sex" : "male" } > newdbR.Test.insert(emp1); Tue Mar 04 15:20:13 ReferenceError: newdbR is not defined (shell):1 >

> use newdbR switched to db newdbR > emp1={name:"Ram",age:"21",sex:"male"} { "name" : "Ram", "age" : "21", "sex" : "male" } > emp2={name:"laxman",age:"22",sex:"male"} { "name" : "laxman", "age" : "22", "sex" : "male" } > db.newdbR.Test.insert(emp1);

emp8={name:"anita1",age:"26",sex:"female"} emp9={name:"anita2",age:"26",sex:"female"} emp10={name:"anita3",age:"26",sex:"female"} emp11={name:"anita4",age:"26",sex:"female"} emp12={name:"anita5",age:"26",sex:"female"} emp13={name:"anita6",age:"26",sex:"female"} emp14={name:"anita7",age:"26",sex:"female"} emp15={name:"anita8",age:"26",sex:"female"} emp16={name:"anita9",age:"26",sex:"female"} emp17={name:"anita10",age:"26",sex:"female"} emp18={name:"anita11",age:"26",sex:"female"} emp19={name:"anita12",age:"26",sex:"female"} emp20={name:"anita13",age:"26",sex:"female"} emp21={name:"anita14",age:"26",sex:"female"} emp22={name:"anita15",age:"26",sex:"female"} emp23={name:"anita16",age:"26",sex:"female"} emp24={name:"anita17",age:"26",sex:"female"} emp25={name:"anita18",age:"26",sex:"female"} emp26={name:"anita19",age:"26",sex:"female"} emp27={name:"anita20",age:"26",sex:"female"} emp28={name:"anita21",age:"26",sex:"female"} emp29={name:"anita22",age:"26",sex:"female"} db.TestTable.insert(emp8); db.TestTable.insert(emp9); db.TestTable.insert(emp10); db.TestTable.insert(emp11); db.TestTable.insert(emp12); db.TestTable.insert(emp13); db.TestTable.insert(emp14); db.TestTable.insert(emp15); db.TestTable.insert(emp16); db.TestTable.insert(emp17); db.TestTable.insert(emp18); db.TestTable.insert(emp19); db.TestTable.insert(emp20); db.TestTable.insert(emp21); db.TestTable.insert(emp22); db.TestTable.insert(emp23); db.TestTable.insert(emp24); db.TestTable.insert(emp25); db.TestTable.insert(emp26); db.TestTable.insert(emp27); db.TestTable.insert(emp28); db.TestTable.insert(emp29);

show collections -->> to see all the tables db.newDBR.TestTable.find(); --->>> returns all documents in in the collection db.newDBR.TestTable.findOne(); ---->>> returns first document in the collection -->> use of javascript in mongoDB var c=db.newDBR.TestTable.find(); while(c.hasNext()) printjson(c.next()); printjson(c.toArray().length); ---->>> converts var c into array and returns its length printjson(c[4]); -->> returns the element at 4th index of array c db.TestTable.find().limit(5);-->> we can limit our result set using limit and 5 is the no. of documents returned in the resultset to insert data in a table dynamically--->>> for(var i=1;i<=25;i++) db.TestTable1.insert({Age:i});

javascript ft. to insert data in a table using for loop > mal ... ... ... ... ... ... function insertData(dbName, colName, num) { //dbName,colName,num--->>are for arguments var col=db.getSiblingDB(dbName).getCollection(colName); for(var i=0;i<num;i++) { col.insert({Count:i}); } print(col.count()); }

calling a ft by passing commandline arguments > insertData("newDBR","TestTable2",30); 60 ==to apply conditon db.TestTable3.find({age:{$lt:24}}).sort({age:1}); sort() is used to apply order by clause-->> sort({age:1}) to sort in ascending o rder if we want to apply some where condition then apply a condition like this find({ age:{$lt:24}}) $lt-->> less than $gt-->> greater than another method to insert data in a table-->>

db.TestTable3.insert({_id:100,name:"XYZ", age:23, bg:"O-"}); to see all column data except one column-->>>>>> db.TestTable3.find({age:{$lt:24}},{_id:0}); retrieving data for a particular column name and we get _id column by default--->>>> db.TestTable3.find({age:{$gt:24}},{name:1}); sorting above query result on _id basis--->>> db.TestTable3.find({age:{$gt:24}},{name:1}).sort({_id:1}); this query shows the result of name column and block the _id column-->>> db.TestTable3.find({age:{$gt:24}},{name:1,_id:0}); this query sorts above query result on the basis of _id--------->>>>> db.TestTable3.find({age:{$gt:24}},{name:1,_id:0}).sort({_id:1}); when we insert data having same _id then it gives us error---->>> > db.TestTable3.insert({_id:100, name:"Zebra",age:25,bg:"A-"}); E11000 duplicate key error index: newDBR.TestTable3.$_id_ dup key: { : 100.0 } to create a new table Product---->>>> prod1 prod2 prod3 prod4 prod5 prod6 prod7 = = = = = = = {_id:"P001",Name:"Mobile",Price:5000,Qty:2} {_id:"P002",Name:"Laptop",Price:30000,Qty:1} {_id:"P003",Name:"Marker",Price:50,Qty:10} {_id:"P004",Name:"Mouse",Price:500,Qty:5} {_id:"P005",Name:"Computer",Price:35000,Qty:1} {_id:"P006",Name:"Shoes",Price:2500,Qty:3} {_id:"P007",Name:"Shirt",Price:800,Qty:4}

db.Product.insert(prod1) db.Product.insert(prod2) db.Product.insert(prod3) db.Product.insert(prod4) db.Product.insert(prod5) db.Product.insert(prod6) db.Product.insert(prod7) var prod8 = {_id:"P008",Name:"TV",Price:{Price:15000,Tax:1200}} var prod9 = {_id:"P009",Name:"Bike",Price:{Price:50000,Tax:6000}} var prod10= {_id:"P010",Name:"Data Card",Price:{Price:2000,Tax:200}} db.Product.insert(prod8) db.Product.insert(prod9) db.Product.insert(prod10) db.Product.find() db.Product.find({'Price.Price':{$lt:15000}})

db.Product.find({'Price.Price':{$gt:15000}}) putting OR condition-->> db.Product.find({ $or : [{ 'Price.Price' : {$gt:5000} },{'Price.Tax' : {$gt : 50 0}}] }) putting AND condition-->> db.Product.find( { $and : [{ 'Price.Price' : 15000},{'Price.Tax' : 1200} ] }) using not equal to operator--->>> COMPARISON OPERATOR ------------------$ne== not equal to operator, $gt==greater than operator, $gte==greater than or equal to operator, $lt==less than operator, $lte==less than or equal to operator, $in==matches any of the values that exists in an array specified in the query, $nin==opposite to $in operator LOGICAL OPERATOR ---------------$or==returns all docs that match the conditions of either clause $and==returns all docs that match the condition of both the clause $not==returns docs that do not match the query $nor==returns all the docs that do not match both the clause ELEMENT OPERATOR ---------------$exists==matches docs that hve the specified field $type== select docs if a field is of the specified type db.TestTable3.find({age:{$ne:21}}); db.TestTable3.find({age:{$in:[21,25,31]}});

Вам также может понравиться