Sunday, 1 September 2013

mongodb hacks

connect to mongod server using ssh, 

use mongo client to connect to server, choose database;
> use recommendation;
switched to db recommendation

> db.getCollectionNames()
[ "movies", "system.indexes" ]

> show collections;
movies
system.indexes

Indexing
---------------------------------------------

> db.movies.getIndexes()
[
{
"v" : 1,
"key" : {
"_id" : 1
},
"name" : "_id_",
"ns" : "recommendation.movies"
}
]

> db.movies.stats()
{
"ns" : "recommendation.movies",
"count" : 967696,   //100K
"size" : 232274176, // 232.274 M
"avgObjSize" : 240,
"storageSize" : 335,900672,
"numExtents" : 14,
"nindexes" : 1,
"lastExtentSize" : 92585984,
"paddingFactor" : 1,
"systemFlags" : 1,
"userFlags" : 1,
"totalIndexSize" : 32,818464,
"indexSizes" : {
"_id_" : 32,818464
},
"ok" : 1
}


[1.1] filter/ projection
db.movies.find({"title":"Apollo 13 (1995)"});
{
"_id" : ObjectId("55599a61f84a9472b69e2a42"),
"movieId" : 28,
"title" : "Apollo 13 (1995)",
"similarId" : 729,
"similarTitle" : "Nell (1994)",
"regularizedCorRelation" : 0.20162457556082056
}
{
"_id" : ObjectId("55599a69f84a9472b69e39f8"),
"movieId" : 28,
"title" : "Apollo 13 (1995)",
"similarId" : 809,
"similarTitle" : "Rising Sun (1993)",
"regularizedCorRelation" : 0.17383996759460163
}



db.movies.find({"title":"Apollo 13 (1995)", 
                "similarMovies.regularizedCorRelation" : 
                           {$gte : 0.09}
               });
{
"_id" : ObjectId("55599a61f84a9472b69e2a42"),
"movieId" : 28,
"title" : "Apollo 13 (1995)",
 "similarMovies" : 
 {
"similarId" : 729,
"similarTitle" : "Nell (1994)",
"regularizedCorRelation" : 0.20162457556082056
  },
  {
"similarId" : 809,
"similarTitle" : "Rising Sun (1993)",
"regularizedCorRelation" : 0.17383996759460163
  }
}


[1.2] sort (_id) in descending order
> db.payment_transactios.find().sort({_id:-1});

[1.3] limit 
> db.payment_transactios.find().sort({_id:-1}).limit(1);

> db.payment_transactios.find().sort({_id:-1}).limit(1).pretty();
OR
> db.payment_transactios.find().sort({_id:-1}).limit(1).forEach(printjson);
> db.payment_transactios.find().sort({_id:-1}).limit(1).toArray();

DBQuery.prototype._prettyShell = true


Aggregation
----------------------------------------

//get DISTINCT movie id
> db.movies.aggregate( [ { $group : { _id : "$movieId" } } ] )
{ "_id" : 1596 }
{ "_id" : 1309 }
{ "_id" : 1672 }
{ "_id" : 1679 }
{ "_id" : 1463 }
{ "_id" : 1510 }
{ "_id" : 1650 }
{ "_id" : 1523 }
{ "_id" : 1430 }
{ "_id" : 1482 }
{ "_id" : 1500 }
{ "_id" : 1432 }
{ "_id" : 1504 }
{ "_id" : 711 }
{ "_id" : 1662 }
{ "_id" : 1156 }
{ "_id" : 1420 }
{ "_id" : 1130 }
{ "_id" : 1577 }
{ "_id" : 1403 }

// sort the DISTINCT movie ids
> db.movies.aggregate( [ { $group : { _id : "$movieId" } }, {$sort :{_id:1}} ] )
{ "_id" : 1 }
{ "_id" : 2 }
{ "_id" : 3 }
{ "_id" : 4 }
{ "_id" : 5 }
{ "_id" : 6 }
{ "_id" : 7 }
{ "_id" : 8 }
{ "_id" : 9 }
{ "_id" : 10 }
{ "_id" : 11 }
{ "_id" : 12 }
{ "_id" : 13 }
{ "_id" : 14 }
{ "_id" : 15 }
{ "_id" : 16 }
{ "_id" : 17 }
{ "_id" : 18 }
{ "_id" : 19 }

{ "_id" : 20 }



// count by moviedId
db.movies.aggregate([{
              $group : {
                 _id : "$movieId", 
                 count: { $sum:1 }
              }}]);

{ "_id" : 1596, "count" : 1 }
{ "_id" : 1309, "count" : 1 }
{ "_id" : 1672, "count" : 1 }
{ "_id" : 1679, "count" : 1 }
{ "_id" : 1463, "count" : 1 }
{ "_id" : 1510, "count" : 1 }
{ "_id" : 1650, "count" : 1 }
{ "_id" : 1523, "count" : 1 }
{ "_id" : 1430, "count" : 4 }
{ "_id" : 1482, "count" : 1 }
{ "_id" : 1500, "count" : 1 }
{ "_id" : 1432, "count" : 4 }
{ "_id" : 1504, "count" : 2 }
...


References
How do I drop a MongoDB database, from the command line?, http://stackoverflow.com/a/8857323/432903

Troubleshoot the Map Function, http://docs.mongodb.org/manual/tutorial/troubleshoot-map-function/

Mongo - get occurrence of lastnames, http://stackoverflow.com/a/5544226/432903

Javascript and MapReduce, http://jcla1.com/blog/2013/05/11/javascript-mapreduce/

http://www.mongovue.com/2010/11/03/yet-another-mongodb-map-reduce-tutorial/

No comments:

Post a Comment