aggregation-framework

Using an Index with Mongo's $first Group Operator

十年热恋 提交于 2020-05-16 22:00:20
问题 Per Mongo's latest $group documentation, there is a special optimization for $first: Optimization to Return the First Document of Each Group If a pipeline sorts and groups by the same field and the $group stage only uses the $first accumulator operator, consider adding an index on the grouped field which matches the sort order. In some cases, the $group stage can use the index to quickly find the first document of each group. It makes sense, since only the first entry in an ordered index

MongoDB Aggregate $lookup on array of object with additional field

岁酱吖の 提交于 2020-05-16 06:26:48
问题 I have two different collections (example below) methods & items . As for now, I'm using pre 3.6 vanilla aggregation query for $lookup : MongoPlayground Example { $lookup: { from: "items", localField: "reagents._id", foreignField: "_id", as: "reagent_items" } } The problem is that if I am using it, I miss quantity field (from methods.reagents embedded) during $lookup stage from original collection. For now, I return quantity right after lookup but as I heard, Mongo introduced from 3.6 a new

MongoDB Aggregate $lookup on array of object with additional field

心不动则不痛 提交于 2020-05-16 06:23:28
问题 I have two different collections (example below) methods & items . As for now, I'm using pre 3.6 vanilla aggregation query for $lookup : MongoPlayground Example { $lookup: { from: "items", localField: "reagents._id", foreignField: "_id", as: "reagent_items" } } The problem is that if I am using it, I miss quantity field (from methods.reagents embedded) during $lookup stage from original collection. For now, I return quantity right after lookup but as I heard, Mongo introduced from 3.6 a new

MongoDB - How to return matched item in an array while using $in in $group?

纵然是瞬间 提交于 2020-05-16 02:19:21
问题 I'm using MongoDB's aggregation. Using $in I'm checking if a value exists in an array or not, And I want to return the matched object from calificacion array into calificacion_usuario field. (my _id is unique in the array). I tried using $first" to return the current element. It's not working, not sure why & what to use. How can I do it? Sample Doc : { "cargo" : "Presidente", "calificacion" : [ { "_id" : "5e894ae6fa9fd23780bcf472", "estrellas" : 3 }, { "_id" : "5e895187fa9fd23780bcf478",

MongoDB Charts – chart cumulative growth?

[亡魂溺海] 提交于 2020-05-15 18:49:11
问题 Using MongoDB Charts, how can I chart cumulative growth? Using the _id value of a document which contains the information of when it was created, I'd like to chart the number of documents in my database over time. E.g., if in July 10 documents were created, and in August another 10 were created, then the chart should display 10 for July and 20 for August. It's easy to bin document creation dates per months (see below example), but I'd like to turn this into a cumulative growth chart. MongoDB

MongoDB Charts – chart cumulative growth?

这一生的挚爱 提交于 2020-05-15 18:48:25
问题 Using MongoDB Charts, how can I chart cumulative growth? Using the _id value of a document which contains the information of when it was created, I'd like to chart the number of documents in my database over time. E.g., if in July 10 documents were created, and in August another 10 were created, then the chart should display 10 for July and 20 for August. It's easy to bin document creation dates per months (see below example), but I'd like to turn this into a cumulative growth chart. MongoDB

Use $strLenCP with Spring Data MongoDB

回眸只為那壹抹淺笑 提交于 2020-05-15 09:34:04
问题 I have this mongodb query db.getCollection('myCollection').aggregate( [{ $project: { length: { $strLenCP: "$prefix" } } }, { $sort: { length: -1 } }] ) that I want to use into a spring java project but I can't manage to write the correct java code (the sort is not the issue). I tried this Aggregation agg = newAggregation(project().andExpression("strLenCP(prefix)").as("prefixLength")); AggregationResults < RequestSettingsWithPrefixLength > results = mongoTemplate.aggregate(agg, RequestSettings

Concat int and string array fields which are in different arrays

半腔热情 提交于 2020-05-15 09:25:53
问题 { "no" : "2020921008981", "date" : ISODate("2020-04-01T05:19:02.263+0000"), "sale" : { "soldItems" : [ { "itemId" : "5b55ac7f0550de00210a3b24", "qty" : NumberInt(1), }, { "itemId" : "5b55ac7f0550de00210a3b25", "qty" : NumberInt(2), } ], "items" : [ { "_id" : ObjectId("5b55ac7f0550de00210a3b24"), unit :"KG" }, { "_id" : ObjectId("5b55ac7f0550de00210a3b25"), unit :"ML" } ] } } Desired output : { "no" : "2020921008981", "sale" : {} "qtyList" : "1 KG \n 2 ML" } In order to build itemQtyList

How to conditionally project fields during aggregate in mongodb

你。 提交于 2020-05-15 05:08:08
问题 I have a user document like: { _id: "s0m3Id", _skills: ["skill1", "skill2"], } Now I want to unwind this document by the _skills field and add a score for each skill. So my aggregate looks like: { '$unwind': {'path': '$_skills', 'preserveNullAndEmptyArrays': true}, }, { '$project': { '_skills': 'label': '$_skills', 'skill_score': 1 }, } }, Sometimes the _skills field can be empty, however in this case I still want the user document to flow through the aggregation - hence the

Select last value of array in sub document

本秂侑毒 提交于 2020-05-15 05:07:30
问题 I have the following document structure { "_id" : 1, "prices" : { "100" : [ 5.67, . . 1.7, ], "101" : [ 4.67, . . 1.4, }, "v" : 2 } We need to get the last value from each of the fields "100" and "101", e.g 1.7 & 1.4, using mongodb v3.0.2. Currently we are loading the whole document and then getting the required values in the application. It has become quite a bottleneck as each document may be ~1MB, which for 100s of documents is adding up. We are hoping there is a suitable feature of