aggregation-framework

Max and group by in Mongodb

情到浓时终转凉″ 提交于 2021-01-28 04:06:32
问题 First of all we are just migrating from SQL Server to Mongodb. I have a collection containing fields TFN, Impressions . I need to transform the sql query in mongo but got stuck at a moment. Scenario is I need to select a top 5 impressions from the collection which are group by on the basis of tfns Select Top 5 a.TFN, a.MaxImpression as MaxCount from ( Select TFN, Max(Impressions) MaxImpression from tblData Where TFN in (Select TFN From @tmpTFNList) and TrendDate between @StartDate AND

How to group data based from two collections in mongodb?

允我心安 提交于 2021-01-28 04:00:37
问题 Following are my two collections users:{ _id: "", email: "test@gmail.com", department: "hr" } details:{ _id: "", email: "abc@gmail.com" some_data:[ {user_email: "test@gmail.com", ....}, {user_email: "test1@gmail.com", ....}, {user_email: "test@gmail.com", ....}, ] } What I require is an output saying top 3 departments in the details based on email. Example: If I query for email: abc@gmail.com, I must get [ {department: "hr", count: 4}, {department: "finance", count: 3}, {department: "IT",

How to group data based from two collections in mongodb?

浪子不回头ぞ 提交于 2021-01-28 03:44:41
问题 Following are my two collections users:{ _id: "", email: "test@gmail.com", department: "hr" } details:{ _id: "", email: "abc@gmail.com" some_data:[ {user_email: "test@gmail.com", ....}, {user_email: "test1@gmail.com", ....}, {user_email: "test@gmail.com", ....}, ] } What I require is an output saying top 3 departments in the details based on email. Example: If I query for email: abc@gmail.com, I must get [ {department: "hr", count: 4}, {department: "finance", count: 3}, {department: "IT",

mongodb aggregate lookup with a query

断了今生、忘了曾经 提交于 2021-01-28 02:59:41
问题 I have collections with following values: reports { "_id": { "$oid": "5f05e1d13e0f6637739e215b" }, "testReport": [ { "name": "Calcium", "value": "87", "slug": "ca", "details": { "description": "description....", "recommendation": "recommendation....", "isNormal": false } }, { "name": "Magnesium", "value": "-98", "slug": "mg", "details": { "description": "description....", "recommendation": "recommendation....", "isNormal": false } } ], "patientName": "Patient Name", "clinicName": "Clinic",

MongoDB find subdocument and sort the results

一世执手 提交于 2021-01-28 02:56:13
问题 I have a collection in MongoDB with a complex structure and subdocuments. The document have an structure like this: doc1 = { '_id': '12345678', 'url': "http//myurl/...", 'nlp':{ "status": "OK", "entities": { "0": { "type" : "Person", "relevance": "0.877245", "text" : "Neelie Kroes" }, "1": { "type": "Company", "relevance": "0.36242", "text": "ICANN" }, "2": { "type": "Company", "relevance": "0.265175", "text": "IANA" } } } } doc2 = { '_id': '987456321', 'url': "http//myurl2/...", 'nlp':{

How to retrieve each single array element from mongo pipeline?

余生长醉 提交于 2021-01-28 00:44:34
问题 Let's assume that this is how a sample document looks like in mongo-db, [ { "_id": "1", "attrib_1": "value_1", "attrib_2": "value_2", "months": { "2": { "month": "2", "year": "2008", "transactions": [ { "field_1": "val_1", "field_2": "val_2", }, { "field_1": "val_4", "field_2": "val_5", "field_3": "val_6" }, ] }, "3": { "month": "3", "year": "2018", "transactions": [ { "field_1": "val_7", "field_3": "val_9" }, { "field_1": "val_10", "field_2": "val_11", }, ] }, } } ] The desired output is

Get distinct values from array based on conditions within the array

Deadly 提交于 2021-01-27 21:41:25
问题 I have a collection with documents like this: { "_id" : ObjectId("59df91dae4b03289d79efb4e"), "name" : "TEST", "payload" : [ { "_id" : "ABC", "status": "FALSE" }, { "_id":"DEF", "status": "TRUE" }, { "_id" : "XYZ", "status": "NULL" } ] } I am trying to find all the payload._id which have status as TRUE/FALSE. Expected result is ["ABC","DEF"] So far, I have something like this. db.collection.distinct('payload._id',{"_id" : "TEST"}) Not sure how to add conditions for elements within array. 回答1:

MongoDB aggregation to group dynamic keys

不打扰是莪最后的温柔 提交于 2021-01-27 20:06:36
问题 I'm working on an aggregation of counts of the value of a variable number of bar rows like in this data structure : > db.data.find() { "_id" : "foo1", "1" : { "bar" : 6 }, "0" : { "bar" : 11 }, "3" : { "bar" : 8 }, "2" : { "bar" : 0 }, "5" : { "bar" : 8 }, "4" : { "bar" : 19 }, "6" : { "bar" : 8 } } { "_id" : "foo2", "1" : { "bar" : 18 }, "0" : { "bar" : 3 }, "3" : { "bar" : 19 }, "2" : { "bar" : 0 }, "5" : { "bar" : 13 }, "4" : { "bar" : 17 }, "7" : { "bar" : 8 }, "6" : { "bar" : 8 }, "8" :

How to return all fields without specify after $group stage

橙三吉。 提交于 2021-01-27 10:47:35
问题 How to sort the child array in a document and keep the parent fields? I have a collection like this: { "_id" : ObjectId("5d1757929831633ff8abb638"), "name" : "Carraro Exlusive", "description" : "Carraro Exlusive description", "image" : "image-15", "__v" : 0, "sellers" : [ { "_id" : ObjectId("5d1757929831633ff8abb639"), "product" : ObjectId("5d1757929831633ff8abb638"), "seller" : ObjectId("5d1518582f9254189ca92e59"), "price" : 13, "stock" : 5, "__v" : 0 }, { "_id" : ObjectId(

$group inner array values without $unwind

↘锁芯ラ 提交于 2021-01-27 08:11:08
问题 I want to group objects in the array by same value for specified field and produce a count. I have the following mongodb document (non-relevant fields are not present). { arrayField: [ { fieldA: value1, ...otherFields }, { fieldA: value2, ...otherFields }, { fieldA: value2, ...otherFields } ], ...otherFields } The following is what I want. { arrayField: [ { fieldA: value1, ...otherFields }, { fieldA: value2, ...otherFields }, { fieldA: value2, ...otherFields } ], newArrayField: [ { fieldA: