Search This Blog

Monday 14 April 2014

Quiz on MongoDB

[  $ ,^,$lt ,$gt,$lte,$gte, $or,$and,$regex ,$in ,$all, .DOT Notation]
cursor operators : .sort({name:-1}), .limit() ,  .skip()
.update() : [ $set , $inc]
----------------------------------------------------------------------------------------------------------
#Using $or
How would you find all documents in the scores collection where the score is less than 50 or greater than 90?
db.scores.find({$or:[{score:{$lt:50}},{score:{$gt:90}}]});
----------------------------------------------------------------------------------------------------------
What will the following query do?
db.scores.find( { score : { $gt : 50 }, score : { $lt : 60 } } );
Find all documents with score between 50 and 60
Find all documents with score greater than 50
Find all documents with score less than 60        [# CORRECT #]
None of the above
[## Q/A -REASON: being a JS Obj the first list of score will be replaced by the second @JS Promt...thus in order to achieve certain effect ,we could use $AND or $gt & $lt within the same inner query ,etc]
--------------------------------------------------------------------------------------------------------
#Querrying inside Array
Which of the following documents would be returned by this query?
db.products.find( { tags : "shiny" } );

{ _id : 42 , name : "Whizzy Wiz-o-matic", tags : [ "awesome", "shiny" , "green" ] } [# CORRECT #]
 { _id : 704 , name : "Fooey Foo-o-tron", tags : [ "blue", "mediocre" ] }
 { _id : 1040 , name : "Snappy Snap-o-lux", tags : "shiny" } [# CORRECT #]
 { _id : 12345 , name : "Quuxinator", tags : [ ] }
 ---------------------------------------------------------------------------------------------------------
 #Using $in & $all
 Which of the following documents matches this query?

db.users.find( { friends : { $all : [ "Joe" , "Bob" ] }, favorites : { $in : [ "running" , "pickles" ] } } )

{ name : "William" , friends : [ "Bob" , "Fred" ] , favorites : [ "hamburgers", "running" ] }
{ name : "Stephen" , friends : [ "Joe" , "Pete" ] , favorites : [ "pickles", "swimming" ] }
 { name : "Cliff" , friends : [ "Pete" , "Joe" , "Tom" , "Bob" ] , favorites : [ "pickles", "cycling" ] }[# CORRECT #]
 { name : "Harry" , friends : [ "Joe" , "Bob" ] , favorites : [ "hot dogs", "swimming" ] }
 -----------------------------------------------------------------------------------------------------------
#Querries with Dot[.] Notation
 Suppose a simple e-commerce product catalog called catalog with documents that look like this:

{ product : "Super Duper-o-phonic",
  price : 100000000000,
  reviews : [ { user : "fred", comment : "Great!" , rating : 5 },
              { user : "tom" , comment : "I agree with Fred, somewhat!" , rating : 4 } ],
  ... }
Write a query that finds all products that cost more than 10,000 and that have a rating of 5 or better..

 db.catalog.find({price:{$gt:10000},"reviews.rating":{$gte:5}}) [# ANSWER #]
 --------------------------------------------------------------------------------------------------------
 #Querying,Cursors
 Recall the documents in the scores collection:
{
"_id" : ObjectId("50844162cb4cf4564b4694f8"),
"student" : 0,
"type" : "exam",
"score" : 75
}
Write a query that retrieves exam -type documents, sorted by score in descending order, skipping the first 50 and showing only the next 20.

 db.scores.find({type:"exam"}).sort({score:-1}).skip(50).limit(20) [# ANSWER #]
 --------------------------------------------------------------------------------------------------------
 #Counting Results
 How would you count the documents in the scores collection where the type was "essay" and the score was greater than 90?
 db.scores.count({type:"essay",score:{$gt:90}})
  ----------------------------------------------------------------------------------
 #Updating wholesale Document
 Let's say you had a collection with the following document in it:
{ "_id" : "Texas", "population" : 2500000, "land_locked" : 1 }
and you issued the query:
db.foo.update({_id:"Texas"},{population:30000000})

{ "_id" : "Texas", "population" : 2500000, "land_locked" : 1 } 
{ "_id" : "Texas", "population" : 3000000, "land_locked" : 1 }
 { "_id" : "Texas", "population" : 30000000 } [# CORRECT #]
 { "_id" : ObjectId("507b7c601eb13126c9e3dcca"), "population" : 2500000 }  
  ----------------------------------------------------------------------------------
 .update() --$set
 For the users collection, the documents are of the form
{
"_id" : "myrnarackham",
"phone" : "301-512-7434",
"country" : "US"
}

Please set myrnarackham's country code to "RU" but leave the rest of the document (and the rest of the collection) unchanged. 
 > db.users.update({"_id" : "myrnarackham"},{$set:{"country":"RU"}}) [# ANSWER #]
   ----------------------------------------------------------------------------------
.update() --$unset
 Write an update query that will remove the "interests" field in the following document in the users collection.

    "_id" : "jimmy" , 
    "favorite_color" : "blue" , 
    "interests" : [ "debating" , "politics" ] 
}
 > db.users.update({"_id":"jimmy"},{$unset:{"interests":1}}) [# ANSWER #]
Further u can use the .find() mthd to verify the same 
> db.users.find({"_id" : "jimmy"})
{ "_id" : "jimmy", "favorite_color" : "blue" }
   ----------------------------------------------------------------------------------
 .update() -- USING $push, $pop, $pull, $pushAll ,$pullAll ,$addToSet
  db.array.update({"id:0"},{$push:{"a":55}})  [# INCORRECT #]
  db.array.update({"id":0},{$push:{a:55}})  [# CORRECT #]
-----------------------------------
 Suppose you have the following document in your friends collection:
{ _id : "Mike", interests : [ "chess", "botany" ] }

What will the result of the following updates be?
db.friends.update( { _id : "Mike" }, { $push : { interests : "skydiving" } } );
db.friends.update( { _id : "Mike" }, { $pop : { interests : -1 } } );
db.friends.update( { _id : "Mike" }, { $addToSet : { interests : "skydiving" } } );
db.friends.update( { _id : "Mike" }, { $pushAll: { interests : [ "skydiving" , "skiing" ] } } );

A> { _id : "Mike", interests : ["botany","skydiving","skydiving","skiing" ] } [# ANSWER #]
   ----------------------------------------------------------------------------------
#  .update() :-- Upserts {upsert:true}
upsert = update(if record foound) else insert the same [thts wht i make out of this.. ;-) ]
After performing the following update on an empty collection
db.foo.update({username:'bar'}, {'$set':{'interests':['cat', 'dog']}}, {upsert: true} );

What could be the state of the collection.
{ "_id" : ObjectId("507b78232e8dfde94c149949"), "interests" : [ "cat", "dog" ]}
 {"interests" : [ "cat", "dog" ], "username" : "bar" } 
 {} 
 { "_id" : ObjectId("507b78232e8dfde94c149949"), "interests" : [ "cat", "dog" ], "username" : "bar" } [# CORRECT #]
   ----------------------------------------------------------------------------------
 Multi-update  db.collection_name.update({},{$set:{}}{multi:true})
 Recall the schema of the scores collection:

{
"_id" : ObjectId("50844162cb4cf4564b4694f8"),
"student" : 0,
"type" : "exam",
"score" : 75
}
Give every document with a score less than 70 an extra 20 points.
> db.scores.update({score:{$lt:70}},{$inc:{score:20}},{multi:true}) [# ANSWER #]
   ----------------------------------------------------------------------------------
.remove()  works on any collection , similar to .find() method...ie unless you specify wch document to remove inside a collection it will remove all one-by-one..
 Recall the schema of the scores collection:
{
"_id" : ObjectId("50844162cb4cf4564b4694f8"),
"student" : 0,
"type" : "exam",
"score" : 75
}
Delete every document with a score of less than 60. 
 > db.scores.remove({score:{$lt:60}})
   ----------------------------------------------------------------------------------
# getLastError
 d.runCommand({getLastError:1}) --will let u know whether the last DB-Operation failed or ws successfull
   ----------------------------------------------------------------------------------

1 comment:

  1. I like this article gives a lot of information. This site is found here y0 running fred 2

    ReplyDelete