aggregation-framework

Keeping field in mongodb group by

别说谁变了你拦得住时间么 提交于 2021-02-08 11:15:37
问题 I have the following kind of docs in a collection in mongo db { _id:xx, iddoc:yy, type1:"sometype1", type2:"sometype2", date: { year:2015, month:4, day:29, type:"day" }, count:23 } I would like to do a sum over the field count grouping by iddoc for all docs where: type1 in ["type1A","type1B",...] where type2 in ["type2A","type2B",...] date.year: 2015, date.month: 4, date.type: "day" date.day between 4 and 7 I would like then to sort these sums. I know now how to do this (see this question) db

Keeping field in mongodb group by

淺唱寂寞╮ 提交于 2021-02-08 11:14:06
问题 I have the following kind of docs in a collection in mongo db { _id:xx, iddoc:yy, type1:"sometype1", type2:"sometype2", date: { year:2015, month:4, day:29, type:"day" }, count:23 } I would like to do a sum over the field count grouping by iddoc for all docs where: type1 in ["type1A","type1B",...] where type2 in ["type2A","type2B",...] date.year: 2015, date.month: 4, date.type: "day" date.day between 4 and 7 I would like then to sort these sums. I know now how to do this (see this question) db

Pentaho BI - MongoDB input Aggregation error due to recent MongoDB upgrade to 3.6

白昼怎懂夜的黑 提交于 2021-02-08 07:46:30
问题 Due to recent Mogodb upgrade to 3.6, pentaho kettle mongoinput step aggregation not be able to fetch data from the Mongodb. The error message: com.mongodb.MongoCommandException: Command failed with error 9: 'The 'cursor' option is required, except for aggregate with the explain argument' on server localhost:2915. The full response is { "ok" : 0.0, "errmsg" : "The 'cursor' option is required, except for aggregate with the explain argument", "code" : 9, "codeName" : "FailedToParse" } It seems

mongo db aggregate randomize ( shuffle ) results

为君一笑 提交于 2021-02-08 04:40:58
问题 I was going thru bunch of mongo docs and can't find a possibility to shuffle or randomize result content is there any ? 回答1: Specifically for the aggregation framework itself there is not really any native way as there is no available operator as yet to do something like generate a random number. So whatever match you could possibly project a field to sort on would not be "truly random" for lack of a shifting seed value. The better approach is to "shuffle" the results as an array after the

How to exclude _id without including other fields using the aggregation framework

怎甘沉沦 提交于 2021-02-08 03:37:30
问题 I would like to get the result of an aggregation pipeline without the _id filed. I know this is possible if you provide explicitly other fields that will be the output of the projection. But, ¿how can I mimic the $projec in a find call? This is what I want ( No field explicitly included ): db.col.find({},{_id:0}) But in the aggregation framework seems to be impossible: db.col.aggregate([{'$project': {_id:0}}]) Error: Printing Stack Trace at printStackTrace (src/mongo/shell/utils.js:37:15) at

How to exclude _id without including other fields using the aggregation framework

别说谁变了你拦得住时间么 提交于 2021-02-08 03:36:54
问题 I would like to get the result of an aggregation pipeline without the _id filed. I know this is possible if you provide explicitly other fields that will be the output of the projection. But, ¿how can I mimic the $projec in a find call? This is what I want ( No field explicitly included ): db.col.find({},{_id:0}) But in the aggregation framework seems to be impossible: db.col.aggregate([{'$project': {_id:0}}]) Error: Printing Stack Trace at printStackTrace (src/mongo/shell/utils.js:37:15) at

MongoDB $lookup on one document's array of object

感情迁移 提交于 2021-02-08 03:31:18
问题 I have searched online but could not find any match my case. Here is the situation. I am using aggregate to combine one collection and one document which is from another collection together restaurants.aggregate([ { $match: { _id: { $in: idList } } }, { $lookup: { from: "tags", localField: "details.restaurantType", foreignField: "details.restaurantType._id", as: "types" } }, { $project: { restaurantName: "$details.restaurantName", restaurantType: "$details.restaurantType", type: { $filter: {

$cond operator java code

做~自己de王妃 提交于 2021-02-07 07:59:49
问题 I have an aggregation query as follows : db.TWITTER_DATA_Processed.aggregate( {$match : { SpId : 840, Scheduler_id : "SCH_01" }},{$group : {_id : {SpId : "$SpId", Scheduler_id : "$Scheduler_id",Country : "$Country"}, positive_count : { $sum: { $cond: [ { $gt: [ "$Sentiment", 0 ] } , 1, 0 ] } }, neutral_count : { $sum: { $cond: [ { $eq: [ "$Sentiment", 0 ] } , 1, 0 ] } }, negative_count : { $sum: { $cond: [ { $lt: [ "$Sentiment", 0 ] } , 1, 0 ] } }}}) Sample output : { "_id" : { "SpId" : 840,

count of $lookup result mongodb

时光毁灭记忆、已成空白 提交于 2021-02-07 05:36:05
问题 Using mongodb with NodeJS driver I have 2 collections. One for department and other for students. Sample data for Deparmtent. { "_id" : ObjectId("5a24d20590d3d12155f3094e"), "name" : "CSE", "hod" : "abc", "students" : [ ObjectId("5a2129172c3e542acb78c1f5"), ObjectId("5a2129172c3e542acb78c1f7"), ObjectId("5a2129172c3e542acb78c1f9"), ObjectId("5a2129172c3e542acb78c1fb") ] } { "_id" : ObjectId("5a24d20590d3d12155f3094f"), "name" : "IT", "hod" : "xyz", , "students" : [ ObjectId(

Merging array fields in MongoDB aggregation

女生的网名这么多〃 提交于 2021-02-06 10:08:35
问题 Is it possible to merge array fields in while using MongoDB aggregation framework? Here is a summary problem I am trying to solve: Sample input documents for aggregation: { "Category" : 1, "Messages" : ["Msg1", "Msg2"], "Value" : 1 }, { "Category" : 1, "Messages" : [], "Value" : 10 }, { "Category" : 1, "Messages" : ["Msg1", "Msg3"], "Value" : 100 }, { "Category" : 2, "Messages" : ["Msg4"], "Value" : 1000 }, { "Category" : 2, "Messages" : ["Msg5"], "Value" : 10000 }, { "Category" : 3,