aggregation-framework

Mongo aggregation framework, Sort and then group not working

て烟熏妆下的殇ゞ 提交于 2019-12-22 03:42:31
问题 I am trying sort data by date first and then group on another field. It is not working to me. The question I am trying to answer is: Select the most recent distinct cid? given this data: db.summary.save({"lid" : 5, "date" : 5, "cid" : 2, "circles" : [ 2 ] }) db.summary.save({"lid" : 2, "date" : 2, "cid" : 1, "circles" : [ 2 ] }) db.summary.save({"lid" : 4, "date" : 0, "cid" : 3, "circles" : [ 2 ] }) db.summary.save({"lid" : 3, "date" : 3, "cid" : 2, "circles" : [ 2 ] }) db.summary.save({"lid"

Mongo aggregation framework, Sort and then group not working

守給你的承諾、 提交于 2019-12-22 03:41:08
问题 I am trying sort data by date first and then group on another field. It is not working to me. The question I am trying to answer is: Select the most recent distinct cid? given this data: db.summary.save({"lid" : 5, "date" : 5, "cid" : 2, "circles" : [ 2 ] }) db.summary.save({"lid" : 2, "date" : 2, "cid" : 1, "circles" : [ 2 ] }) db.summary.save({"lid" : 4, "date" : 0, "cid" : 3, "circles" : [ 2 ] }) db.summary.save({"lid" : 3, "date" : 3, "cid" : 2, "circles" : [ 2 ] }) db.summary.save({"lid"

Flatten a nested object in MongoDB and rename

北慕城南 提交于 2019-12-22 00:39:53
问题 Suppose I have documents like this { "id" : "1415166669", "color" : { "14" : "Silver" }, "name":"Random Name" } where the key inside color can change, i.e Next Document may look like this { "id" : "1415126969", "color" : { "11" : "Gold" }, "name":"Random Name 2" } I want to flatten and rename them so that my documents have a similar structure as follows: { "id" : "1415126969", "color" : "Gold" "name":"Random Name 2" } and { "id" : "1415166669", "color" : "Silver" "name":"Random Name" } I

Mongodb regex in aggregation using reference to field value

夙愿已清 提交于 2019-12-21 22:00:25
问题 note: I'm using Mongodb 4 and I must use aggregation, because this is a step of a bigger aggregation Problem How to find in a collection documents that contains fields that starts with value from another field in same document ? Let's start with this collection: db.regextest.insert([ {"first":"Pizza", "second" : "Pizza"}, {"first":"Pizza", "second" : "not pizza"}, {"first":"Pizza", "second" : "not pizza"} ]) and an example query for exact match: db.regextest.aggregate([ { $match : { $expr: {

Using $cond operator with Spring-data-mongodb [duplicate]

限于喜欢 提交于 2019-12-21 21:36:01
问题 This question already has answers here : How to use $cond operation in Spring-MongoDb aggregation framework (2 answers) Closed last year . I am looking to aggregate the following data { "user": "user1", "error": true } { "user": "user2", "error": false } { "user": "user1", "error": false } Into { "_id": "user1", "errorCount": 1, "totalCount": 2 }, { "_id": "user2", "errorCount": 0, "totalCount": 1 } With $cond operator, this can be achieved using: $group: { _id: "$user", errorCount : { "$sum"

Filter results by the Last Array Entry Field Value

﹥>﹥吖頭↗ 提交于 2019-12-21 20:37:14
问题 Having this document structure (omitting irrelevant fields for brevity): [ { "_id" : 0, "partn" : [ { "date" : ISODate("2015-07-28T00:59:14.963Z"), "is_partner" : true }, { "date" : ISODate("2015-07-28T01:00:32.771Z"), "is_partner" : false }, { "date" : ISODate("2015-07-28T01:15:29.916Z"), "is_partner" : true }, { "date" : ISODate("2015-08-05T13:48:07.035Z"), "is_partner" : false }, { "date" : ISODate("2015-08-05T13:50:56.482Z"), "is_partner" : true } ] }, { "_id" : 149, "partn" : [ { "date"

Filter Documents by Distance Stored in Document with $near

陌路散爱 提交于 2019-12-21 19:51:22
问题 I am using the following example to better explain my need. I have a set of points(users) on a map and collection schema is as below { location:{ latlong:[long,lat] }, maxDistance:Number } i have another collection with events happening in the area. schema is given below { eventLocation:{ latlong:[long,lat] } } now users can add their location and the maximum distance they want to travel for to attend an event and save it. whenever a new event is posted , all the users satisfying their

View progress of long running mongodb aggregation job

旧时模样 提交于 2019-12-21 07:26:25
问题 I have a long running job using Mongodb's ( 2.6.0-rc2 ) aggregation framework: http://docs.mongodb.org/manual/core/aggregation-introduction/ I have written the aggregation in javascript and run the job as a script (i.e. mongo localhost:27017/test myjsfile.js ). After starting the script, is there any way to see the progress of the job? For example, using the sample aggregation job: db.zipcodes.aggregate([ {$group: { _id: "$state", totalPop: {$sum: "$pop"} }}, {$match: {totalPop: {$gte: 10

Mongodb Aggregation framework group and sort

这一生的挚爱 提交于 2019-12-21 06:55:34
问题 I have the following document structure... { "id":"documentID" "sessionId":"sometext" "msg":"sometext" "time":"date" } sessionId can exist in many documents I want to aggregate the documents by sessionId , the result for each session should contain the set of messages related to the session sorted by time. Using the MongoDB aggregation framework how can I achieve that? I have tried to sort first and then group but the messages in each session wasn't sorted for some reason: { $sort: { "time":

Distinct count of multiple fields using mongodb aggregation

孤人 提交于 2019-12-21 04:41:15
问题 I'm trying to count distinct values of multiple fields By one MongoDB Aggregation query. So here's my data: { "car_type": "suv", "color": "red", "num_doors": 4 }, { "car_type": "hatchback", "color": "blue", "num_doors": 4 }, { "car_type": "wagon", "color": "red", "num_doors": 4 } I want a distinct count of each field: distinct_count_car_type=3 distinct_count_color=2 distinct_count_num_doors=1 I was able to group multiple fields and then do a distinct count but it could only give me a count on