0 is returned as { "$numberLong": "0" } 1 is returned as { "$numberInt": "1" }

I have a function which iterates over records, putting them into a hashmap to return to my flutter app. For the app I also add a numeric index value (which is needed later) by the app. The function returns two different types for this numeric value (it’s a position index used later) $numberLong for 0, $numberInt for 1. And this of course is a problem for the JSON parser package I’m using.

I’m curious as to the reason 0 is represented in EJSON one way for 0, while non-zero is represented another?

Is there a way in the function to declare this so zero and non-zero are returned with the same EJSON type?

My other choice is to use pass a ones-based index down then decrement when using, which i’d rather not do if there’s a better alternative.

Here is a test example function I wrote to show the output…

exports = async function(arg){
  // This default function will get a value and find a document in MongoDB
  // To see plenty more examples of what you can do with functions see: 
  // https://www.mongodb.com/docs/atlas/app-services/functions/
  var findResult = [];
  try {
    // Get a value from the context (see "Values" tab)
    // Update this to reflect your value's name.
    // var valueName = "value_name";
    // var value = context.values.get(valueName);
    for(var i = 0; i < 6; i++) {
      findResult.push(i);
    }

  } catch(err) {
    console.log("Error occurred while executing findOne:", err.message);

    return { error: err.message };
  }

  // To call other named functions:
  // var result = context.functions.execute("function_name", arg1, arg2);

  return { result: findResult };
};

And the results…

EJSON.parse('{"result":[{"$numberLong":"0"},{"$numberInt":"1"},{"$numberInt":"2"},{"$numberInt":"3"},{"$numberInt":"4"},{"$numberInt":"5"}]}')

I tried using 1 as the first number to start the counter, thinking 0 might be problem, but the first value returned is always $numberLong, the subsequent are always $numberInt

Blockquote

Hi @Josh_Whitehouse,

Unfortunately, Javascript only has ONE type, that is Number, so there’ll always be a bit of discrepancy when trying to set a representation: data that’s not coming from a collection (where a schema can be set to force a specific type) isn’t consistently represented by EJSON, can I ask what’s the actual use case?

What the function is returning is an object, so the fields are in fact just numbers: if you’re transmitting that result outside (say, in an HTTP Endpoint), you can also use JSON.stringify() to set the response body, so any JSON-compliant parser would work, EJSON is a possibility, but not the only one.

1 Like

the function is called from a flutter app - which uses a JSON parsing package, which makes scanning the JSON object returned for both $numberLong and $numberInt very tricky (the parser is constructs the flutter object from JSON in a strict fashion.

For now, I am going to workaround this by passing the value as a string, then converting it to a number in the flutter code once it’s been parsed.

Yes, that’s correct: the suggested way is to use JSON.stringify(result) as a returned value, to ensure everything is JSON-compatible, then use jsonDecode on that string on the client side