Monday 28 March 2016

MongoDB sqls.

Homework: Homework 1.2

C:\mongodb\mongodb-bin\bin\mongod --dbpath C:\mongodb\data

Download the handout. Take a look at its content.

Now, import its contents into MongoDB, into a database called "pcat" and a collection called "products". Use the mongoimport utility to do this.

When done, run this query in the mongo she


mongoimport /d pcat  /c products /file  C:\mongodb\poductschema.json /type json

2016-03-19T22:08:58.944+0800    connected to: localhost
2016-03-19T22:09:00.591+0800    imported 11 documents


var c = db.products.find( { }, { name : 1, _id : 0 } ).sort( { name : 1 } ); while( c.hasNext() ) { print( c.next().name); }

Endless loop:

while (1) {db.t1.update({}, {$inc:{c1: "heloo how are you"}},false,true);}

for (var i = 0; i < 100; i++) { db.t3.insert({c1: "Inserting in loop" , c2 : i}) }


db.t3.find({c2 : { $gt : 20 }}).explain().queryPlanner


http://www.thegeekstuff.com/2014/03/mongodb-books/



---Homework 2:

>  var bulk=db.items.initializeUnorderedBulkOp();
> bulk.insert({x:'dgdg', y: "heloo hruddfdfdsf"})
> bulk.insert({x:'dgdg', y: "heloo hruddfdfdsf"})
> bulk.insert({x:'dgdg', y: "heloo hruddfdfdsf"})
> bulk.insert({x:'dgdg', y: "heloo hruddfdfdsf"})
bulk.execute()



1) Insert js script
C:\mongodb\mongodb-bin\bin>mongo --shell pcat C:\mongodb\M102\homework_2_1\homework2.js
> homework.a()
3.05

2)
b=pcat.products

myobj=b.findOne({"_id" : ObjectId("507d95d5719dbef170f15c00")})

myobj.term_years=3
b.update( {_id : myobj._id }, myobj)

myobj.limits.sms.over_rate=0.01
b.update( {_id : myobj._id }, myobj)
> homework.b()
0.050.019031

3) Product where voice is exists, just count they need:

b.find({"limits.voice" : {$exists:true}})