aggregation-framework

Mongo query to sort by distinct count

冷暖自知 提交于 2019-12-24 16:03:53
问题 I have two fields 'company' and 'url'. I want to sort it by number of times distinct 'company' is occurring and then display three 'url' corresponding to that particular company. Data is stored like this: { "_id" : ObjectId("56c4f73664af6f7305f3670f"), "title" : "Full Stack Software Developer", "url" : "http://www.indeed.com/cmp/Upside-Commerce,-Inc./jobs/Full-Stack-Software-Developer-6e93e36ea5d0e57e?sjdu=QwrRXKrqZ3CNX5W-O9jEvRQls7y2xdBHzhqWkvhd5FFfs8wS9wesfMWXjNNFaUXen2pO-kyc_Qbr7-

Failed Aggregation on Tornado/Motor: yielded unknown object MotorAggregationCursor

时光总嘲笑我的痴心妄想 提交于 2019-12-24 12:47:37
问题 I'm having issue to execute MongoDB aggregation operation on Tornado. This is the code, pipeline = [ {'$match': { '$or': [ {'owner.id': '56dfdaa4082024b9384c0055'}, {'members.top.member.id':'56dfdaa4082024b9384c0055'} ] }}, {'$sort': {'date_s': -1}}, {'$skip': 0}, {'$limit': 20}, {'$project':{ 'created_at': 1, 'name': 1, 'id': '$_id', 'group.group_id': 1, '_id': 0, 'permission': 1, 'owner': 1, 'type': 1, 'members.total': 1, 'desc': 1, 'declared': 1 }} ] cursor = yield db.activities.aggregate

Return a document with the max value on a number field using MongoDB aggregation

浪子不回头ぞ 提交于 2019-12-24 12:34:05
问题 If I have a bunch of documents for instance { _id: mongoId, subcatId: mongoId, viewCount: 2 } _id is unique but subcatId isn't. If I wanted to return each document that had the highest viewCount per subcatId, how would I do that using aggregation with Mongoose/MongoDB? 回答1: You can do that like this: db.test.aggregate([ // Sort the docs by viewCount descending so that the highest ones come first {$sort: {viewCount: -1}}, // Group by subcatId and take the first doc from each group {$group: {

$sum using Mongo DB aggreagation

白昼怎懂夜的黑 提交于 2019-12-24 12:18:26
问题 I have a document as Follows: {_id:1,"$field1":10,"$field2":20} Through mongo db java driver, can we update all the documents with a new field called $total which would sum of field1 and field2 THe equivalent in Relation database would like Update table set total=field1+field2 Is the same possible using mongo DB java driver without fetching records one by one. I have tried the following link and it doesnt give the result as specified in the link https://docs.mongodb.org/manual/reference

$add with some fields as Null returning sum value as Null

喜你入骨 提交于 2019-12-24 11:52:05
问题 These are the documents in my collection { "_id" : 1, "quizzes" : [ 10, 6, 7 ], "labs" : [ 5, 8 ], "final" : 80, "midterm" : 75 ,"extraMarks":10} { "_id" : 2, "quizzes" : [ 9, 10 ], "labs" : [ 8, 8 ], "final" : 95, "midterm" : 80 } { "_id" : 3, "quizzes" : [ 4, 5, 5 ], "labs" : [ 6, 5 ], "final" : 78, "midterm" : 70 } Only Document 1 has a field extra marks: Now i have to make a projection as a sum of "final"+"midterm"+"extraMarks" I have written a query for projection as follows: db.students

$add with some fields as Null returning sum value as Null

孤街浪徒 提交于 2019-12-24 11:48:29
问题 These are the documents in my collection { "_id" : 1, "quizzes" : [ 10, 6, 7 ], "labs" : [ 5, 8 ], "final" : 80, "midterm" : 75 ,"extraMarks":10} { "_id" : 2, "quizzes" : [ 9, 10 ], "labs" : [ 8, 8 ], "final" : 95, "midterm" : 80 } { "_id" : 3, "quizzes" : [ 4, 5, 5 ], "labs" : [ 6, 5 ], "final" : 78, "midterm" : 70 } Only Document 1 has a field extra marks: Now i have to make a projection as a sum of "final"+"midterm"+"extraMarks" I have written a query for projection as follows: db.students

Get Count of specific field mongodb

北城余情 提交于 2019-12-24 11:43:11
问题 I am using below query to get combined data from users and project collections: db.collection.aggregate([ { "$group": { "_id": "$userId", "projectId": { "$push": "$projectId" } } }, { "$lookup": { "from": "users", "let": { "userId": "$_id" }, "pipeline": [ { "$match": { "$expr": { "$eq": [ "$_id", "$$userId" ] }}}, { "$project": { "firstName": 1 }} ], "as": "user" } }, { "$unwind": "$user" }, { "$lookup": { "from": "projects", "let": { "projectId": "$projectId" }, "pipeline": [ { "$match": {

Mongo : Group and push more than 1 element

泄露秘密 提交于 2019-12-24 10:49:39
问题 I have a mongodb collection data as per below;I want to group Speciality,Code and Age fields based on EmployeedID( i.e 0001). { "_id" : ObjectId("54d0512191a4da7736e9db43"), "EmployeeID" : "0001", "Speciality" : "xxx", "Code" : "P", "Age" : 8 } /* 1 */ { "_id" : ObjectId("54d0512191a4da7736e9db44"), "EmployeeID" : "0002", "Speciality" : "yyyyy", "Code" : "P", "Age" : 6 } /* 2 */ { "_id" : ObjectId("54d0512191a4da7736e9db45"), "EmployeeID" : "0001", "Speciality" : "zzz", "Code" : "P", "Age" :

Group and count over a start and end range

穿精又带淫゛_ 提交于 2019-12-24 10:44:58
问题 If I have data in the following format: [ { _id: 1, startDate: ISODate("2017-01-1T00:00:00.000Z"), endDate: ISODate("2017-02-25T00:00:00.000Z"), type: 'CAR' }, { _id: 2, startDate: ISODate("2017-02-17T00:00:00.000Z"), endDate: ISODate("2017-03-22T00:00:00.000Z"), type: 'HGV' } ] Is it possible to retrieve data grouped by 'type', but also with a count of the type for each of month in a given date range e.g. between 2017/1/1 to 2017/4/1 would return: [ { _id: 'CAR', monthCounts: [ /*January*/ {

Mongo aggregation: partitioning values into groups

本秂侑毒 提交于 2019-12-24 10:24:04
问题 This is probably a longshot, but: I'd like to group a set of time-series documents by gaps between dates: sort the documents ascending by date, then partition when the interval between the current and previous is above some threshold. I can do this easily after getting the documents, of course; in this example, the original documents get a new partition number field: // assuming sorted docs var partition = 0; var partitioned = docs.map((e,i) => { if(i > 0) if(e.date - docs[i-1].date >