aggregation-framework

MongoDB lookup array of objects by field (join conditions and uncorrelated sub-queries)

岁酱吖の 提交于 2020-06-28 03:56:41
问题 I don't understand what I'm missing with the aggregation lookup using join conditions and uncorrelated sub-queries. processes collection: { _id: 'p1', steps: [ { _id: 'ps1', step: 's1', time: 10 }, { _id: 'ps2', step: 's2', time: 15 } ] } steps collection (for the document with _id: s1 ): { _id: 's1', name: 'step 1' } Working aggregation (standard one, without join conditions and uncorrelated sub-queries): processes.aggregate([ { // match stage or whatever prior stage }, { $lookup: { from:

how can I find an array of values in a $match aggregation and group the result?

无人久伴 提交于 2020-06-28 02:54:13
问题 Note:I think it is not possible to use javascript code in this live editor this is my live code: https://mongoplayground.net/p/IM4rY2K7Alt I am new to Mongodb I have a lot of faith to understand this exercise to understand more about the topic of aggregations My problem is this. I have a collection with a structure like this: [ { "toystore": 22, "toystore_name": "Toystore A", "toys": [ { "toy": "buzz", "code": 17001, "price": 500 }, { "toy": "woddy", "code": 17002, "price": 1000 }, { "toy":

Mongoose group and count

蓝咒 提交于 2020-06-25 00:54:07
问题 Below is my mongodb structure: [{ _id: '111', items: [{ productId: '123' }] }, { _id: '222', items: [{ productId: '123' }, { productId: '456' }] }, { _id: '333', items: [{ productId: '123' }, { productId: '456' }, { productId: '789' }] }] And I expect to group and count productId so that the result to be: [{ productId: '123', count: 3 }, { productId: '456', count: 2 }, { productId: '789', count: 1 }] Well, I tried using aggregation like this, but I think I got it wrong: const aggregatorOpts =

$replaceRoot in mongodb

北城以北 提交于 2020-06-23 07:44:34
问题 I use aggregation framework for group by of multiple fields as { _id:{_id:"$_id",feature_type:"$feature_type",feature_name:"$feature_name"}, features: { $push: "$features" } } it give result like {_id: {_id:1,feature_type:"Test",feature_name:"Tests"}, features:["23423","32423","2342342"] } but I want result like {_id:1,feature_type:"Test",feature_name:"Tests", features:["23423","32423","2342342"] } how can i acheve this using aggregration framework. 回答1: You need to use $replaceRoot to change

$replaceRoot in mongodb

。_饼干妹妹 提交于 2020-06-23 07:44:32
问题 I use aggregation framework for group by of multiple fields as { _id:{_id:"$_id",feature_type:"$feature_type",feature_name:"$feature_name"}, features: { $push: "$features" } } it give result like {_id: {_id:1,feature_type:"Test",feature_name:"Tests"}, features:["23423","32423","2342342"] } but I want result like {_id:1,feature_type:"Test",feature_name:"Tests", features:["23423","32423","2342342"] } how can i acheve this using aggregration framework. 回答1: You need to use $replaceRoot to change

Transform keymap into vector using MongoDB framework

时间秒杀一切 提交于 2020-06-18 04:51:17
问题 I have documents like this one at collection x at MongoDB: { "_id" : ... "attrKeys": [ "A1", "A2" ], "attrs" : { "A1" : { "type" : "T1", "value" : "13" }, "A2" : { "type" : "T2", "value" : "14" } } } The A1 and A2 elements above are just examples: the attrs field may hold any number of keys of any name. The key names in attrs are stored in the attrNames field. I would like to use the MongoDB aggregation framework to transform that document into one like this: { "_id" : ... "attrs" : [ { "key"

can we write mongodb crud queries and aggregate query together?

北城以北 提交于 2020-06-15 07:09:07
问题 In MongoDB can we execute below written query? db.dbaname.find(userName:"abc").aggregate([]) else is there any other way we can execute CRUD and aggregate query together. 回答1: Short answer - No you can't do this : .find(userName:"abc").aggregate([]) aggregation-pipeline is heavily used for reads which is mostly similar to .find() but capable of executing complex queries with help of it's multiple stages & many aggregation-operators . there are only two stages in aggregation $out & $merge that

MongoDB: Map nested array in aggregate function

无人久伴 提交于 2020-05-31 04:00:12
问题 I am trying to update property name by using aggregation. Current document [ { _id: 5, payLoad: { grades: [ { grade_: 80, mean: 75 }, { grade_: 90, mean: 80 } ] } } ] expected document renaming grade_ to grade [ { _id: 5, payLoad: { grades: [ { grade: 80, mean: 75 }, { grade: 90, mean: 80 } ] } } ] To achieve this I am trying to map the aggregation db.collection.aggregate([ { $addFields: { payLoad: { grades: { $map: { input: "$grades", as: "grade", in: { grade: "$$grade.grade_", mean: "$

MongoDB pagination on arrays - Check if $slice reached the beginning of the array

泪湿孤枕 提交于 2020-05-31 02:24:45
问题 In my application i have an box where users can read comments. If some user scrolls down, it loads more and more comments, everything fine. I slice the array from the back to the begin because i use $push whenever somebody write some comment so i slice it like this $slice: [ -5 , 5] . This will slice the last 5 elements out of the array. The -5 is actually an variable in my aggregation pipeline. Whenever i loaded some comments i decrease the value from -5 to -10 and so on so on. The problem i

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

做~自己de王妃 提交于 2020-05-30 08:04:22
问题 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