aggregation-framework

MongoDB - Aggregate Sum

穿精又带淫゛_ 提交于 2020-01-21 07:43:15
问题 I am attempting to calculate the total amount of money spent being tracked inside of our database. Each order document contains a field "total_price" I am attempting to use the following code: db.orders.aggregate({ $group: { _id: null, total: {$sum: "$total_price"} } }) Unfortunately, the only output I get is: { "result" : [ { "_id" : null, "total" : 0 } ], "ok" : 1 } But to verifiy there is actually numerical data stored, and just not being totaled: db.orders.find()[0].total_price this

mongo group and count with condition

China☆狼群 提交于 2020-01-21 04:18:26
问题 I'm trying to group a set of documents and count them based on their value. For example { "_id" : 1, "item" : "abc1", "value" : "1" } { "_id" : 2, "item" : "abc1", "value" : "1" } { "_id" : 3, "item" : "abc1", "value" : "11" } { "_id" : 4, "item" : "abc1", "value" : "12" } { "_id" : 5, "item" : "xyz1", "value" : "2" } Here I would like to group by "item" and get in return a count how many times the "value" is bigger than 10 and how many times smaller. So: { "item": "abc1", "countSmaller": 2,

Mongodb aggregate sort and limit within group [duplicate]

风流意气都作罢 提交于 2020-01-20 05:17:04
问题 This question already has answers here : mongodb group values by multiple fields (3 answers) Closed 11 months ago . I have a collection of items for sale with the following schema: var itemSchema = new Schema({ "category" : { type : Schema.Types.ObjectId, ref : 'Category' }, "merchant" : { type : Schema.Types.ObjectId, ref : 'Merchant' }, "rating" : Number }) I have inherited an aggregate query which returns items matching a category, grouped by merchant, with the groups sorted by the maximum

$skip and $limit in aggregation framework

有些话、适合烂在心里 提交于 2020-01-19 04:33:47
问题 When I read the document I found the following notes: When a $sort immediately precedes a $limit in the pipeline, the $sort operation only maintains the top n results as it progresses, where n is the specified limit, and MongoDB only needs to store n items in memory. This optimization still applies when allowDiskUse is true and the n items exceed the aggregation memory limit. If I'm right about this, it applies only when I use the $sort and $limit together like db.coll.aggregate([ ..., {$sort

$skip and $limit in aggregation framework

白昼怎懂夜的黑 提交于 2020-01-19 04:33:25
问题 When I read the document I found the following notes: When a $sort immediately precedes a $limit in the pipeline, the $sort operation only maintains the top n results as it progresses, where n is the specified limit, and MongoDB only needs to store n items in memory. This optimization still applies when allowDiskUse is true and the n items exceed the aggregation memory limit. If I'm right about this, it applies only when I use the $sort and $limit together like db.coll.aggregate([ ..., {$sort

sum up value from multiple subdocuments in mongodb

五迷三道 提交于 2020-01-17 03:36:52
问题 I have the following datastructure: { "_id" : ObjectId("4e96f771e016b98aa63d88c9"), "goals" : { "4809" : { "VisitsBeforeGoal" : 12, "pagesBeforeGoal" : 16 }, "4810" : { "VisitsBeforeGoal" : 2, "pagesBeforeGoal" : 6 }, "4811" : { "VisitsBeforeGoal" : 3, "pagesBeforeGoal" : 8 } }, "totalPages" : 246, "totalVisits" : 114 } 4809, 4810 and 4811 is goalID's that are not know, e.g. dynamic. Now what i want is to get the sum of "VisitsBeforeGoal" on each goal and "pagesBeforeGoal" and a sum of goals.

Aggregation by date in Morphia

限于喜欢 提交于 2020-01-17 02:50:07
问题 I'm trying to migrate a PostgreSQL-based application to MongoDB. I'm using Morphia to connect to MongoDB. In my app I have this query to get a list of measurements with Hibernate: ... jpq = jpq.from(qm) .where(qm.parameter.eq(parameter)); jpq = jpq.where(qm.dateTime.between(startDate, endDate)); ... case MINUTE: jpq = jpq.groupBy(qm.dateTime.minute()); case HOUR: jpq = jpq.groupBy(qm.dateTime.hour()); case DAY: jpq = jpq.groupBy(qm.dateTime.dayOfMonth()); case MONTH: jpq = jpq.groupBy(qm

Can Mongo use a reference and compare values during aggregation?

天大地大妈咪最大 提交于 2020-01-16 22:26:14
问题 Given this document: { "_id" : ObjectId("53f7287881a97c71e58e3514"), "title" : "My Page", "currentVersion" : 1, "versions" : [ { "version" : 0, "content" : [ { "data" : "foo", } ] }, { "version" : 1, "content" : [ { "data" : "bar", } ] } ] } Is there anyway, (via the aggregation framework or otherwise) to perform a document projection where the only content in versions is the one that satisfies versions.version==currentVersion for each document? I understand that the first step might be to

Can Mongo use a reference and compare values during aggregation?

孤街醉人 提交于 2020-01-16 22:25:27
问题 Given this document: { "_id" : ObjectId("53f7287881a97c71e58e3514"), "title" : "My Page", "currentVersion" : 1, "versions" : [ { "version" : 0, "content" : [ { "data" : "foo", } ] }, { "version" : 1, "content" : [ { "data" : "bar", } ] } ] } Is there anyway, (via the aggregation framework or otherwise) to perform a document projection where the only content in versions is the one that satisfies versions.version==currentVersion for each document? I understand that the first step might be to

Pivot embedded documents keys to it's own document (column in Table View) Mongo

谁说我不能喝 提交于 2020-01-16 19:35:10
问题 I have a set of documents like: { "_id" : ObjectId("53ad60c1600f5b241c693ce2"), "items" : [ { "EffDate" : ISODate("2014-05-01T00:00:00.000+0000"), "PlanProductCode" : "317", "PlanProduct" : "Medicare SNP", "BenefitPackage" : "SUPERIOR HEALTH PLAN INC, SU MEDICARE ADVANTAGE PLAN", "EndDate" : ISODate("9999-12-31T00:00:00.000+0000") }, { "EffDate" : ISODate("2014-05-01T00:00:00.000+0000"), "PlanProductCode" : "317", "PlanProduct" : "Medicare SNP", "BenefitPackage" : "SUPERIOR HEALTH PLAN INC,