aggregation-framework

How can i calculate price positive and negative price using mongodb or robomongo?

▼魔方 西西 提交于 2019-12-29 08:24:16
问题 below is my userpricing collection data { "_id" : ObjectId("584bc9ba420a6b189c510af6"), "user_id" : 1, "mobilenumber":"01234", "price" : 2000.0, "type" : "credit", }, { "_id" : ObjectId("584bc9ba420a6b189c510af6"), "user_id" : 1, "mobilenumber":"01234", "price" : -1000.0, "type" : "credit", }, { "_id" : ObjectId("584bc9ba420a6b189c3323w23"), "user_id" : 2, "mobilenumber":"04321", "price" : 1000.0, "type" : "credit", } here i want to calculate total postive and total negative price of all user

MongoDB Show children items in One to Many relationship

断了今生、忘了曾经 提交于 2019-12-29 08:15:20
问题 This is the example of 1: N relationship. There is a one root item which consists of the few items (children): { "_id" : ObjectId("52d017d4b60fb046cdaf4851"), "dates" : [ 1399518702000, 1399126333000, 1399209192000, 1399027545000 ], "dress_number" : "4", "name" : "J. Evans", "numbers" : [ "5982", "5983", "5984", "5985" ], "goals": [ "1", "0", "4", "2" ], "durations": [ "78", "45", "90", "90" ] } What I want to do is to show children data from the root item: { "dates": "1399518702000",

Mongo Sort by Count of Matches in Array

怎甘沉沦 提交于 2019-12-29 08:11:48
问题 Lets say my test data is db.multiArr.insert({"ID" : "fruit1","Keys" : ["apple", "orange", "banana"]}) db.multiArr.insert({"ID" : "fruit2","Keys" : ["apple", "carrot", "banana"]}) to get individual fruit like carrot i do db.multiArr.find({'Keys':{$in:['carrot']}}) when i do an or query for orange and banana, i see both the records fruit1 and then fruit2 db.multiArr.find({ $or: [{'Keys':{$in:['carrot']}}, {'Keys':{$in:['banana']}}]}) Result of the output should be fruit2 and then fruit1,

Get percentages with MongoDB aggregate $group

烈酒焚心 提交于 2019-12-29 06:11:09
问题 I'd like to get percentages from a group pipeline in a MongoDB aggregate. My data: { _id : 1, name : 'hello', type : 'big' }, { _id : 2, name : 'bonjour', type : 'big' }, { _id : 3, name : 'hi', type : 'short' }, { _id : 4, name : 'salut', type : 'short' }, { _id : 5, name : 'ola', type : 'short' } My request group by type, and count: [{ $group : { _id : { type : '$type' }, "count" : { "$sum" : 1 } } }] Result: [ { _id { type : 'big', }, count : 2 }, { _id { type : 'short', }, count : 3 } ]

Get percentages with MongoDB aggregate $group

旧城冷巷雨未停 提交于 2019-12-29 06:08:17
问题 I'd like to get percentages from a group pipeline in a MongoDB aggregate. My data: { _id : 1, name : 'hello', type : 'big' }, { _id : 2, name : 'bonjour', type : 'big' }, { _id : 3, name : 'hi', type : 'short' }, { _id : 4, name : 'salut', type : 'short' }, { _id : 5, name : 'ola', type : 'short' } My request group by type, and count: [{ $group : { _id : { type : '$type' }, "count" : { "$sum" : 1 } } }] Result: [ { _id { type : 'big', }, count : 2 }, { _id { type : 'short', }, count : 3 } ]

Getting unix timestamp in seconds out of MongoDB ISODate during aggregation

给你一囗甜甜゛ 提交于 2019-12-29 05:34:09
问题 I was searching for this one but I couldn't find anything useful to solve my case. What I want is to get the unix timestamp in seconds out of MongoDB ISODate during aggregation. The problem is that I can get the timestamp out of ISODate but it's in milliseconds. So I would need to cut out those milliseconds. What I've tried is: > db.data.aggregate([ {$match: {dt:2}}, {$project: {timestamp: {$concat: [{$substr: ["$md", 0, -1]}, '01', {$substr: ["$id", 0, -1]}]}}} ]) As you can see I'm trying

MongoDB Aggregation: Compute Running Totals from sum of previous rows

可紊 提交于 2019-12-29 04:03:45
问题 Sample Documents: { _id: ObjectId('4f442120eb03305789000000'), time: ISODate("2013-10-10T20:55:36Z"), value:1 }, { _id: ObjectId('4f442120eb03305789000001'), time: ISODate("2013-10-10T28:43:16Z"), value:2 }, { _id: ObjectId('4f442120eb03305789000002'), time: ISODate("2013-10-11T27:12:66Z"), value:3 }, { _id: ObjectId('4f442120eb03305789000003'), time: ISODate("2013-10-11T10:15:38Z"), value:4 }, { _id: ObjectId('4f442120eb03305789000004'), time: ISODate("2013-10-12T26:15:38Z"), value:5 } It's

MongoDB Aggregation: Compute Running Totals from sum of previous rows

雨燕双飞 提交于 2019-12-29 04:03:22
问题 Sample Documents: { _id: ObjectId('4f442120eb03305789000000'), time: ISODate("2013-10-10T20:55:36Z"), value:1 }, { _id: ObjectId('4f442120eb03305789000001'), time: ISODate("2013-10-10T28:43:16Z"), value:2 }, { _id: ObjectId('4f442120eb03305789000002'), time: ISODate("2013-10-11T27:12:66Z"), value:3 }, { _id: ObjectId('4f442120eb03305789000003'), time: ISODate("2013-10-11T10:15:38Z"), value:4 }, { _id: ObjectId('4f442120eb03305789000004'), time: ISODate("2013-10-12T26:15:38Z"), value:5 } It's

MongoDB to Use Sharding with $lookup Aggregation Operator

ぃ、小莉子 提交于 2019-12-29 03:20:06
问题 $lookup is new in MongoDB 3.2. It performs a left outer join to an unsharded collection in the same database to filter in documents from the “joined” collection for processing. To use $lookup , the from collection cannot be sharded. On the other hand, sharding is a useful horizontal scaling approach. What's the best practise to use them together? 回答1: As the docs you quote indicate, you can't use $lookup on a sharded collection. So the best practice workaround is to perform the lookup

Promote subfields to top level in projection without listing all keys

青春壹個敷衍的年華 提交于 2019-12-29 01:24:49
问题 I have documents like {'a': 1, 'z': {'b': 2, 'c': 3,}} . I want {'a': 1, 'b': 2, 'c': 3} . I can do this with aggregate({'$project': {'b': '$z.b', 'c': '$z.c'}}) Is it possible to do it without listing all of the keys in the subdocument manually? 回答1: With MongoDB 3.4 you can use $objectToArray and $arrayToObject with $replaceRoot in order to change this: db.wish.aggregate([ { "$replaceRoot": { "newRoot": { "$arrayToObject": { "$concatArrays": [ [{ "k": "a", "v": "$a" }], { "$objectToArray":