aggregation-framework

How to dynamically build mongodb query

两盒软妹~` 提交于 2020-01-02 16:07:41
问题 I have a match expression in a mongodb aggregation. There are 3 fields that are included in the match but they don't all always contain data. I only want to include the fields in the match if the field isn't empty. This is what the match looks like if all fields have data but for example, if the array used for studentGradeLevels is empty, then I don't want to include it or I want the query to still return data ignoring the empty parameter. $match: { "school._id": "7011", "studentGradeLevels":

How to dynamically build mongodb query

允我心安 提交于 2020-01-02 16:07:25
问题 I have a match expression in a mongodb aggregation. There are 3 fields that are included in the match but they don't all always contain data. I only want to include the fields in the match if the field isn't empty. This is what the match looks like if all fields have data but for example, if the array used for studentGradeLevels is empty, then I don't want to include it or I want the query to still return data ignoring the empty parameter. $match: { "school._id": "7011", "studentGradeLevels":

$sum from documents and subdocuments group by “$author” (MongoDB)

旧时模样 提交于 2020-01-02 13:33:10
问题 This is my collection: { "_id" : 10926400, "votes": 131, "author": "Jesse", "comments" : [ { "id" : 1, "votes": 31, "author": "Mirek" }, { "id": 2, "votes": 13, "author": "Leszke" } ] }, { "_id" : 10926401, "votes": 75, "author": "Mirek", "comments" : [ { "id" : 1, "votes": 17, "author": "Jesse" }, { "id": 2, "votes": 29, "author": "Mirek" } ] } And I want $sum values of votes and comments.votes of each author expected output( sort $votes: -1 ): "Mirek" total votes: 31 + 75 + 29 = 135 "Jesse"

Include fields in mongodb aggregate

早过忘川 提交于 2020-01-02 09:00:09
问题 I have the following collection: {"orderID" : "30688", "branch" : "CO", "customerID" : "11396783", "customerEmail" : "foo@bar.com"} {"orderID" : "30688", "branch" : "CO", "customerID" : "11396783", "customerEmail" : "foo@bar.com"} {"orderID" : "30688", "branch" : "CO", "customerID" : "11396783", "customerEmail" : "foo@bar.com"} {"orderID" : "89765", "branch" : "CO", "customerID" : "54157526", "customerEmail" : ""} {"orderID" : "89765", "branch" : "CO", "customerID" : "54157526",

Conditionally include a field (_id or other) in mongodb project aggregation?

一曲冷凌霜 提交于 2020-01-02 06:59:30
问题 I've got a mongodb aggregation pipeline with a $project stage and I'd like to include certain fields only if conditions are met. Specifically, I'd like to exclude the _id in one condition and include a second field 'second_id' in the other condition. I know that it's not possible (yet) to exclude fields from a mongodb $project, but is it possible to conditionally include them? Is there a way to conditionally exclude the _id field? It accepts a 0 or 1, but what if I want to determine that 0 or

Query for latest version of a document by date in mongoDB

廉价感情. 提交于 2020-01-02 06:59:10
问题 I am trying to find a mongoDB script which will look at a collection where there are multiple records of the same document and only provide me with the latest version of each document as a result set. I cannot explain it in English any better than above but maybe this little SQL below might explain it further. I want each document by transaction_reference but only the latest dated version ( object_creation_date ). select t.transaction_reference, t.transaction_date, t.object_creation_date, t

pymongo aggregate don't allow explain option

試著忘記壹切 提交于 2020-01-02 06:47:05
问题 I succesfully run: result = my_col.aggregate(my_pipeline, allowDiskUse=True) Now when I try: result = my_col.aggregate(my_pipeline, allowDiskUse=True, explain=True) it fails saying: pymongo.errors.ConfigurationError: The explain option is not supported. Use Database.command instead. Thus I try so as to add the needed option: result = mydb.command('aggregate', 'mycol', my_pipeline, {'explain':True}) but it fails saying: pymongo.errors.OperationFailure: 'pipeline' option must be specified as an

Mongo error when using aggregation: sort exceeded memory limit

生来就可爱ヽ(ⅴ<●) 提交于 2020-01-02 05:55:13
问题 I get the mongo error exceeded memory limit with error code 16819 when I use aggregation sort. Im using mongo 2.6. The query is as follows: db.BASE_TABLE_CREATION_ExecuteHiveScript_26_V0.aggregate([ { "$project" : { "visitor_localdate" : 1 , "_id" : 0}}, { "$sort" : { "visitor_localdate" : -1}} ]) 回答1: By default aggregation in MongoDB occurs in memory and pipeline stages have limit of 100 Mb RAM. Looks like you have exceeded this threshold. To handle large dataset you should enable

How to order MongoDB Aggregation with match, sort, and limit

天大地大妈咪最大 提交于 2020-01-02 04:40:51
问题 My current aggregation is: db.group_members.aggregate({ $match: { user_id: { $in: [1,2,3] } } }, { $group: { _id: "$group_id" } }, { $sort: { last_post_at: -1 } }, { $limit: 5 }) For a document structure of: { _id: '...', user_id: '...', group_id: '...', last_post_at: Date, } I've also got an index on {user_id: 1, last_post_at: -1} Since my index is already on last_post_at is the sort useless? I'm not 100% sure how the ordering of this. My end goal is to replicate this SQL: SELECT DISTINCT ON

How to write multiple group by id fields in Mongodb java driver

家住魔仙堡 提交于 2020-01-02 04:08:28
问题 In the below query { $group : { _id : { success:'$success', responseCode:'$responseCode', label:'$label'}, max_timeStamp : { $timeStamp : 1 }, count_responseCode : { $sum : 1 }, avg_value : { $sum : "$value" }, count_success : { $sum : 1 } }} How _id : { success:'$success', responseCode:'$responseCode', label:'$label'}, can be translated to use in java mongodb driver. I tried BasicDBList list = new BasicDBList(); list.add(new BasicDBObject("success", "$success")); list.add(new BasicDBObject(