aggregation-framework

Need to calculate the datetime difference for the datetime field saved in string format in MongoDB

梦想的初衷 提交于 2020-05-30 08:03:56
问题 Sample Documents : { "_id" : ObjectId("50ed90a55502684f440001ac"), "time" : "2020-05-14T14:12:46.058-0400", "ServerTimeStamp" : "2020-05-14T14:12:46.058-0400" } { "_id" : ObjectId("50ed90a55502684f440001ad"), "time" : "2020-05-14T15:12:46.058-0400", "ServerTimeStamp" : "2020-05-14T14:12:46.058-0400" } { "_id" : ObjectId("50ed90a55502684f440001ae"), "time" : "2020-05-14T18:12:46.058-0400", "ServerTimeStamp" :"2020-05-14T14:12:46.058-0400" } I would like to calculate the time difference of

How to $lookup by avoiding null values in mongodb aggregate

陌路散爱 提交于 2020-05-29 10:34:56
问题 In here i'm using $lookup to to a left join from other collections, the query works fine but when some records missing values it returns errmsg : $in requires an array as a second argument, found: null Heres the querying document structure : { "no" : "2020921008981", "sale" : { "soldItems" : [ { "itemId" : "5b55ac7f0550de00210a3b24", }, { "itemId" : "5b55ac7f0550de00215584re", } ], "bills" : [ { "billNo" : "2020921053467", "insurancePlanId" : "160", }, { "billNo" : "2020921053467",

MongoDB creates array of arrays in $group $push instead of flat array

偶尔善良 提交于 2020-05-27 18:35:35
问题 I am trying to group a set of documents after an $unwind operation. My documents look like this: { "_id" : ObjectId("5cdb5b5acadf5100019da2f4"), "allowedLocations" : [ { "type" : "country", "value" : "world", "label" : "World" } ], "disallowedLocations" : [ { "type" : "country", "value" : "CF", "label" : "Central African Republic" }, { "type" : "country", "value" : "CN", "label" : "China" } ], } { "_id" : ObjectId("5cdb5b5acadf5100019da2f4"), "allowedLocations" : [ { "type" : "country",

mongo aggregate result exceed maximum document size

元气小坏坏 提交于 2020-05-25 11:36:20
问题 I use mongo aggregate function to find duplicated documents in a collection, where the collections looks like the following: {_id, placement_id, placement_name, program_id, target} I need to find all the documents that have exactly the same fields except _id and placement_id, so this two documents are the same: {_id:3, placement_id:23, placement_name:"pl1", program_id:5, target:"-"} {_id:7, placement_id:55, placement_name:"pl1", program_id:5, target:"-"} The aggregate function I came up with

mongo aggregate result exceed maximum document size

被刻印的时光 ゝ 提交于 2020-05-25 11:36:07
问题 I use mongo aggregate function to find duplicated documents in a collection, where the collections looks like the following: {_id, placement_id, placement_name, program_id, target} I need to find all the documents that have exactly the same fields except _id and placement_id, so this two documents are the same: {_id:3, placement_id:23, placement_name:"pl1", program_id:5, target:"-"} {_id:7, placement_id:55, placement_name:"pl1", program_id:5, target:"-"} The aggregate function I came up with

MongoDB allowDiskUse not working..

纵饮孤独 提交于 2020-05-24 21:28:06
问题 Experts. I'm new to MongoDB, but know enough to get my self in trouble.. case in point: db.test.aggregate( [ {$group: {_id: {email: "$email", gender: "$gender"}, cnt: {$sum: 1}}}, {$group: {_id: "$_id.email", cnt: {$sum: 1}}}, {$match: {cnt: 2}} ], {allowDiskUse : true} ) and no matter what variations I try, I keep getting the same error ("Pass allowDiskUse:true to opt in"): Error("Printing Stack Trace")@:0 ()@src/mongo/shell/utils.js:37 ([object Array],[object Object])@src/mongo/shell

Is there an elseif thing in MongoDB to $cond while aggregating

江枫思渺然 提交于 2020-05-24 20:15:41
问题 So I need a custom field calculated in MongoDB as follows if( field1 =="A") ->customfield=10 else if(field1 =="B" )->customfield=20 else (field1 =="C" ) ->customfield=15 I'm using aggregation along with the $project statement. But the $cond operator doesn't allow elseif (subbranching the else) and merely allows two static branches if and else. Using a nested elseif causes "exception: field inclusion is not allowed inside of $expressions" Heres my query(which gives me the error) db.items

Count based on Condition and divide by # of records for time interval

给你一囗甜甜゛ 提交于 2020-05-24 05:37:05
问题 I have mongoplayground example ready for this question. https://mongoplayground.net/p/m_G-yBuX6uk Currently it shows data aggregation based on 15min time interval on some fields. There is a field called "presenceStatus" which has value either 0 or 1. Current result { "accountId": ObjectId("5e1fe45cd05bfb0cc549297d"), "applicationNumber": 30, "area": "area2", "avgZoneCountNumber": 0, "avgZoneCountNumberInstant": 0, "buildingId": ObjectId("5e1fe5e3d05bfb0cc5494146"), "createdAt": ISODate("1970

Group by id doesn't work in spring data mongodb

百般思念 提交于 2020-05-17 06:08:06
问题 Im trying to use Aggregation in Spring data with mongodb. After few stages unwind , lookup , match I come up with the sample following data which is after the projection, then I try to group it by _id . { "_id": 1, "name":"Maths" }, { "_id": 1, "name":"Maths" }, { "_id": 2, "name":"Science" }, { "_id": 2, "name":"Science" } The following mongo script is working perfectly. { $project: { name: 1 } }, { $group: { _id: '$_id', name: { $first: '$name' } } } When I do it in spring, group("_id")

Mongo aggregate query results in less document with sorting

随声附和 提交于 2020-05-16 22:40:46
问题 I have faced a weird issue while querying one of our collections and aggregating the result out to another collection. I am querying unique users who have done some action and projecting the count of action performed per user in the aggregation query. var result = db.getCollection('user_actions').aggregate( [ {"$match":{"createdAt":{"$gte":1585161000000,"$lt":1585247340000}}}, {"$group":{"_id":{"accountId":"$user.id"},"count":{"$sum":1}}}, {"$sort": {"count": -1}}, {"$project":{"_id":0,"count