aggregation-framework

How to calculate the running total using aggregate

跟風遠走 提交于 2019-12-17 18:25:02
问题 I'm developing a simple financial app for keeping track of incomes and outcomes. For the sake of simplicity, let's suppose these are some of my documents: { "_id" : ObjectId("54adc0659413535e02fba115"), "description" : "test1", "amount" : 100, "dateEntry" : ISODate("2015-01-07T23:00:00Z") } { "_id" : ObjectId("54adc21a0d150c760270f99c"), "description" : "test2", "amount" : 50, "dateEntry" : ISODate("2015-01-06T23:00:00Z") } { "_id" : ObjectId("54b05da766341e4802b785c0"), "description" :

Count array elements that matches condition

强颜欢笑 提交于 2019-12-17 17:07:08
问题 I have a mongoDB collection called "conference" with an array of participants as below : [ { "_id" : 5b894357a0c84d5a5d221f25, "conferenceName" : "myFirstConference", "startDate" : 1535722327, "endDate" : 1535722420, "participants" : [ { "name" : "user1", "origin" : "internal", "ip" : "192.168.0.2" }, { "name" : "user2", "origin" : "external", "ip" : "172.20.0.3" }, ] }, ... ] I would like to get the following result : [ { "conferenceName" : "myFirstConference", "startDate" : 1535722327,

Mongo Aggregation : $group and $project array to object for counts

笑着哭i 提交于 2019-12-17 14:57:12
问题 I have documents like: { "platform":"android", "install_date":20151029 } platform - can have one value from [android|ios|kindle|facebook ] . install_date - there are many install_dates There are also many fields. Aim : I am calculating installs per platform on particular date. So I am using group by in aggregation framework and make counts by platform. Document should look like like: { "install_date":20151029, "platform" : { "android":1000, "ios": 2000, "facebook":1500 } } I have done like:

MongoDB group by hour

纵饮孤独 提交于 2019-12-17 14:02:58
问题 I save tweets to mongo DB: twit.stream('statuses/filter', {'track': ['animal']}, function(stream) { stream.on('data', function(data) { console.log(util.inspect(data)); data.created_at = new Date(data.created_at); collectionAnimal.insert(data, function(err, docs) {}); }); }); It's OK. The tweet time in MongoDB is in format: 2014-04-25 11:45:14 GMT (column created_at) Now I need group column created_at in hours. I would like to have the result: hour | count tweets in hour 1 | 28 2 | 26 3 | 32 4

MongoDB group by hour

送分小仙女□ 提交于 2019-12-17 14:02:05
问题 I save tweets to mongo DB: twit.stream('statuses/filter', {'track': ['animal']}, function(stream) { stream.on('data', function(data) { console.log(util.inspect(data)); data.created_at = new Date(data.created_at); collectionAnimal.insert(data, function(err, docs) {}); }); }); It's OK. The tweet time in MongoDB is in format: 2014-04-25 11:45:14 GMT (column created_at) Now I need group column created_at in hours. I would like to have the result: hour | count tweets in hour 1 | 28 2 | 26 3 | 32 4

Aggregating in local timezone in mongodb

↘锁芯ラ 提交于 2019-12-17 13:54:58
问题 I am building application in mongodb and nodejs that will be used in Italy . Italy timezone is +02:00 . This means if any one saving some data at 01:am of 11 July then it will be saved as 11:00 pm of 10 July as mongo saves date in UTC. We need to show date wise tx count. So I made group by query on date. But it shows that tx in previous day. What should be workaround for this. > db.txs.insert({txid:"1",date : new Date("2015-07-11T01:00:00+02:00")}) > db.txs.insert({txid:"2",date : new Date(

$unwind an object in aggregation framework

倖福魔咒の 提交于 2019-12-17 10:53:22
问题 In the MongoDB aggregation framework, I was hoping to use the $unwind operator on an object (ie. a JSON collection). Doesn't look like this is possible, is there a workaround? Are there plans to implement this? For example, take the article collection from the aggregation documentation . Suppose there is an additional field "ratings" that is a map from user -> rating. Could you calculate the average rating for each user? Other than this, I'm quite pleased with the aggregation framework.

MongoDB aggregation with Java driver

让人想犯罪 __ 提交于 2019-12-17 10:49:12
问题 I need your help for using MongoDB aggregation framework with java driver. I don't understand how to write my request, even with this documentation. I want to get the 200 oldest views from all items in my collection. Here is my mongo query (which works like I want in console mode): db.myCollection.aggregate( {$unwind : "$views"}, {$match : {"views.isActive" : true}}, {$sort : {"views.date" : 1}}, {$limit : 200}, {$project : {"_id" : 0, "url" : "$views.url", "date" : "$views.date"}} ) Items in

Export mongodb aggregation framework result to a new collection

谁都会走 提交于 2019-12-17 10:44:46
问题 I want to save the aggregation framework result to a new collection. I know that's impossible with the framework at the moment with the command itself. Is there a workaround in the shell? 回答1: Update: See Salvador's answer for a more efficient way to do this in MongoDB 2.6+. Save the aggregation result in a variable and then insert its result property into a new collection: var result = db.foo.aggregate(...); db.bar.insert(result.result); 回答2: Starting with Mongo 2.6.0 you can do this

MongoDB {aggregation $match} vs {find} speed

感情迁移 提交于 2019-12-17 10:36:46
问题 I have a mongoDB collection with millions of rows and I'm trying to optimize my queries. I'm currently using the aggregation framework to retrieve data and group them as I want. My typical aggregation query is something like : $match > $group > $ group > $project However, I noticed that the last parts only take a few ms, the beginning is the slowest. I tried to perform a query with only the $match filter, and then to perform the same query with collection.find. The aggregation query takes