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

$not operator does not handle non-array values correctly. #146

Closed
redgeoff opened this issue Jul 20, 2020 · 2 comments
Closed

$not operator does not handle non-array values correctly. #146

redgeoff opened this issue Jul 20, 2020 · 2 comments

Comments

@redgeoff
Copy link
Contributor

I'd like to start by saying that your library looks awesome!

I'm considering making mingo the MongoDB query engine for MSON as it implements aggregation, a very needed concept that is currently missing.

My hold up is that in my quick tests, it looks like there may be a problem with basic operators like $not. For example, take a look at this codsandbox and you'll see that the output of bar is true and not the negation of foo. In other words, shouldn't bar=false?

For convenience, here is the code:

import { Aggregator } from "mingo/aggregator";
import "mingo/init/system";

const collection = [
  {
    foo: true
  }
];

let agg = new Aggregator([
  {
    $project: {
      bar: {
        $not: "$foo"
      }
    }
  }
]);

let result = agg.run(collection);
console.log(result);

By comparison, this is how MongoDB handles this same aggregation. Please note that the Result has bar=false

Configuration:

[
  {
    foo: true
  }
]

Query:

db.collection.aggregate([
  {
    $project: {
      bar: {
        $not: "$foo"
      }
    }
  }
])

Result:

[
  {
    "_id": ObjectId("5a934e000102030405000000"),
    "bar": false
  }
]
@kofrasa
Copy link
Owner

kofrasa commented Jul 20, 2020

Thanks and I appreciate you considering this library.

On the issue itself, the $not operator is actually implemented although it seems there is an inconsistency with MongoDB.

The documentation does say the value for the expression when used in an aggregation context must be [ <expression> ], that is, it must be wrapped in an array. If you changed your example query to bar: { $not: [ "$foo" ] }, you will get the expected result. See example

Nonetheless given the inconsistency it is fair to treat this a bug. The documentation alone does not constitute a spec of any kind.

@redgeoff
Copy link
Contributor Author

Awesome, thanks for the clarification! I'll leave this issue open to leave a record of the bug.

kofrasa added a commit that referenced this issue Jul 20, 2020
MongoDB accepts either a plain value or array with single element as per the documentation.
@kofrasa kofrasa changed the title $not operator not supported? $not operator does not handle non-array values. Jul 20, 2020
@kofrasa kofrasa changed the title $not operator does not handle non-array values. $not operator does not handle non-array values correctly. Jul 20, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants