aggregation-framework

Avoid Aggregate 16MB Limit

杀马特。学长 韩版系。学妹 提交于 2019-12-17 20:25:47
问题 I have a collection of about 1M documents. Each document has internalNumber property and I need to get all internalNumber s in my node.js code. Previously I was using db.docs.distinct("internalNumber") or collection.distinct('internalNumber', {}, {},(err, result) => { /* ... */ }) in Node. But with the growth of the collection I started to get the error: distinct is too big, 16m cap . Now I want to use aggregation. It consumes a lot of memory and it is slow, but it is OK since I need to do it

MongoDB Aggregate Sum Each Key on a Subdocument

本秂侑毒 提交于 2019-12-17 20:24:48
问题 I have multiple documents with this schema, each document is per product per day: { _id:{}, app_id:'DHJFK67JDSJjdasj909', date:'2014-08-07', event_count:32423, event_count_per_type: { 0:322, 10:4234, 20:653, 30:7562 } } I would like to get the sum of each event_type for a particular date_range. This is the output I am looking for where each event type has been summed across all the documents. The keys for event_count_per_type can be anything, so I need something that can loop through each of

Mongodb count distinct with multiple group fields

[亡魂溺海] 提交于 2019-12-17 20:14:02
问题 I have transaction table which is populated by holidays taken by the employees. I would need help on following sql scenario in mongodb. select employee,month,year,count(distinct (holiday_type) from transactions group by employee,month,year I need to use aggregation in mongodb and was created mongo query like this and this gives me wrong solution db.transactions.aggregate([ { "$group": { "_id": { "Month": { "$month" : "$date" }, "Year": { "$year" : "$date" }, "employee" : "$employee", "holiday

MongoDB count distinct value?

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-17 20:12:03
问题 Here below show my code. I have to calculate the how many times distinct value repeated. Here i have store distinct value in "results".I used collection.count() to calculate but it's not work. please any one tell me where i have to mistake. Thank you very much . var DistinctIntoSingleDB = function(Collection,arr,opt,distVal,callback){ Collection.find({}).distinct(distVal, function(err, results) { if(!err && results){ console.log("Distinct Row Length :", results.length); var a,arr1 = []; for

Map an 'array of objects' to a simple array of key values

你说的曾经没有我的故事 提交于 2019-12-17 19:49:24
问题 I am new to the mongoDB aggregation pipeline and have a really basic question but could not find the answer anywhere. I would like to simply convert the following block: "exclude" : [ { "name" : "Accenture" }, { "name" : "Aon Consulting" } ] to: "exclude" : [ "Accenture", "Aon Consulting" ] using the aggregation pipeline but I cannot seem to find how to do it even after going through the documentation on https://docs.mongodb.com/manual/reference/operator/aggregation/. Thanks for your help.

Compare two date fields in MongoDB

心已入冬 提交于 2019-12-17 19:38:13
问题 in my collection each document has 2 dates, modified and sync. I would like to find those which modified > sync, or sync does not exist. I tried {'modified': { $gt : 'sync' }} but it's not showing what I expected. Any ideas? Thanks 回答1: You can not compare a field with the value of another field with the normal query matching. However, you can do this with the aggregation framework: db.so.aggregate( [ { $match: …your normal other query… }, { $match: { $eq: [ '$modified', '$sync' ] } } ] ); I

Sorting aggregation addToSet result

强颜欢笑 提交于 2019-12-17 19:34:50
问题 Is there a way to get result of $addToSet as sorted array ? I tried to expand the pipeline and $unwind the array, sort it and group it again , but still the result isn't sorted. The arrays are pretty big and i try to avoid sort them in the the application. Document Example : { "_id" : ObjectId("52a84825cc8391ab188b4567"), "id" : 129624 "message" : "Sample", "date" : "12-09-2013,17:34:34", "dt" : ISODate("2013-12-09T17:34:34.000Z"), } Query : db.uEvents.aggregate( [ {$match : {dt : {$gte : new

“group by” queries on meteor collection

≡放荡痞女 提交于 2019-12-17 18:44:23
问题 My data mongoDB : >db.CUSTOMER.find() {"Name": "A", "CreatedDate": "Wed Jan 29 2014"} {"Name": "B", "CreatedDate": "Fri Jan 31 2014"} {"Name": "C", "CreatedDate": "Sat Feb 01 2014"} {"Name": "D", "CreatedDate": "Sat Feb 01 2014"} In meteor: Customer = new Meteor.Collection("CUSTOMER"); I'm trying to group them by date (Mon, Tues, Wed, ...) in meteor collection along with the total of the data. It should be something like this: {"Date": "Wed Jan 29 2014", "Total" 1} {"Date": "Fri Jan 31 2014",

MongoDB aggregation with $lookup limit some fields to return from query

隐身守侯 提交于 2019-12-17 18:34:14
问题 In mongo, after doing an aggregation with $lookup , I would like the request to return only some fields and not the whole document. I have the following query : db.somecollection.aggregate([{ $lookup: { from: "campaigns", localField: "campId", foreignField: "_id", as: "campaign" } }, { $unwind: "$campaign" }, { $lookup: { from: "entities", localField: "campaign.clientid", foreignField: "_id", as: "campaign.client" } }]); This request will return me this : { "_id" : ObjectId(

MongoDB Projection of Nested Arrays

亡梦爱人 提交于 2019-12-17 18:26:03
问题 I've got a collection "accounts" which contains documents similar to this structure: { "email" : "john.doe@acme.com", "groups" : [ { "name" : "group1", "contacts" : [ { "localId" : "c1", "address" : "some address 1" }, { "localId" : "c2", "address" : "some address 2" }, { "localId" : "c3", "address" : "some address 3" } ] }, { "name" : "group2", "contacts" : [ { "localId" : "c1", "address" : "some address 1" }, { "localId" : "c3", "address" : "some address 3" } ] } ] } Via q = { "email" :