aggregation-framework

mongodb aggregation with $project to conditionally exclude a field

你离开我真会死。 提交于 2020-01-12 20:28:47
问题 I would like to exclude a field from a mongo db aggergtion pipeline, after reading the docs, I thought I needed to specify either 1 or 0 to keep the field or not (cf. http://docs.mongodb.org/manual/reference/operator/aggregation/project/) So I tried the following (using node.js mongoose, but the syntax is quite the same as plain mongo): aggregate.match({ date: { $gte : today } }); aggregate.sort('-date'); aggregate.group({ _id: '$name', date: { $first: '$date' }, user: { $first: '$user' },

mongodb aggregation with $project to conditionally exclude a field

我与影子孤独终老i 提交于 2020-01-12 20:28:31
问题 I would like to exclude a field from a mongo db aggergtion pipeline, after reading the docs, I thought I needed to specify either 1 or 0 to keep the field or not (cf. http://docs.mongodb.org/manual/reference/operator/aggregation/project/) So I tried the following (using node.js mongoose, but the syntax is quite the same as plain mongo): aggregate.match({ date: { $gte : today } }); aggregate.sort('-date'); aggregate.group({ _id: '$name', date: { $first: '$date' }, user: { $first: '$user' },

How to group by different fields

十年热恋 提交于 2020-01-11 08:36:12
问题 I want to find all users named 'Hans' and aggregate their 'age' and number of 'childs' by grouping them. Assuming I have following in my database 'users'. { "_id" : "01", "user" : "Hans", "age" : "50" "childs" : "2" } { "_id" : "02", "user" : "Hans", "age" : "40" "childs" : "2" } { "_id" : "03", "user" : "Fritz", "age" : "40" "childs" : "2" } { "_id" : "04", "user" : "Hans", "age" : "40" "childs" : "1" } The result should be something like this: "result" : [ { "age" : [ { "value" : "50",

Return the last Document From a Lookup

只愿长相守 提交于 2020-01-11 07:46:06
问题 db.groups.aggregate([ { $lookup: { from: "posts", localField: "_id", foreignField: "group", as: "post" } } ]) I'm getting response for groups and all post like.. [{geoup1,[post's array]}, {group2,[post's array]}] If there is any post I just want last added post into post collection 回答1: You can either use $slice db.groups.aggregate([ { "$lookup": { "from": "posts", "localField": "_id", "foreignField": "group", "as": "post" }}, { "$addFields": { "post": { "$slice": ["$post", -1] } }} ]) Or

Mongodb returning multiple sub array results and excluding other returned results

十年热恋 提交于 2020-01-11 07:21:08
问题 I have the following collection with a node array: { "_id" : ObjectId("55acf6187d4c31475417fa62"), "node" : [ { "-id" : "29331496", "-uid" : "1899168", "-changeset" : "26313303", "-lat" : "-37.6104102", "-lon" : "144.9459817", "-timestamp" : "2014-10-25T03:36:51Z", "-user" : "heyitsstevo", "-visible" : "true", "-version" : "6" }, { "-id" : "29331497", "-uid" : "1899168", "-version" : "2", "-lon" : "144.9451088", "-timestamp" : "2014-10-25T03:36:51Z", "-user" : "heyitsstevo", "-visible" :

How can I get top n buckets for an aggregation and all other buckets combined into an “other” bucket?

自闭症网瘾萝莉.ら 提交于 2020-01-11 05:16:50
问题 Assume a collection with schema like as shown below: { "customer" : <unique-id-for-customer>, "purchase" : <number>, } Now, I want to get the the top 5 customers (by purchase-queantity) and the 6th bucket is "others" which combines all the purchase quantity from other customers. Basically, the output of aggregation should be like: { "_id" : "customer100", "purchasequantity" : 4000000 } { "_id" : "customer5", "purchasequantity" : 81800 } { "_id" : "customer4", "purchasequantity" : 40900 } { "

Mongodb - aggregation $push if conditional

天涯浪子 提交于 2020-01-10 23:45:24
问题 I am trying to aggregate a batch of documents. There are two fields in the documents I would like to $push. However, lets say they are "_id" and "A" fields, I only want $push "_id" and "A" if "A" is $gt 0. I tried two approaches. First one. db.collection.aggregate([{ "$group":{ "field": { "$push": { "$cond":[ {"$gt":["$A", 0]}, {"id": "$_id", "A":"$A"}, null ] } }, "secondField":{"$push":"$B"} }]) But this will push a null value to "field" and I don't want it. Second one. db.collection

Spring mongo aggregation filter with $and, $in and $eq conditions

和自甴很熟 提交于 2020-01-10 06:11:06
问题 { "Field1": "ABC", "Field2": [ { "Field3": "ABC1","Field4": "REVIEWED","Field5":"True" }, { "Field3": "ABC2","Field4": "APPROVED","Field5":"True" }, { "Field3": "ABC3","Field4": "REJECTED","Field5":"True" }, { "Field3": "ABC4","Field4": "APPROVED","Field5":"False" } ] } I want to fetch APPROVED,REVIEWED and True record ie. { "Field1": "ABC", "Field2": [ { "Field3": "ABC1","Field4": "REVIEWED","Field5":"True" }, { "Field3": "ABC2","Field4": "APPROVED","Field5":"True" } ] } following mongo

MongoError: Unrecognized pipeline stage name: '$addFields'

我们两清 提交于 2020-01-10 03:51:19
问题 MongoError: Unrecognized pipeline stage name: '$addFields'. "mongoose": "^4.5.8" My sourcecode: Post.aggregate( [{ $addFields: { userName: { $concat: [ "$author.firstName", " ", "$author.lastName" ] } } //$project: { userName: { $concat: [ "$author.firstName", " ", "$author.lastName" ] } } //this is ok! }], function (err, result) { if (err) { console.log(err); return; } console.log(result); } ) Post model: let schema = { id: "post", properties: { content: {type: "string"}, author: { type:

Finding highest value from sub-arrays in documents

試著忘記壹切 提交于 2020-01-10 02:50:26
问题 Let say I have the following collection: { _id: 1, Array: [ { K: "A", V: 8 }, { K: "B", V: 5 }, { K: "C", V: 13 } ] } { _id: 2, Array: [ { K: "D", V: 12 }, { K: "E", V: 14 }, { K: "F", V: 2 } ] } I would like to run a query that returns the sub-document with the highest "V", so in that case I would get: { _id: 1, Array: [ { K: "E", V: 14 } ] } or simply: { K: "E", V: 14 } The important part is that I want the memory usage on the Mongo server to be O(1) (no matter how many documents I process,