[SERVER-19259] $project expressions not working for documents inside arrays Created: 02/Jul/15  Updated: 03/Aug/15  Resolved: 03/Aug/15

Status: Closed
Project: Core Server
Component/s: Aggregation Framework
Affects Version/s: None
Fix Version/s: None

Type: Bug Priority: Major - P3
Reporter: guru rajan Assignee: Unassigned
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Attachments: PNG File output query 1.png     PNG File output query 2.png     File value.json    
Operating System: ALL
Steps To Reproduce:

1. Restore the attached value.json in any database
2. Execute the below query, output is correct, review at "output query 1.png"
 
db.salary.aggregate([{"$match":{"salary.amount":{$gte:5200}}}, {"$project":{"_id":0,"ID":1}} ]).toArray();
 
3. Execute the below query, output is wrong, review at "output query 2.png"
 
db.CaseDetail.aggregate([
{"$match":{"caseAssignedTo":"153"}},
{"$project":{"_id":0,"agreementNo":1,MI: {"$gte": ["$insuranceDetail.insuredAmount", 6000]}}} 
]).toArray();

Participants:

 Description   

Match condition is not working inside projection for Embedded dcouments



 Comments   
Comment by Ramon Fernandez Marina [ 03/Aug/15 ]

guruskec, if you run:

db.salary.aggregate([{"$project":{"_id":0,"agreementNo":1,MI:"$salary.amount"}}])

you'll see that the result documents look like this:

{ "MI" : [ 5000 ] }
{ "MI" : [ 5500 ] }
{ "MI" : [ 5500 ] }
{ "MI" : [ 5500 ] }
{ "MI" : [ 5500 ] }
{ "MI" : [ 5500 ] }

so to get the behavior you're looking for you need to take this into account and do

> db.salary.aggregate([{"$project":{"_id":0,"agreementNo":1,MI:{"$gte": ["$salary.amount", [5300] ]}}} ])
{ "MI" : false }
{ "MI" : true }
{ "MI" : true }
{ "MI" : true }
{ "MI" : true }
{ "MI" : true }

Please note that this only works when $salary.amount has one element. Since one can't reach into an array via dot notation in the projection stage we've added an array indexing operator in SERVER-4589, which is part of our development version. You may also want to take a look at the $unwind and $map operators, as they may help you achieve the behavior you want.

Regards,
Ramón.

Comment by guru rajan [ 02/Jul/15 ]

Please ignore Query 2 and use the below

db.salary.aggregate([{"$project":{"_id":0,"agreementNo":1,MI:

{"$gte": ["$salary.amount", 6000]}

}} ]).toArray();

Comment by Ramon Fernandez Marina [ 02/Jul/15 ]

guruskec, the values.json file does not contain any documents that satisfy the $match condition of the second aggregation pipeline. Can you please upload documents that do along with a description of the behavior you expect?

Thanks,
Ramón.

Generated at Thu Feb 08 03:50:21 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.