aggregation-framework

MongoDB — Find duplicate documents by multiple keys

浪子不回头ぞ 提交于 2020-08-25 04:48:01
问题 I have a collection with documents that look like the following: { "_id" : ObjectId("55b377cb66b393427367c3e2"), "comment" : "This is a comment", "url_key" : "55b377cb66b393427367c3df", //This is an ObjectId from another record in a different collection } I need to find records in this collection that contain duplicate values for the both the comment AND the url_key. I can easily generate (using aggregate) duplicate records for the same, single, key (eg: comment), but I can't figure out how

MongoDB Aggregation join array of strings to single string

戏子无情 提交于 2020-08-22 04:07:32
问题 We're trying to 'join' an array of strings to a single string within an aggregation. Given is the following dataset: Collection 1: { id: 1234, field: 'test' } Collection 2: { id: 1111, collection1_id: 1234, name: 'Max' }, { id: 1112, collection1_id: 1234, name: 'Andy' } The current result (after lookup etc.): { id: 1234, field: 'test', collection2: ['Max', 'Andy'] } The desired result: { id: 1234, field: 'test', collection2: 'Max, Andy' } Is it somehow possible to join the 'collection2' to a

Sum all values of same named fields of docs in a collection

怎甘沉沦 提交于 2020-08-09 07:19:22
问题 I have a database with the following documents: { name: "John", a: 20, b: 30, c: 40, d: 50 }, { name: "Rich", a: 20, b: 30, c: 40, d: 50 }, { name: "Anne", a: 20, b: 30, c: 40, d: 50 }, { name: "Sam", a: 20, b: 30, c: 40, d: 50 }, I want to calculate the total of hours spent in each of these fields. I accomplished that by: db.hours.aggregate([{$group: {_id: null, totalA: {$sum: "$a"}, totalB: {$sum: "$b"}, totalC: {$sum: "$c"}, totalD: {$sum: "$d"}}}]) { "_id" : null, "totalA" : 80, "totalB"

Get total users list along with the active users in mongodb matched on OR condition

不想你离开。 提交于 2020-08-08 05:13:25
问题 Table : user: A, active_1 : "true", active_2 : "false", is_user : "true" user: B, active_1 : "false", active_2 : "true", is_user : "true" user: C, active_1 : "false", active_2 : "false", is_user : "true" Expected Output: { "_id" : null, "total" : 3, "count" : 2 } I need to check either the active_1 or active_2 is true and get the output as like total which indicates the total no.of users which is A,B and C. The count indicates the users who have either active_1 or active_2 is true. It should

MongoDB: Reduce array of objects into a single object by computing the average of each field

橙三吉。 提交于 2020-08-07 05:27:46
问题 I use the MongoDB aggregation API to aggregate some data daily. The result of this aggregation is of this format: [ { aggDate: '2019-05-23', results: [ { foo: 0.58, bar: 0.42 }, { foo: 0.32, bar: 0.98 } ] } ] The aggregation on the date is fine, but now I would like to aggregate the objects in the results array. The result of this aggregation should be of the following format: [ { aggDate: '2019-05-23', result: { foo: 0.45 // avg of all the `foo`, here: (0.58 + 0.32) / 2 bar: 0.7 // avg of

MongoDB: Reduce array of objects into a single object by computing the average of each field

╄→尐↘猪︶ㄣ 提交于 2020-08-07 05:26:21
问题 I use the MongoDB aggregation API to aggregate some data daily. The result of this aggregation is of this format: [ { aggDate: '2019-05-23', results: [ { foo: 0.58, bar: 0.42 }, { foo: 0.32, bar: 0.98 } ] } ] The aggregation on the date is fine, but now I would like to aggregate the objects in the results array. The result of this aggregation should be of the following format: [ { aggDate: '2019-05-23', result: { foo: 0.45 // avg of all the `foo`, here: (0.58 + 0.32) / 2 bar: 0.7 // avg of

How to watch for changes to specific fields in MongoDB change stream

余生长醉 提交于 2020-08-04 11:45:33
问题 I am using the node driver for mongodb to initiate a change stream on a document that has lots of fields that update continuously (via some logic on the insert/update end that calls $set with only the fields that changed), but I would like to watch only for changes to a specific field. My current attempt at this is below but I just get every update even if the field isn't part of the update. I think the "updateDescription.updatedFields" is what I am after but the code I have so far just gives

Sort by date in mongoose aggregation framework

拜拜、爱过 提交于 2020-08-01 09:58:27
问题 Im working on a nodejs+mongodb project using mongoose. Now I have come across a question I don't know the answer to. I am using aggregation framework to get grouped results. The grouping is done on a date excluding time data field like: "2013 02 06". Code looks like this: MyModel.aggregate([ {$match: {$and: [{created_date: {$gte: start_date}}, {created_date: {$lte: end_date}}]}}, {$group: { _id: { year: {$year: "$created_at"}, month: {$month: "$created_at"}, day: {$dayOfMonth: "$created_at"}

$project: Is it possible to access a property of an expression result in just one single stage?

我们两清 提交于 2020-07-30 07:14:08
问题 Is there any way to carry out the following operation in just one $project stage? db.getCollection('users').aggregate([ { $project : { firstEmail : { $arrayElemAt : ["$emails", 0] } } }, { $project : { domain : "$firstEmail.domain" } } ]) 回答1: You need $let operator: db.getCollection('users').aggregate([ { $project : { domain : { $let: { vars: { firstEmail: { $arrayElemAt : ["$emails", 0] } }, in: "$$firstEmail.domain" } } } } ]) Mongo Playground 回答2: When working with array of objects, you

MongoDb Aggregation: How can I group an array-1 based on another array-2 when given array-1 and array-2?

人盡茶涼 提交于 2020-07-28 04:24:32
问题 EDIT: My original question was MongoDb Aggregation: Can you $unwind an input document variable in the pipline of a $lookup stage? Consider the code below: {$lookup: { from:"mydoc", let: {"c":"$myArray"}, pipeline: [ {$unwind: "$$c"}, ] as:"myNewDoc" }} How would I unwind c if I wanted to? /////END OF ORIGINAL QUESTION -----EDIT----- From Tom Slabbaert's comment we now know that it is possible to $unwind an input document variable in the pipline of a $lookup stage. But it is not recommended.