aggregation-framework

MongoDB running total like aggregation of previous records up until occurrance of value

ぃ、小莉子 提交于 2019-12-24 10:00:36
问题 I am currently dealing with a set of in-game-events for various matches. In the game it is possible to kill enemies and purchase items in a shop. What I have been trying to do right now, is to count the number of kills that have occured in a single match up until every purchasing event. { "_id" : ObjectId("5988f89ae5873exxxxxxx"), "gameId" : NumberLong(2910126xxx) "participantId" : 3, "type" : "ITEM_PURCHASED", "timestamp" : 656664 }, { "_id" : ObjectId("5988f89ae5873exxxxxxx"), "gameId" :

Pymongo aggregation - passing python list for aggregation

蓝咒 提交于 2019-12-24 09:58:46
问题 Here is my attempt at performing the aggregation (day-wise) based on timestamp if all the elements are hardcoded inside the query. pipe = [ { "$match": { "cid": ObjectId("57fe39972b8dbc1387b20913") } }, { "$project": { "animal_dog": "$animal.dog", "animal_dog_tail": "$animal.dog.tail", "animal_cat": "$animal.cat", "tree": "$fruits", "day": {"$substr": ["$timestamp", 0, 10]} }}, { "$group": { "_id" : "$day", "animal_dog" : {"$sum": "$animal_dog"}, "animal_dog_tail": {"$sum": "$animal_dog_tail"

TypeError: callback.apply is not a function after allowDiskUse

点点圈 提交于 2019-12-24 09:08:04
问题 I have a collection having 1Million documents... I have passed the option for allowDiskUse and now it is throwing error TypeError: callback.apply is not a function I have searched for this but could get the solution... Please help const pictures = await Picture.aggregate([ { $sort: { createdAt: -1 }}, { $group: { _id: { $dateToString: { format: "%Y-%m-%d", date: "$createdAt" } }, pictures: { $push: { _id: '$_id', image: '$image', time: { $dateToString: { format: "%H:%M:%S", date: "$createdAt"

Using a Dynamic Value in Aggregation

て烟熏妆下的殇ゞ 提交于 2019-12-24 08:52:35
问题 I have a document structure like this: { "_id" : ObjectId("59d7cd63dc2c91e740afcdb"), "dateJoined": ISODate("2014-12-28T16:37:17.984Z"), "dateActivated": ISODate("2015-02-28T16:37:17.984Z"), "enrolled" : [ { "month":-10, "enrolled":'00'}, { "month":-9, "enrolled":'00'}, { "month":-8, "enrolled":'01'}, //other months { "month":8, "enrolled":'11'}, { "month":9, "enrolled":'11'}, { "month":10, "enrolled":'00'} ] } "month" value in enrolled is relative to dateJoined that range from -X to +X that

$facet aggregation in monogdb

流过昼夜 提交于 2019-12-24 08:16:09
问题 My Sample data looks like below: Category response Privacy 1 Mobile 1 Privacy 1 Privacy 0 Phishing 1 Mobile 1 Desktop 1 Desktop 0 Security 1 I have created an aggregate query to group all categories and get the count as below: db.cmi5DashboardData.aggregate([ {$group:{_id:'$category',knt:{$sum:'$response'}}}, {$sort:{knt:-1}}, {$project:{_id:0,category:'$_id',count:'$knt'}} ]) I get the output as below: Category count Privacy 2 Mobile 2 Phishing 1 Desktop 1 Security 1 However, I need to group

remove “\n” from values fields results in mongo db

99封情书 提交于 2019-12-24 08:09:36
问题 How can I remove "\n" from results of searching values in mongo db without remove it from the values of mongo db ? please I need your help! 回答1: Example with 2 documents: db.testcol.insert({ textstr: "Hello World\nBye World\nHello World\n" }) db.testcol.insert({ textstr: "Testing\n123\nTesting\n" }) db.testcol.find({}, {_id: 0, textstr: 1}).forEach(function(e, i) { e.textstr=e.textstr.replace(new RegExp('\n', 'g'), ""); printjson(e); }); Will output: { "textstr" : "Hello WorldBye WorldHello

How to project specific fields in array on filtered lookup

丶灬走出姿态 提交于 2019-12-24 08:06:16
问题 I am combining two collection using $lookup and I am able to apply a filter on the 'joined' collection and use a projection on the starting collection , but I did not manage to combine both a filter and a projection on the joined collection trying several approach using $redact and $project. I have looked intensively on stackoverflow, but I could not find this combination. Here come an example: collection meta: { "Exp": "A","test": "OK","date": "3"} { "Exp": "B","test": "OK","date": "5"} {

mongodb - unwinding nested subdocuments

淺唱寂寞╮ 提交于 2019-12-24 07:57:47
问题 For the following dataset example: lists { _id: 1, included_lists: [ 2 ], items: [ "i1" ]} { _id: 2, included_lists: [], items: [ "i2", "i3" ]} items { _id: "i1", details: [{}, {}, {}]} { _id: "i2", details: [{}, {}, {}]} { _id: "i3", details: [{}, {}, {}]} I want to grab all the items for a list, including the ones attached to the included_lists For example: if we're looking at list _id 1, we should get items i1, i2, i3 I have an idea how to do this, which involves using populate or $lookup

Upsert mongo array of object with condition

风格不统一 提交于 2019-12-24 07:45:18
问题 I have a collection with some documents and each document has ha list of objects representing a temporal interval in epoch with a keyword. I want to update the final value of the interval of each object in the right document where the ending value is greater than a value. If nothing can be updated I want to insert a new interval with both start and end as the new value and with the keyword used in the query. I'm using nifi to perform this task and the update block, with upsert enabled. I can

Upsert mongo array of object with condition

喜夏-厌秋 提交于 2019-12-24 07:42:04
问题 I have a collection with some documents and each document has ha list of objects representing a temporal interval in epoch with a keyword. I want to update the final value of the interval of each object in the right document where the ending value is greater than a value. If nothing can be updated I want to insert a new interval with both start and end as the new value and with the keyword used in the query. I'm using nifi to perform this task and the update block, with upsert enabled. I can