aggregation-framework

Concat String by Group

耗尽温柔 提交于 2019-12-23 12:36:26
问题 In my query I want to combine same item into a String group by _id here is my document "_id" : ObjectId("59e955e633d64c81875bfd2f"), "tag_id" : 1, "client_id" : "10001" "_id" : ObjectId("59e955e633d64c81875bfd30"), "tag_id" : 1, "client_id" : "10002" I hope the output would be "_id" : 1 "client_id" : "10001,10002" 回答1: You can do it with the aggregation framework as a "two step" operation. Which is to first accumulate the items to an array via $push withing a $group pipeline, and then to use

$unwind 2 fields separately in mongodb query

孤街浪徒 提交于 2019-12-23 12:26:37
问题 I want to $unwind 2 fields, school and home . database structure is like; { "id" : 1, "school" : [ { "path" : "school1", "code" : "code1", }, { "path" : "school2", "code" : "code2", }, { "path" : "school3", "code" : "code3", }, { "path" : "school4", "code" : "code4", } ], "home" : [ { "path" : "home1", "code" : "homeCode1", }, { "path" : "home2", "code" : "homeCode2", }, ] } I wanted to $unwind school and home fields and get each of them as; { "id" : 1, "school" : [ { "path" : "school1",

mongo aggregation query in golang with mgo driver

China☆狼群 提交于 2019-12-23 12:06:54
问题 I have the following query in mongodb - db.devices.aggregate({ $match: {userId: "v73TuQqZykbxFXsWo", state: true}}, { $project: { userId: 1, categorySlug: 1, weight: { $cond: [ {"$or": [ {$eq: ["$categorySlug", "air_fryer"] }, {$eq: ["$categorySlug", "iron"] } ] }, 0, 1] } } }, {$sort: {weight: 1}}, { $limit : 10 } ); I'm trying to write this in golang using the mgo driver but not able to wrap my head around this at all! How do I translate this to a golang mgo query? 回答1: The examples on the

MongoDB Aggregate for a sum on a per week basis for all prior weeks

倖福魔咒の 提交于 2019-12-23 12:00:02
问题 I've got a series of docs in MongoDB. An example doc would be { createdAt: Mon Oct 12 2015 09:45:20 GMT-0700 (PDT), year: 2015, week: 41 } Imagine these span all weeks of the year and there can be many in the same week. I want to aggregate them in such a way that the resulting values are a sum of each week and all its prior weeks counting the total docs. So if there were something like 10 in the first week of the year and 20 in the second, the result could be something like [{ week: 1, total:

MongoDB Aggregate for a sum on a per week basis for all prior weeks

…衆ロ難τιáo~ 提交于 2019-12-23 11:59:28
问题 I've got a series of docs in MongoDB. An example doc would be { createdAt: Mon Oct 12 2015 09:45:20 GMT-0700 (PDT), year: 2015, week: 41 } Imagine these span all weeks of the year and there can be many in the same week. I want to aggregate them in such a way that the resulting values are a sum of each week and all its prior weeks counting the total docs. So if there were something like 10 in the first week of the year and 20 in the second, the result could be something like [{ week: 1, total:

mongo $sum compounded when doing $unwind and then $group on multiple fields

随声附和 提交于 2019-12-23 10:07:03
问题 I have the following document structure { "app_id": "DHJFK67JDSJjdasj909", "date": ISODate("2014-08-07T00:00:00.000Z"), "event_count": 100, "events": [ { "type": 0, "value": 12 }, { "type": 10, "value": 24 }, { "type": 20, "value": 36 }, { "type": 30, "value": 43 } ], "unique_events": [ { "type": 0, "value": 5 }, { "type": 10, "value": 8 }, { "type": 20, "value": 12 }, { "type": 30, "value": 56 } ] } I am trying to get a sum of event_counts and also the values for unique_events and events per

The Field “$_id” must be an accumulator object

旧巷老猫 提交于 2019-12-23 08:48:16
问题 I am trying to group some documents within mongoDB, right after a $match stage like this: db.trips.aggregate([ { "$match": { "Stop Time": { "$lt": "31.07.2013 23:59" } } }, { "$group": { "$_id": { "Start": "$Start", "End": "$End" }, } }] However I am getting the following error: "The field '$_id' must be an accumulator object" Why does this error show up, and why does it only show up when I include the match stage? Without the match stage, it works just fine. 回答1: $_id is not an accumulator

The Field “$_id” must be an accumulator object

我的未来我决定 提交于 2019-12-23 08:46:02
问题 I am trying to group some documents within mongoDB, right after a $match stage like this: db.trips.aggregate([ { "$match": { "Stop Time": { "$lt": "31.07.2013 23:59" } } }, { "$group": { "$_id": { "Start": "$Start", "End": "$End" }, } }] However I am getting the following error: "The field '$_id' must be an accumulator object" Why does this error show up, and why does it only show up when I include the match stage? Without the match stage, it works just fine. 回答1: $_id is not an accumulator

Use mongodb aggregation framework to group by length of array

江枫思渺然 提交于 2019-12-23 08:08:58
问题 I have a collection that looks something like this: { "_id": "id0", "name": "...", "saved_things": [ { ... }, { ... }, { ... }, ] } { "_id": "id1", "name": "...", "saved_things": [ { ... }, ] } { "_id": "id2", "name": "...", "saved_things": [ { ... }, ] } etc... I want to use mongodb's aggregation framework in order to come up with a histogram result that tells how many users have a certain count of the saved_things . For example, for the dataset above it could return something like: { "_id":

How to use MongoDB aggregate to get the first of each group, including nulls?

寵の児 提交于 2019-12-23 05:46:19
问题 In my MongoDB people collection I need to filter people with the same 'alias' property value, keeping the first one of them, and also keeping all people with a null 'alias'. Some sample people data: { "_id" : "1", "flag" : true, "name" : "Alice", "alias" : null }, { "_id" : "2", "flag" : true, "name" : "Bob", "alias" : "afa776bea788cf4c" }, { "_id" : "3", "flag" : true, "name" : "Bobby", "alias" : "afa776bea788cf4c" }, { "_id" : "4", "flag" : true, "name" : "Cristina", "alias" : null }, { "