Tag Archives: mongodb

mongodb query array nth element

‘pageviews.0.page_type’:’home’ will check whether the ‘page_type’ of the first element in the pageviews array is ‘home’

Posted in Uncategorized | Tagged | Leave a comment

MongoDB commands

db.getMongo().slaveOk = true

Posted in Uncategorized | Tagged | Leave a comment

mongoexport query timestamp

mongoexport … –query ‘{“start_time”:{“$gte”:new Date(1351641600000),”$lt”:new Date(1351728000000)}}’ –out 20121031.json *the last three digits is #operations within a given second.

Posted in mongodb | Tagged | Leave a comment

find query in mongo and pymongo

mongos> db.user_data.find({‘site_id’:37,’cart’:{$exists:true},’order’:{$exists:true},’start_time’:{$gt:ISODate(“2012-09-11T00:00:00.000Z”)}}) translated into pymongo for session in sessions.find({“site_id”:37,”cart”:{“$exists”:True},”order”:{“$exists”:True},”start_time”:{“$gt”:datetime.strptime(‘20120911′,’%Y%m%d’)}}):

Posted in mongodb | Tagged , | Leave a comment

MongoDB count distinct substrings of values

counting distinct values is easy as: db.my_collection.distinct(_id) but counting substrings of the values is not var cnt=new Object() db.my_collection.find({_id:{$exists:true}}).forEach(function(doc){if(doc._id != null && typeof doc._id == “string”) cnt[doc._id.substring(0,doc._id.indexOf(‘:’))]=1;}) var size=0; for(key in cnt) {if (cnt.hasOwnProperty(key)) size++;};

Posted in mongodb | Tagged | Leave a comment