aggregation-framework

MongoDB aggreagte fill missing days [duplicate]

蹲街弑〆低调 提交于 2019-12-18 12:19:09
问题 This question already has answers here : Fill missing dates in records (4 answers) Closed 9 months ago . I have a product collection with the following documents: { "_id" : 1, "item" : "abc", created: ISODate("2014-10-01T08:12:00Z") } { "_id" : 2, "item" : "jkl", created: ISODate("2014-10-02T09:13:00Z") } { "_id" : 3, "item" : "hjk", created: ISODate("2014-10-02T09:18:00Z") } { "_id" : 4, "item" : "sdf", created: ISODate("2014-10-07T09:14:00Z") } { "_id" : 5, "item" : "xyz", created: ISODate(

Mongo find duplicates for entries for two or more fields

无人久伴 提交于 2019-12-18 11:53:01
问题 I have documents like this: { "_id" : ObjectId("557eaf444ba222d545c3dffc"), "foreing" : ObjectId("538726124ba2222c0c0248ae"), "value" : "test", } I want to find all documents which have duplicated values for pair foreing & value . 回答1: You can easily identify the duplicates by running the following aggregation pipeline operation: db.collection.aggregate([ { "$group": { "_id": { "foreing": "$foreing", "value": "$value" }, "uniqueIds": { "$addToSet": "$_id" }, "count": { "$sum": 1 } } }, { "

How to merge array field in document in Mongo aggregation

蹲街弑〆低调 提交于 2019-12-18 10:45:49
问题 I have one requirement where i need to do aggregation on two records both have an array field with different value. What I need that when I do aggregation on these records the result should have one array with unique values from both different arrays. Here is example : First record { Host:"abc.com" ArtId:"123", tags:[ "tag1", "tag2" ] } Second record { Host:"abc.com" ArtId:"123", tags:[ "tag2", "tag3" ] } After aggregation on host and artid i need result like this: { Host: "abc.com", ArtId:

Dynamic keys after $group by

。_饼干妹妹 提交于 2019-12-18 09:25:54
问题 I have following collection { "_id" : ObjectId("5b18d14cbc83fd271b6a157c"), "status" : "pending", "description" : "You have to complete the challenge...", } { "_id" : ObjectId("5b18d31a27a37696ec8b5773"), "status" : "completed", "description" : "completed...", } { "_id" : ObjectId("5b18d31a27a37696ec8b5775"), "status" : "pending", "description" : "pending...", } { "_id" : ObjectId("5b18d31a27a37696ec8b5776"), "status" : "inProgress", "description" : "inProgress...", } I need to group by

MongoDB lookup when foreign field is an array of objects

六月ゝ 毕业季﹏ 提交于 2019-12-18 09:14:56
问题 I have two collections initiatives and resources : initiative document example: { "_id" : ObjectId("5b101caddcab7850a4ba32eb"), "name" : "AI4CSR", "ressources" : [ { "function" : ObjectId("5c3ddf072430c46dacd75dbb"), "participating" : 0.1, }, { "function" : ObjectId("5c3ddf072430c46dacd75dbc"), "participating" : 5, }, { "function" : ObjectId("5c3ddf072430c46dacd75dbb"), "participating" : 12, }, { "function" : ObjectId("5c3ddf072430c46dacd75dbd"), "participating" : 2, }, ], } and a resource

Is there a floor function in Mongodb aggregation framework?

有些话、适合烂在心里 提交于 2019-12-18 08:14:18
问题 I try to do some integer math in mongodb and have some problems. Is there a way to round doubles up or down in the mongo db aggregation framework? 回答1: As per the comment from Stennie, the floor and ceiling methods are not supported. And as per my comment above, the best solution that I could find was just doing an operation that looks like: (num-mod(num,1)) which should yield the same result as the floor function. 回答2: Starting from MongoDB 3.2, you can use the following arithmetic

How to use $lookup as INNER JOIN in MongoDB Aggregation?

让人想犯罪 __ 提交于 2019-12-18 07:12:21
问题 I have used $lookup in my aggregate query. But as I am seeing it works as LEFT OUTER JOIN . I want to fetch exact matches document(INNER JOIN) with $lookup . Is there any way to get it done? This is my inventory collection: /* 1 */ { "_id" : 1, "sku" : "abc", "description" : "product 1", "instock" : 120 } /* 2 */ { "_id" : 2, "sku" : "def", "description" : "product 2", "instock" : 80 } /* 3 */ { "_id" : 3, "sku" : "ijk", "description" : "product 3", "instock" : 60 } /* 4 */ { "_id" : 4, "sku"

How to use $lookup as INNER JOIN in MongoDB Aggregation?

送分小仙女□ 提交于 2019-12-18 07:12:06
问题 I have used $lookup in my aggregate query. But as I am seeing it works as LEFT OUTER JOIN . I want to fetch exact matches document(INNER JOIN) with $lookup . Is there any way to get it done? This is my inventory collection: /* 1 */ { "_id" : 1, "sku" : "abc", "description" : "product 1", "instock" : 120 } /* 2 */ { "_id" : 2, "sku" : "def", "description" : "product 2", "instock" : 80 } /* 3 */ { "_id" : 3, "sku" : "ijk", "description" : "product 3", "instock" : 60 } /* 4 */ { "_id" : 4, "sku"

Query performance issue for large nested data in mongodb

徘徊边缘 提交于 2019-12-18 07:09:04
问题 I'm trying to query results from a large dataset called 'tasks' containing 187297 documents which are nested into another dataset called 'workers' , that's in its turn nested into a collection called 'production_units' . production_units -> workers -> tasks (BTW this is a simplified version of production_units): [{ "_id": ObjectId("5aca27b926974863ed9f01ab"), "name": "Z", "workers": [{ "name": "X Y", "worker_number": 655, "employed": false, "_id": ObjectId("5aca27bd26974863ed9f0425"), "tasks"

MongoDB group by values in an array field

若如初见. 提交于 2019-12-18 06:59:49
问题 I have my asset documents in the below format. db.asset.find({}).limit(1).pretty() { "_id" : ObjectId("54e650a10364a65f62c0df4a"), "_class" : "com.model.core.Asset", "displayName" : "Bingo Rhymes For Children + More 3D Animation Nursery Rhymes & Kids' Songs", "assetType" : "VIDEO", "active" : true, "originalUrl" : "https://www.youtube.com/watch?v=j-tdVvvXn9k&feature=youtube_gdata", "groupIds" : [ ], "folderIds" : [ "54e6507b0364a65f62c0df47", "54e6507b0364a65f62c0df48" ] } As you can see each