[SERVER-65323] when we are trying to use $ifnull inside $cond we r getting exat oposite result Created: 07/Apr/22  Updated: 27/Oct/23  Resolved: 12/Apr/22

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

Type: Bug Priority: Critical - P2
Reporter: jibanjyoti dhal Assignee: Chris Kelly
Resolution: Works as Designed Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Participants:

 Description   

[
{
'$match': {
'seatNo':

{ '$exists': true }

}
},

{ '$unwind': '$allowedFor' }

, {
'$project': {
'_id': 0,
'roomNo': 1,
'seatNo': 1,
'status': 1,
'allowedFor': 1,
'startTime': {
'$cond': [

{ '$ifNull': [ '$allowedFor.startTime', false ] }

, true, false
]
}
}
}
]

sample data = [{
"_id":

{ "$oid": "624e746b202bbc012c609cdd" }

,
"eNo": "0183",
"examCode": "E001",
"age": "03-15-1993",
"cat1": "Devloper",
"cat2": "Delhi",
"dob": "1993-03-15T00:00:00.000Z",
"email": "jibanjyoti444222@gmail.com",
"examName": "DEMO",
"gender": "Male",
"isPWD": false,
"name": "Jiban jyoti Dhal",
"password": "a123456",
"phone": 7008583514,
"registerOn": "2022-04-05T17:26:45.000Z",
"sUid": "5df5249496e2a95ad3317ff9959c24",
"shaPassword": "a123456",
"allowedFor": [

{ "allowed": true, "mappedOn": "2022-04-05T17:27:27.000Z", "attended": false, "subCode": "S001", "slotId": "Slot-1" }

],
"imageDownloaded": true,
"imageDownloadedOn":

{ "$date": "2022-04-07T10:49:40Z" }

,
"imageUrl": "candidateImage/5438.jpg",
"ip": "192.168.96.125",
"isFaceAuthMatched": false,
"roomNo": 3,
"seatNo": "3-5",
"sitAssignedBy": "satya",
"sitAssignedOn":

{ "$date": "2022-04-07T05:59:33.320Z" }

,
"status": "socketDisconnected"
},{
"_id":

{ "$oid": "624e746b202bbc012c609cde" }

,
"eNo": "5438",
"examCode": "E001",
"age": "09-01-1980",
"cat1": "Scientist-G",
"cat2": "Delhi",
"dob": "1980-08-31T23:00:00.000Z",
"email": "rkskrishna@gmail.com",
"examName": "DEMO",
"gender": "Male",
"isPWD": true,
"name": " R K Sahoo",
"password": "a123456",
"phone": 8280000062,
"registerOn": "2022-04-05T17:26:45.000Z",
"sUid": "e59910f481d8e83a2d917ff9959c25",
"shaPassword": "a123456",
"allowedFor": [
{
"allowed": true,
"mappedOn": "2022-04-05T17:27:27.000Z",
"attended": true,
"subCode": "S001",
"slotId": "Slot-1",
"isSmartlockEnable": "Yes",
"startTime":

{ "$date": "2022-04-07T11:37:16Z" }

,
"newSetNo": 1,
"assignQuestionlist": [
"E001S001SEC-16",
"E001S001SEC-11",
"E001S001SEC-13",
"E001S001SEC-15",
"E001S001SEC-17"
],
"endTime":

{ "$date": "2022-04-07T11:37:29Z" }

}
],
"imageDownloaded": true,
"imageDownloadedOn":

{ "$date": "2022-04-07T10:49:40Z" }

,
"imageUrl": "candidateImage/5438.jpg",
"ip": "192.168.96.125",
"isFaceAuthMatched": false,
"roomNo": 3,
"seatNo": "3-6",
"sitAssignedBy": "satya",
"sitAssignedOn":

{ "$date": "2022-04-07T05:59:33.320Z" }

,
"status": "socketDisconnected"
},{
"_id":

{ "$oid": "624ed7359834c764f37217ac" }

,
"eNo": "5439",
"examCode": "E001",
"age": "09-01-1980",
"cat1": "Scientist-G",
"cat2": "Delhi",
"dob": "1980-08-31T23:00:00.000Z",
"email": "rkskrishna@gmail.com",
"examName": "DEMO",
"gender": "Male",
"isPWD": true,
"name": " R K Sahoo",
"password": "a123456",
"phone": 8280000062,
"registerOn": "2022-04-05T17:26:45.000Z",
"sUid": "e59910f481d8e83a2d917ff9959c25",
"shaPassword": "a123456",
"allowedFor": [
{
"allowed": true,
"mappedOn": "2022-04-05T17:27:27.000Z",
"attended": true,
"subCode": "S001",
"slotId": "Slot-1",
"isSmartlockEnable": "Yes",
"startTime":

{ "$date": "2022-04-07T11:37:16Z" }

,
"newSetNo": 1,
"assignQuestionlist": [
"E001S001SEC-16",
"E001S001SEC-11",
"E001S001SEC-13",
"E001S001SEC-15",
"E001S001SEC-17"
]
}
],
"imageDownloaded": true,
"imageDownloadedOn":

{ "$date": "2022-04-07T10:49:40Z" }

,
"imageUrl": "candidateImage/5438.jpg",
"ip": "192.168.96.125",
"isFaceAuthMatched": false,
"roomNo": 3,
"seatNo": "3-8",
"sitAssignedBy": "satya",
"sitAssignedOn":

{ "$date": "2022-04-07T05:59:33.320Z" }

,
"status": "socketConnected"
}]

Problem Statement/Rationale

What is going wrong? What action would you like the Engineering team to take?

Steps to Reproduce

How could an engineer replicate the issue you're reporting?

Expected Results

What do you expect to happen?

Actual Results

What do you observe is happening?

Additional Notes

Any additional information that may be useful to include.



 Comments   
Comment by Chris Kelly [ 12/Apr/22 ]

Hi jibanjyoti442@gmail.com,

I went ahead and tested the query with your sample data, and $ifNull appears to be behaving as expected on this query. Specifically, it is following the behavior as described in MongoDB's documentation for $ifNull.

$ifNull is checking to see if the value is null, which it is doing successfully, and is setting the value of the startTime field in your projection to either true or false, depending on whether it is null. However, your projection displays both the true and false cases in the final result. You can filter this further by adding another $match pipeline stage which limits your results further based on which are true or false in this case. I.e.

{$match:  {"startTime": true} }

In the interest of performance, it is worth placing $match filters like this as early in your aggregation pipeline as possible. However, adding another $match at the end of your pipeline appears to resolve your immediate issue.

For future questions like this, I recommend asking the community for help by posting on the MongoDB Developer Community Forums.

If the discussion there leads you to suspect a bug in the MongoDB server, then we'd want to investigate it as a possible bug here in the SERVER project.

Regards,
Christopher

 

Comment by Tim Fogarty [ 07/Apr/22 ]

Hi, this Jira project is only for tracking development and bugfixes for the MongoDB Database Tools (mongoimport, mongodump, etc.). So this isn't the best place to get an answer to your question.

Since this has to do with MongoDB server behavior, I will move this ticket to the Server project. I would recommend also you post your question in the MongoDB Community Forum here: https://community.mongodb.com/.

Generated at Thu Feb 08 06:02:26 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.