Comments (6)
Thanks for report @stalniy
Unfortunately, mingo
only tests for identity when custom objects are supplied. It has no knowledge of the native MongoDB ObjectId
type.
That said, it makes sense to run custom equality checks for values of the same type on a best effort basis such as: comparing their toJSON()
or toString()
methods if present. I will consider this in the next version.
You can temporarily check against the string value of ObjectId
as a workaround.
On a side note, I wrote Mingo
first to target the client environment. Depending on the collection size, it will be more performant using the MongoDB query library directly if you are on the server.
from mingo.
@kofrasa ObjectID comparison was not completely fixed. There are several cases which doesn't work:
// oid - ObjectId constructor, id - instance of ObjectId
> new Query({ id }).test({ id: id })
true
> new Query({ id }).test({ id: oid(id.toString()) })
true
// the next cases fails
> new Query({ id }).test({ id: id.toString() })
false
> new Query({ id }).test({ id: id.toString() })
false
> new Query({ id: id.toString() }).test({ id })
from mingo.
Hi @stalniy
User-defined types can only be compared on a best effort basis with the requirement that operands are of the same type (see my earlier comment), so this is expected behaviour.
That means, comparing ObjectId
against String
will not work. However, two identical ObjectId
values will compare correctly (as your second tests show). Previously, the only check for identity was using the ===
operator but the current release has improved on it by using hashes.
The library is not aware of custom types including ones defined by MongoDB. It is trivial for users to pass an identical type, or use a primitive javascript type for the operands. I think the behaviour is sound as is.
I am open to discuss if you have a different viewpoint.
Thanks
from mingo.
Probably the best option is to allow to specify custom comparator for client types. For ObjectId
it may look like:
function compareObjectId(idA, idB) {
const [valueA, valueB] = [String(idA), String(idB)]
if (valueA === valueB) {
return 0
}
return valueA > valueB ? 1 : -1
}
Then
mingo.setup({
comparators: {
ObjectId: compareObjectId
}
})
what do you think?
from mingo.
This pattern can be confusing. I presume the use will be such: For unequal types, check whether the type name of any of the operands has a comparator. Use that to compare the types.
The problem lies in checking either operand (what order?). If the operands aren't restricted to be of the same type, then one can also mistakenly register different types which compare against each other circularly.
Example:
comparators: {
A: aCompareB,
B: bCompareC,
C: cCompareA
}
Obviously this is not sound, but mingo
cannot tell. I did consider adding a comparator for the purposes of sorting custom types but opted out for simplicity. Even so, the restriction that both operands are of the same type would still hold to retain the principle of least surprise.
So even with comparators subject to the restrictions mentioned, your examples will still not pass.
It does seem to me that it is still simpler to use JS native types to avoid surprising semantics.
from mingo.
I have same problem. I need a sort on ObjectId objects. Why not use valueOf function? Or convert object to string? Or allow specify to library a object to primitive function converter for compare.
from mingo.
Related Issues (20)
- Possible breaking error in 6.1.1 `dateFromParts` HOT 1
- core.addOperators is missing HOT 1
- How does generator function for collection work? HOT 2
- Expressions in arithmetic operators are evaluated in a nested path HOT 2
- Projection not working properly for deep nested objects HOT 1
- $Reduce $Map Returning incorrect results and making other fields return undefined.
- Es6 Module build HOT 1
- [email protected] breaks CommonJS requires of files inside package HOT 20
- Issues with $let since 6.2.0 HOT 1
- Strange behaviour in $round HOT 3
- Wrong evaluation with `NaN` value HOT 4
- TypeError: CreateListFromArrayLike called on non-object HOT 10
- Filter inside of map causes undefined variable item error
- Add support for `$linearFill` (aggregation window)
- Add support for $fill (aggregation)
- Add support for $densify (aggregation)
- $filter "truthy" condition returns incorrect results HOT 4
- Support useStrictMode for all truth value checks HOT 1
- Add support for $graphLookup (pipeline)
- aggregate() $sort stage on nested date replaces all items in collection with the last item (6.3.2) HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from mingo.