aggregation-framework

Mongodb Aggregation Framework and timestamp

十年热恋 提交于 2019-12-25 11:35:06
问题 I have a collection { "_id" : 1325376000, "value" : 13393} { "_id" : 1325462400, "value" : 13393} ObjectIds are Unix Timestamp and are storing as Number manually.(at insert time). now I'm searching for a solution that i could calculate sum of values for each month with Aggregation Framework. 回答1: Here is a way you can do it by generating the aggregation pipeline programmatically: numberOfMonths=24; /* number of months you want to go back from today's */ now=new Date(); year=now.getFullYear();

Mongodb Aggregation Framework and timestamp

两盒软妹~` 提交于 2019-12-25 11:34:06
问题 I have a collection { "_id" : 1325376000, "value" : 13393} { "_id" : 1325462400, "value" : 13393} ObjectIds are Unix Timestamp and are storing as Number manually.(at insert time). now I'm searching for a solution that i could calculate sum of values for each month with Aggregation Framework. 回答1: Here is a way you can do it by generating the aggregation pipeline programmatically: numberOfMonths=24; /* number of months you want to go back from today's */ now=new Date(); year=now.getFullYear();

Mongoose Aggregate with lookup with group

耗尽温柔 提交于 2019-12-25 09:35:37
问题 I have invoice Model as following { ... "itemDetails": [ { "item": "593a1a01bbb00000043d9a4c", "purchasingPrice": 100, "sellingPrice": 150, "qty": 200, "_id": "59c39c2a5149560004173a05", "discount": 0 } ], "payments": [], ... } Item details Item is an object Id which refers to Item collection. I wanted to get sale by Item so I manage to work it out following way Invoice.aggregate([ { "$unwind": "$itemDetails" }, { "$group": { "_id": "$itemDetails.item", "qty": { "$sum": "$itemDetails.qty" },

Find based on calculated difference

安稳与你 提交于 2019-12-25 09:29:08
问题 I need to query my Mongo DB based on some parameters. However, to get the parameter, I first need to use a find() anyway and then do some calculations. This, obviously, is not efficient. Let me elaborate: Here is my collection I want to query: { "title": "1", "description": "1", "aCounter": 100, "bCounter": 20, "__v": 0 } I want to find posts where aCounter - bCounter is more than 50 . I have the model and everything ready but don't know about what to put in the find() parameters. postModel

MongoDb aggregation $lookup with foreign _ids in arrays

自古美人都是妖i 提交于 2019-12-25 09:27:10
问题 I'm a MongoDb novice. I'm getting pretty good, but no expert yet. I'm trying setup my collections in a way that makes sense. I'd like to keep some links to foreign docs inside arrays of just _ids and also arrays of objects that have _ids. I created a JSON doc with notes that I think fully shows what I'm trying to do... // ( item ) Character Inventory/Items collection [ { "_id": "1234", "name": "Sword", "descr": "Long sword, well worn, light rust", "encumber": 2, "del": false }, { "_id": "1271

Unable to compute average time

我们两清 提交于 2019-12-25 07:47:46
问题 3 days old into MongoDB, and I am not finding it very fluid. I am simply trying to compute the average time for a field but I keep running into all sorts of problems. Here is my code: db.results.group({ key:{"profile.Zend_Http_Client_Adapter_Socket::read==>fgets":{$exists:true}}, initial: {count: 0, total:0}, reduce: function(doc, out){ out.count++; out.total += doc."profile.Zend_Http_Client_Adapter_Socket::read==>fgets.wt"; }, finalize: function(out){ out.avg = out.total/out.count; } }); The

how to return response using mongo's aggregate?

烂漫一生 提交于 2019-12-25 07:45:09
问题 I'm starting with mongodb, I'm using aggregate function which gives me the last user of the last element into the sampleStatus array. (I mean the latest record added to sampleStatus) I have a collection of samples like this : { "_id" : ObjectId("58d6cbc14124691cd8154d72"), "correlativeCode" : "CSLLPA53E20M017W", "registrationMethod" : "taken", "originPlace" : "SOMEPLACE", "temperature" : 16, "sampleStatus" : [ { "nameStatus" : "status1", "place" : "place1", "rejectionReason" : "Nothing",

how to return response using mongo's aggregate?

谁都会走 提交于 2019-12-25 07:42:46
问题 I'm starting with mongodb, I'm using aggregate function which gives me the last user of the last element into the sampleStatus array. (I mean the latest record added to sampleStatus) I have a collection of samples like this : { "_id" : ObjectId("58d6cbc14124691cd8154d72"), "correlativeCode" : "CSLLPA53E20M017W", "registrationMethod" : "taken", "originPlace" : "SOMEPLACE", "temperature" : 16, "sampleStatus" : [ { "nameStatus" : "status1", "place" : "place1", "rejectionReason" : "Nothing",

Using multiple $lookup with aggregation in mongodb

廉价感情. 提交于 2019-12-25 05:07:07
问题 I have three collections, _project - It contains all the projects _build - It contains all the builds and every build must belong to a project _build.details - It contains ads which must belong to an adset and each adset must belongs to a campaign and each campaign must belongs to a build. _project document structure: { "_id" : ObjectId("58d8c501be2bee2bc0b3b081"), "CreatedBy" : ObjectId("58c801c606f72508d87421c6"), .... .... },... _build document structure: { "_id" : ObjectId(

creating covered index for aggregation framework

蓝咒 提交于 2019-12-25 04:26:57
问题 I have a problem with creating index for my query and can't find any similar solution on the web, so maybe some of you will help me. To simplify problem let's say we have Phones with some attributes, { "type":"Samsung", "model":"S3", "attributes":[{ "value":"100", "name":"BatteryLife" },{ "value":"200$", "name":"Price" } } With index: {"type":1, "attributes.value":1} We have millions of phones for every type and i want to find phones for given type that have given attributes, my query looks