aggregation-framework

Mongo query not giving exact results for aggregate function

三世轮回 提交于 2020-01-04 05:28:25
问题 My mongo database contains a collection 'Shops' and the data is like below: { "_id" : ObjectId("XXXX1b83d2b227XXXX"), "ShopId" : 435, "products" : [ { "productId" : "1234", "productName" : "non veg", "productCategory" : "meals", "mrp" : "38", }, { "productId" : "5234", "productName" : "non veg", "productCategory" : "meals", "mrp" : "38", }, { "productId" : "6234", "productName" : "apple", "productCategory" : "juice", "mrp" : "38", }, { "productId" : "7234", "productName" : "non veg",

Empty array prevents document to appear in query

∥☆過路亽.° 提交于 2020-01-03 18:49:42
问题 I have documents that have a few fields and in particular the have a field called attrs that is an array. I am using the aggregation pipeline. In my query I am interested in the attrs (attributes) field if there are any elements in it. Otherwise I still want to get the result. In this case I am after the field type of the document. The problem is that if a document does not contain any element in the attrs field it will be filtered away and I won't get its _id.type field, which is what I

Mongodb: Get documents sorted by a dynamic ranking

时光总嘲笑我的痴心妄想 提交于 2020-01-03 04:36:29
问题 I have these documents: { "_id" : ObjectId("52abac78f8b13c1e6d05aeed"), "score" : 125494, "updated" : ISODate("2013-12-14T00:55:20.339Z"), "url" : "http://pictwittrer.com/1crfS1t" } { "_id" : ObjectId("52abac86f8b13c1e6d05af0f"), "score" : 123166, "updated" : ISODate("2013-12-14T00:55:34.354Z"), "url" : "http://bit.ly/JghJ1N" } Now, i would like to get all documents sorted by this dynamic ranking: ranking = score / (NOW - updated).abs ranking is a float value where: - score is the value of

mongo-go-driver aggregate query always return “Current”: null

[亡魂溺海] 提交于 2020-01-03 04:01:13
问题 I want to sum a field by using pipeline := []bson.M{ bson.M{"$group": bson.M{ "_id": "", "count": bson.M{ "$sum": 1}}} } ctx, _ := context.WithTimeout(context.Background(), 5*time.Second) result, err := collection.Aggregate(ctx, pipeline) but it always return "Current": null Any solution? 回答1: First, Collection.Aggregate() returns a mongo.Cursor, not the "direct" result. You have to iterate over the cursor to get the result documents (e.g. with Cursor.Next() and Cursor.Decode()), or use

mongodb group by first character

妖精的绣舞 提交于 2020-01-03 03:48:04
问题 I have a problem in mongodb. I want to create aggregation witch result will be like this: A 10 B 2 C 4 D 9 E 3 ... I have a column words in my table and I want to group my records according to first character of column words . I find resolve for sql but not for mongo. I will be very grateful for your help 回答1: You don't show what the docs in your collection look like, but you can use the aggregate collection method to do this: // Group by the first letter of the 'words' field of each doc in

Dynamic Sticky Sorting in Mongo for a simple value or list

倖福魔咒の 提交于 2020-01-03 03:35:27
问题 I'm trying to dynamically sticky sort a collection of records with the value that is sticky being different with each query. Let me give an example. Here are some example docs: {first_name: 'Joe', last_name: 'Blow', offices: ['GA', 'FL']} {first_name: 'Joe', last_name: 'Johnson', offices: ['FL']} {first_name: 'Daniel', last_name: 'Aiken', offices: ['TN', 'SC']} {first_name: 'Daniel', last_name: 'Madison', offices: ['SC', 'GA']} ... a bunch more names ... Now suppose I want to display the

How to merge two matching objects from different array into one object?

社会主义新天地 提交于 2020-01-03 03:23:10
问题 I have a situation where I have got one result from aggregation where I am getting data in this format. { "_id" : ObjectId("5a42432d69cbfed9a410e8ad"), "bacId" : "BAC0023444", "cardId" : "2", "defaultCardOrder" : "2", "alias" : "Finance", "label" : "Finance", "for" : "", "cardTooltip" : { "enable" : true, "text" : "" }, "dataBlocks" : [ { "defaultBlockOrder" : "1", "blockId" : "1", "data" : "0" }, { "defaultBlockOrder" : "2", "blockId" : "2", "data" : "0" }, { "defaultBlockOrder" : "3",

MongoDB : near + lookup in aggregation query

二次信任 提交于 2020-01-03 01:55:12
问题 I would like to know if this is even possible. Actually my query is : a near query on Users table + a forEach loop of find queries on UserSports table where user._id = userId of UserSports (it's like a join done in a foreach) : //geonear query db.users.find({ geopoint_mongo: {$near: {$geometry: {type: "Point", coordinates: [userGeopoint.lng, userGeopoint.lat]}, $maxDistance: maxDistance * 1000 } } }, {_id: 1, geopoint: 1, url_avatar: 1, bool_coach: 1, bool_ambassador: 1, gender: 1, bool

MongoDB max in group results

好久不见. 提交于 2020-01-03 01:40:28
问题 I grouped results by mvid and the counted number of occurrences in each group. db.some_details.aggregate([ {$group: {_id: "$mvid", count: {$sum: 1}}} ]) Now, I would like to select group with max count. Here is my naive guess: db.some_details.aggregate([ {$group: {_id: "$mvid", count: {$sum: 1}}}, {$max: "count"} ]) It, obviously, generates an error. I need to use max with group, but I don't have anything to group on. 回答1: What also would work is sorting the result and then using only the

How to group and select document corresponding to max within each group in MongoDB?

戏子无情 提交于 2020-01-02 19:31:44
问题 Here is my mongo collection 'sales': {"title":"Foo", "hash": 17, "num_sold": 49, "place": "ABC"} {"title":"Bar", "hash": 18, "num_sold": 55, "place": "CDF"} {"title":"Baz", "hash": 17, "num_sold": 55, "place": "JKN"} {"title":"Spam", "hash": 17, "num_sold": 20, "place": "ZSD"} {"title":"Eggs", "hash": 18, "num_sold": 20, "place": "ZDF"} I would like to group by hash and return document with the greatest "num_sold". So as output I would like to see: {"title":"Baz", "hash": 17, "num_sold": 55,