aggregation-framework

MongoDb Aggregation: How can I group an array-1 based on another array-2 when given array-1 and array-2?

匆匆过客 提交于 2020-07-28 04:20:52
问题 EDIT: My original question was MongoDb Aggregation: Can you $unwind an input document variable in the pipline of a $lookup stage? Consider the code below: {$lookup: { from:"mydoc", let: {"c":"$myArray"}, pipeline: [ {$unwind: "$$c"}, ] as:"myNewDoc" }} How would I unwind c if I wanted to? /////END OF ORIGINAL QUESTION -----EDIT----- From Tom Slabbaert's comment we now know that it is possible to $unwind an input document variable in the pipline of a $lookup stage. But it is not recommended.

MongoDb Aggregation: How can I group an array-1 based on another array-2 when given array-1 and array-2?

倾然丶 夕夏残阳落幕 提交于 2020-07-28 04:20:33
问题 EDIT: My original question was MongoDb Aggregation: Can you $unwind an input document variable in the pipline of a $lookup stage? Consider the code below: {$lookup: { from:"mydoc", let: {"c":"$myArray"}, pipeline: [ {$unwind: "$$c"}, ] as:"myNewDoc" }} How would I unwind c if I wanted to? /////END OF ORIGINAL QUESTION -----EDIT----- From Tom Slabbaert's comment we now know that it is possible to $unwind an input document variable in the pipline of a $lookup stage. But it is not recommended.

MongoDB: Using match with input document variables

混江龙づ霸主 提交于 2020-07-28 00:28:10
问题 Why do I have to use this code: { $match: { $expr: { <aggregation expression> } } } to match a document using a document input variable as opposed to doing: { $match: { <query> } } ? For example: $lookup: { from: "comments", let: { myvar: '$myInputDocVariable'}, pipeline: [ { $match: { $expr: { $and: [ { $eq: [ "$varFromCommentDocument", "$$myvar" ] }, ] } } }, ], as: "returnedValue" } The query above works fine but the query below does not work as expected. Why is this? Does this mean that

MongoDB: Using match with input document variables

≯℡__Kan透↙ 提交于 2020-07-28 00:27:36
问题 Why do I have to use this code: { $match: { $expr: { <aggregation expression> } } } to match a document using a document input variable as opposed to doing: { $match: { <query> } } ? For example: $lookup: { from: "comments", let: { myvar: '$myInputDocVariable'}, pipeline: [ { $match: { $expr: { $and: [ { $eq: [ "$varFromCommentDocument", "$$myvar" ] }, ] } } }, ], as: "returnedValue" } The query above works fine but the query below does not work as expected. Why is this? Does this mean that

How to output/ map an array with only the required fields after a filter in mongodb? [duplicate]

♀尐吖头ヾ 提交于 2020-07-22 05:51:26
问题 This question already has answers here : $project: Is it possible to access a property of an expression result in just one single stage? (2 answers) Closed 5 days ago . I am using this data: { "world": "Comic", "characters": [ { "character": "megaman", "type":"hero", "code":"123" }, { "character": "dr willow", "type":"villain", "code":"1234" }, { "character": "spiderman", "type":"hero", "code":"12345" }, { "character": "venom", "type":"villain", "code":"123456" } ] } With this code: db

MongoDb aggregate is reading same value multiple times

我们两清 提交于 2020-07-22 04:28:04
问题 I have this mongodb aggregate that is working and i am getting data in excel file. But in excel file i am seeing duplicate records even though it appears in the system for only one time. I am trying to understand why it is reading a value multiple times even though the filter value is occuring only once and not repeating itself. And how can i fix this issue? I know i need to make use of $group but i am having issue with its syntax so you help will be mich appreciated. Here is my aggregate db

Spring Data Mongo - How to get the nested distinct array for nested value?

ぃ、小莉子 提交于 2020-07-19 23:02:54
问题 I'm taking a reference from : Spring Data Mongo - Perform Distinct, but doesn't wants to pull embedded documents in results and asking another questions. I want to find technology list where "subdeptCd" : "1D". How can we do that ? { "firstName" : "Laxmi", "lastName" : "Dekate", ..... ....... ..... "departments" : { "deptCd" : "Tax", "deptName" : "Tax Handling Dept", "status" : "A", "subdepts" : [ { "subdeptCd" : "1D", "subdeptName" : "Tax Clearning", "desc" : "", "status" : "A" "technology"

Spring Data Mongo - How to get the nested distinct array for nested value?

99封情书 提交于 2020-07-19 23:02:21
问题 I'm taking a reference from : Spring Data Mongo - Perform Distinct, but doesn't wants to pull embedded documents in results and asking another questions. I want to find technology list where "subdeptCd" : "1D". How can we do that ? { "firstName" : "Laxmi", "lastName" : "Dekate", ..... ....... ..... "departments" : { "deptCd" : "Tax", "deptName" : "Tax Handling Dept", "status" : "A", "subdepts" : [ { "subdeptCd" : "1D", "subdeptName" : "Tax Clearning", "desc" : "", "status" : "A" "technology"

Conditionally Include Aggregation Pipeline Stages

若如初见. 提交于 2020-07-19 05:29:05
问题 I have a function which get me some orders based on given parameters. But, the parameter can be empty, in that case I want to leave the $match alone. This is the code which I currently have: if(req.query.status && typeof(req.query.status) == 'array'){ var match = { $in: req.query.status }; }else if(req.query.status){ var match = req.query.status; }else{ //when empty find all statuses var match = null; } Order.aggregate( { $match: { 'shop.nameSlug' : req.query.nameSlug, } }, { $unwind: "

How do I filter to return a subset of a mongoose schema?

我只是一个虾纸丫 提交于 2020-07-18 21:30:15
问题 My objective is to query mongoDB for a subset of an array within an array. For example, in the 'whole Data Set' (below), I would like to get all of the chorePerson records that have a personID of '5c6e3c74b9f5ed0016b00577' Whole Data Set [ { "_id": "5c7464a26b47a13470411031", "affiliation": "liss_family", "year": 2019, "weekNumber": 9, "chart": [ { "chorePerson": [ { "_id": "5c7464a26b47a13470411054", "person": "emily", "personID": "5c6e3c74b9f5ed0016b00577", "chore": "Catbox", "choreID":