aggregation-framework

MongoDB aggregation framework $subtract

北战南征 提交于 2019-12-23 05:39:15
问题 I'm want use mongodb to achieve simple query like mysql "select a-b from table", but aggregation framework query result is not right. data: { "_id" : ObjectId("511223348a88785127a0d13f"), "a" : 1, "b" : 1, "name" : "xxxxx0" } { "_id" : ObjectId("511223348a88785127a0d13f"), "a" : 2, "b" : 2, "name" : "xxxxx1" } mongodb cmd: db.site.aggregate([ { $match: { "a" : {$exists:true}, "b" : {$exists:true}, } }, { $project: { _id : 0,name : 1, r1: {$subtract:["$a", "$b"]} } }, { $limit: 100 }, ]);

Spring data mongodb application design and data aggregation

老子叫甜甜 提交于 2019-12-23 05:25:11
问题 I was developing a little application with spring data mongo and angularjs . Here is model : public class Lease { @Id private String id; private long created; private Lessor lessor; private Lessee lessee; } public class Payment { @Id private String id; private Integer month; private Integer year; private Long amount; private String leaseId; } I did not embed Payment model as I need it to have an id and edit it in its own form. On the main page of the app it represents a list of leases with

Spring data mongodb application design and data aggregation

…衆ロ難τιáo~ 提交于 2019-12-23 05:25:07
问题 I was developing a little application with spring data mongo and angularjs . Here is model : public class Lease { @Id private String id; private long created; private Lessor lessor; private Lessee lessee; } public class Payment { @Id private String id; private Integer month; private Integer year; private Long amount; private String leaseId; } I did not embed Payment model as I need it to have an id and edit it in its own form. On the main page of the app it represents a list of leases with

Group and Sum according to the position percentage MongoDb Aggregation

陌路散爱 提交于 2019-12-23 05:23:05
问题 I have to group by the values of myArray and sum the total with a different attribution, it's mean for example for this documents we should make a distribution to make 40% of total for the the first value of the array and 40%(2/5) of total for the last and 20%(1/5) for others shared equitaly : /* 1 */ { "_id" : ObjectId("5a535c48a4d86ed94a7e8618"), "total" : 5.8, "myArray" : [ { "value" : "a" 2/5 (40%) }, { "value" : "b 1/20 (20% /4) }, { "value" : "a" 1/20 (20% / 4) }, { "value" : "c" 1/20

Group and Sum according to the position percentage MongoDb Aggregation

吃可爱长大的小学妹 提交于 2019-12-23 05:22:12
问题 I have to group by the values of myArray and sum the total with a different attribution, it's mean for example for this documents we should make a distribution to make 40% of total for the the first value of the array and 40%(2/5) of total for the last and 20%(1/5) for others shared equitaly : /* 1 */ { "_id" : ObjectId("5a535c48a4d86ed94a7e8618"), "total" : 5.8, "myArray" : [ { "value" : "a" 2/5 (40%) }, { "value" : "b 1/20 (20% /4) }, { "value" : "a" 1/20 (20% / 4) }, { "value" : "c" 1/20

mongodb $where query to fetch sub-document content

只愿长相守 提交于 2019-12-23 04:43:10
问题 Below mongodb query is not returning the document. What is wrong with the query? I want to specify $where to match an element in array [EDIT] Above problem is a simplified version of my scenario. I am using a spring data mongodb repository. I want to specify the query using @Query annotation. Was hoping the $where part to be simple comparison and not a full fledged javascript function. something like @Query ( value=" { flag: true, $where: \"this.versions.version == this.defaultVersion \"}",

mongodb aggregate average of array elements

空扰寡人 提交于 2019-12-23 04:14:08
问题 I have collection of documents in mongodb 2.6.11 where 'cpu' is array showing [cpu0,cpu1], see example below { cpu: [ '100','20' ], hostname: 'host1',_id: 1 }, { cpu: [ '40','30' ], hostname: 'host1',_id: 2 }, etc I'm looking for average cpu on 'host1' which is ['70','25'] ( because '100'+'40'/2='70' and '20'+'30'='25' ). I am trying aggregate for cpu1 but it is not giving me right result db.collection.aggregate( [ { $group: { _id:"hostname", avgCPU: {$avg: "$cpu.1"} } } ] ); 回答1: In MongoDB

multi sum/count on mongodb (sum gender and total all results)

≡放荡痞女 提交于 2019-12-23 02:33:02
问题 i have this document: {gender:"male", ...}, {gender:"male", ...}, {gender:"female", ...}, {gender:"female", ...}, {gender:"female", ...}, So, i need retrive like { total:5, male:2, female:3 } my actual query(no work): db.collection.aggregate([ { $match:{...} }, { $group:{ _id:"$_id", gender:{ $push:"$gender" }, total:{ $sum:1 } } }, { $unwind:"$gender" }, { $group:{ _id:"$gender", name:{ $addToSet:"$all" }, "count":{ $sum:1 } } } ]) how i can retrive counter of gender and total? thanks 回答1:

MongoDB aggregation performance capability

旧城冷巷雨未停 提交于 2019-12-23 01:43:14
问题 I am trying to work through some performance considerations about using MongoDb for a considerable amount of documents to be used in a variety of aggregations. I have read that a collection has 32TB capcity depending on the sizes of chunk and shard key values. If I have 65,000 customers who each supply to us (on average) 350 sales transactions per day, that ends up being about 22,750,000 documents getting created daily. When I say a sales transaction, I mean an object which is like an invoice

Return 0 if $sum is null (aggregation framework)

天涯浪子 提交于 2019-12-23 00:54:11
问题 So I'm using aggregation framework to sum some results in given time blocks. My question is how do I return count: 0 every time if there was no results in the time block. So let's say in first ten seconds my count: 7 in next ten seconds count: 2 and then if in the third time block there was nothing I would like aggregate function to return count: 0 Below simple summing operation: $group: { _id: 1, count: { $sum: 1 } } I have tried using $ifNull , but it didn't work for me: $group: { _id: 1,