Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

$filter "truthy" condition returns incorrect results #320

Closed
gak10100 opened this issue Mar 27, 2023 · 4 comments
Closed

$filter "truthy" condition returns incorrect results #320

gak10100 opened this issue Mar 27, 2023 · 4 comments
Labels

Comments

@gak10100
Copy link

gak10100 commented Mar 27, 2023

When using $filter with a "truthy" condition it filters out all results.

const result = aggregate(
	[
		{
			urls: [
				'http://google.com',
				'http://msn.com',
				null
			]
		}
	],
	[
		{
			"$project": {
				"urls": {
					"$filter" : {
						"input" : "$urls",
						"as" : "url",
						"cond" : "$$url"
					}
				}
			}
		}
	]
);

console.log(result[0]);

Expected Result

{ urls: [ 'http://google.com', 'http://msn.com'] }

Actual Result

{ urls: [] }
@gak10100
Copy link
Author

Please note that mongo filters out null and false but does NOT filter out empty string

@kofrasa
Copy link
Owner

kofrasa commented Mar 28, 2023

Thanks for reporting this bug. Need to improve testing across a number of operators given the examples aren't always sufficient.

Feel free to send a PR.

@kofrasa kofrasa added the bug label Mar 28, 2023
@gak10100
Copy link
Author

It appears $cond is inconsistent with mongo on empty string. I prefer mingo's implementation but for consistency.

{
	"$project": {
		"emptyString": { $cond: ['', true, false] },
		"null": { $cond: [null, true, false] },
		"undefined": { $cond: [undefined, true, false] },
		"-1": { $cond: [-1, true, false] },
		"0": { $cond: [0, true, false] },
		"1": { $cond: [1, true, false] }
	}
}

Mingo

{
  '0': false,
  '1': true,
  string: true,
  emptyString: false,
  null: false,
  undefined: false,
  '-1': true
}

Mongo

{
  '0': false,
  '1': true,
  string: true,
  emptyString: true,
  null: false,
  undefined: false,
  '-1': true
}

@kofrasa
Copy link
Owner

kofrasa commented Mar 30, 2023

It appears $cond is inconsistent with mongo on empty string. I prefer mingo's implementation but for consistency.

{
	"$project": {
		"emptyString": { $cond: ['', true, false] },
		"null": { $cond: [null, true, false] },
		"undefined": { $cond: [undefined, true, false] },
		"-1": { $cond: [-1, true, false] },
		"0": { $cond: [0, true, false] },
		"1": { $cond: [1, true, false] }
	}
}

Mingo

{
  '0': false,
  '1': true,
  string: true,
  emptyString: false,
  null: false,
  undefined: false,
  '-1': true
}

Mongo

{
  '0': false,
  '1': true,
  string: true,
  emptyString: true,
  null: false,
  undefined: false,
  '-1': true
}

Will address all remaining cases in #321.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants