aggregation-framework

Fastest way to get histogram of array sizes using MongoDB aggregation framework

依然范特西╮ 提交于 2020-01-02 01:03:12
问题 I'm trying to get a list of the number of records that have arrays of varying size. I want to get the distribution of array sizes for all records so I can build a histogram like this: | * | * documents | * * | * * * |_*__*__*___*__*___ 2 5 6 23 47 Array Size So the raw documents look something like this: {hubs : [{stuff:0, id:6}, {stuff:1"}, .... ]} {hubs : [{stuff:0, id:6}]}` So far using the aggregation framework and some of the help here I've come up with db.sitedata.aggregate([{ $unwind:'

Mongo db aggregation multiple conditions

我的梦境 提交于 2020-01-02 00:57:14
问题 I want to project a collection applying exporting a value only if a field is inside a range. Sort of: db.workouts.aggregate({ $match: { user_id: ObjectId(".....") } }, { $project: { '20': { $cond: [ {$gt: [ "$avg_intensity", 20]} , '$total_volume', 0] } } }) I need to get the value only if the avg_intensity is inside a certain range. I will then group and sum on the projection result. What I am trying to do is applying a $gt and $lt filter but with no much success. db.workouts.aggregate( {

Compute first order derivative with MongoDB aggregation framework

生来就可爱ヽ(ⅴ<●) 提交于 2020-01-02 00:52:15
问题 Is it possible to calculate a first order derivative using the aggregate framework? For example, I have the data : {time_series : [10,20,40,70,110]} I'm trying to obtain an output like: {derivative : [10,20,30,40]} 回答1: db.collection.aggregate( [ { "$addFields": { "indexes": { "$range": [ 0, { "$size": "$time_series" } ] }, "reversedSeries": { "$reverseArray": "$time_series" } } }, { "$project": { "derivatives": { "$reverseArray": { "$slice": [ { "$map": { "input": { "$zip": { "inputs": [ "

Perform joins in mongodb with three collections?

≯℡__Kan透↙ 提交于 2020-01-01 20:49:12
问题 I am using $lookup for joining different collections in mongoDB. Now i am facing a problem here suppose i have 3 collections given below. user_movies { "_id": "_id" : ObjectId("5834ecf7432d92675bde9d83"), "mobile_no": "7941750156" "movies" : ["dallas00", "titanic00", "green_mile00"] } movies { "_id": "_id" : ObjectId("4834eff7412d9267556d9d52"), "movie_name" : "Dallas Buyer's Club", "movie_id": "dallas00", "active": 0 } movie_comments { "_id": "_id" : ObjectId("1264eff7412d92675567h576"),

Is it possible to type cast data inside an aggregation pipeline on MongoDB?

删除回忆录丶 提交于 2020-01-01 09:33:28
问题 When I need to aggregate things by date using the aggregate command on MongoDB, I usually do this: db.mycollection.aggregate( { $project: { day: {$dayOfMonth: "$date"}, mon: {$month: "$date"}, year: {$year: "$date"}, } }, { $group: { _id : {day: "$day", mon: "$mon", year: "$year"}, count: {$sum: 1} } } ) and eventually concatenate the day , mon , and year fields into a date string in the application. For many reasons though, sometimes I want to concatenate the fields before leaving the

How do I use aggregation operators in a $match in MongoDB (for example $year or $dayOfMonth)?

流过昼夜 提交于 2020-01-01 08:05:28
问题 I have a collection full of documents with a created_date attribute. I'd like to send these documents through an aggregation pipeline to do some work on them. Ideally I would like to filter them using a $match before I do any other work on them so that I can take advantage of indexes however I can't figure out how to use the new $year/$month/$dayOfMonth operators in my $match expression. There are a few examples floating around of how to use the operators in a $project operation but I'm

Aggregation in Golang mgo for Mongodb

故事扮演 提交于 2020-01-01 04:55:38
问题 Anybody knows what’s the equivalent of aggregate command we use in mongodb shell for golang mgo/bson? Something like that: aggregate([{$match:{my_id:ObjectId("543d171c5b2c1242fe0019")}},{$sort:{my_id:1, dateInfo:1, name:1}},{$group:{_id:"$my_id", lastEntry:{$max: "$dateInfo"},nm:{$last:"$name"}}}]) 回答1: Assuming that c is your Collection: pipe := c.Pipe([]bson.M{{"$match": bson.M{"name":"John"}}}) resp := []bson.M{} err := pipe.All(&resp) if err != nil { //handle error } fmt.Println(resp) //

Field not showing in Mongoose aggregation

╄→尐↘猪︶ㄣ 提交于 2019-12-31 05:36:06
问题 I'm trying to get a count of all distinct departments. I'd like the results to contain the deptType, deptName and the count. The grouping works, and results show deptType and dCount, but not deptName. Any idea why? My data looks like this: { "_id": "10280", "city": "NEW YORK", "state": "NY", "departments": [ {"departmentType":"01", "departmentHead":"Peter"}, {"departmentType":"02", "departmentHead":"John"} ] }, { "_id": "10281", "city": "LOS ANGELES", "state": "CA", "departments": [ {

Invalid operator '$size' in aggregation

烈酒焚心 提交于 2019-12-31 05:28:13
问题 I got the following piece of code: from pymongo import MongoClient client = MongoClient('ipOfServer') db = client.admin db.authenticate('login', 'password', source='admin_') heh = list(db.events.aggregate( [ {"$match": {"status": 'start'}}, {"$group": {"_id": "$eventName", "players": {"$addToSet": "$uid"}}}, {"$project": {"_id": 1, "Count": {"$size": "$players"}}} ])) print(heh) and this is worked for the original programmer who wrote and tested it code result while testing. But when I try to

Count events and insert string literal during aggregation

我的未来我决定 提交于 2019-12-31 04:04:29
问题 I have large collection of documents which represent some kind of events. Collection contains events for different userId. { "_id" : ObjectId("57fd7d00e4b011cafdb90d22"), "userId" : "123123123", "userType" : "mobile", "event_type" : "clicked_ok", "country" : "US", "timestamp" : ISODate("2016-10-12T00:00:00.308Z") } { "_id" : ObjectId("57fd7d00e4b011cafdb90d22"), "userId" : "123123123", "userType" : "mobile", "event_type" : "clicked_cancel", "country" : "US", "timestamp" : ISODate("2016-10