Operators for MongoDB/BSON numeric types
devkev opened this issue · comments
eg. $numberInt
, $numberLong
, $numberDecimal
Looks like these are the main things from the BSON spec which are missing in mgenerate.js. They would vaguely work like change.integer()
, and/or maybe also change.dollar()
for $numberDecimal
.
Code is also missing (but it's very infrequently encountered). So are deprecated BSON types like DBPointer/DBRef, Symbol, and undefined (but being deprecated, they don't really matter).
Those are already there, just not documented yet. Adding to README now.
Hi @rueckstiess
I have a json document with $date
, $numberLong
, $numberInt
. Using mgeneratejs 0.5.1 it fails on $numberInt
, while $date
and $numberLong
are ok.
node:events:505
throw er; // Unhandled 'error' event
^
Error: unknown operator: $numberInt
Message: TypeError: chance[op] is not a function
at callOperator (/usr/local/lib/node_modules/mgeneratejs/lib/index.js:59:11)
at evalValue (/usr/local/lib/node_modules/mgeneratejs/lib/index.js:111:14)
at /usr/local/lib/node_modules/mgeneratejs/node_modules/lodash/lodash.js:13469:38
at /usr/local/lib/node_modules/mgeneratejs/node_modules/lodash/lodash.js:4967:15
at baseForOwn (/usr/local/lib/node_modules/mgeneratejs/node_modules/lodash/lodash.js:3032:24)
at Function.mapValues (/usr/local/lib/node_modules/mgeneratejs/node_modules/lodash/lodash.js:13468:7)
at evalObject (/usr/local/lib/node_modules/mgeneratejs/lib/index.js:72:18)
at evalValue (/usr/local/lib/node_modules/mgeneratejs/lib/index.js:113:12)
at arrayMap (/usr/local/lib/node_modules/mgeneratejs/node_modules/lodash/lodash.js:653:23)
at Function.map (/usr/local/lib/node_modules/mgeneratejs/node_modules/lodash/lodash.js:9622:14)
Emitted 'error' event on Stream instance at:
at Stream.onerror (node:internal/streams/legacy:62:12)
at Stream.emit (node:events:527:28)
at Immediate.<anonymous> (/usr/local/lib/node_modules/mgeneratejs/node_modules/event-stream/index.js:176:16)
at processImmediate (node:internal/timers:466:21)
Hi @taatuut,
Thanks for reporting this. Initially I didn't add a $numberInt
operator, because $number
/ $integer
already exist in chance.js and can be used. But I realised they are actually producing numbers between -2^53 and +2^53-1 (Biggest number in Javascript).
To clean up this mess and make it more compatible with what MongoDB does, I did the following:
- Changed the behaviour of
$number
/$integer
to return a 32-bit integer with value between -2^31 and +2^31-1. - Added another alias
$numberInt
to map to$integer
. - Added this to the documentation here: https://github.com/rueckstiess/mgeneratejs#numberint
You can get the latest version by upgrading to 0.6 via npm update -g mgeneratejs
.
Hope that solves this issue.
Hi @rueckstiess, thanks for the quick action, works without issues now!