Category Archives: mongodb

mongodb query with cond, group, sort and limit

db.user_data.aggregate([{$match:{‘start_time’:{$gt:ISODate(“2013-07-15T00:00:00Z”),$lt:ISODate(“2013-08-15T00:00:00Z”)},’site_id’:403}},{$project:{term:’$ga.term’}},{$group:{ _id : ‘$term’ , number : { $sum : 1 }}},{$sort : { number : -1 }},{$limit : 10}]);

Posted in mongodb | 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 query: is not null and compare two field values

db.user.find({campaign:{$exists:true},’order.discount’:{$ne:null},$where:function() {return this.order.discount.code != this.campaign.offer.coupon} })

Posted in mongodb | 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