What the difference between MongoDB Realm JS and standard JS?

Hi, I’m working with MongoDB Realm Functions and found unexpected behavior while I was trying to check for network response status code.

const response = await context.http.get({ url: 'https://api.github.com/asd' });
console.log(response.statusCode); // 404
console.log(typeof 404); // number
console.log(typeof response.statusCode); // number
console.log(response.statusCode == 404); // true
console.log(response.statusCode === 404); // true
console.log([404].includes(404)); // true
console.log([404].includes(response.statusCode.valueOf())); // true
console.log([404].includes(response.statusCode)); // false

So I’m curious why both == and === evaluates to true while includes evaluates to false for a statusCode field which is number? Where I can read about the difference between MongoDB Realm JS and standard JS to improve my expectations? Thanks!

Hello @Anton_P,

I acknowledge the timeline since your post and it’s a fair question to raise :slight_smile:

There is a difference in process-engine for MongoDB Realm JS and standard JS. That said, MongoDB Realm does follow the standard JS specifications.

In this case == and === both returning true is expected as both operands are the same value and same type without any coercion mystery.

The include returning false here is a bug on our end. I have raised your concern with the engineering team and I will keep you posted on the update.

Genuinely appreciate your patience with us :slight_smile:

Cheers, :performing_arts:


The other strange thing I noticed today:

const objects1 = await db.collection("collection").distinct("_id");
console.log(typeof objects1[0]); // object
console.log(objects1[0]); // 61b102c0048b84e9c13e6429
console.log(objects1[0].valueOf()); // 61b102c0048b84e9c13e6429

const objects2 = await db.collection("collection").distinct("_id");
console.log(typeof objects2[0]); // object
console.log(objects2[0]); // ObjectID("61b102c0048b84e9c13e6429")
console.log(objects2[0].valueOf()); // ObjectID("61b102c0048b84e9c13e6429")

So when I use sort() operator on a received collection of ObjectIDs they are transformed to some other objects and I don’t even know how to get a value from such objects.

And of course it’s hard to work with ObjectId overall

const id1 = new BSON.ObjectId('6235acd8850c208d65896cf9');
const id2 = new BSON.ObjectId('6235acd8850c208d65896cf9');
console.log(id1 == id2); // false
console.log(id1.valueOf() == id2.valueOf()); // false - I thought I can use that =[
console.log(id1.toString() == id2.toString()); // true

Hello @Anton_P,

This is normal JS behaviour

const myObj1 = { id: 222 };
const myObj2 = { id: 222 };
console.log(myObj1 == myObj2); // false

There is a specific method to compare ObjectId.

var a = ObjectId("6235acd8850c208d65896cf9");
var b = ObjectId("6235acd8850c208d65896cf9");
console.log(a.equals(b)); // true

You can read more on ObjectId.

Cheers, :performing_arts:


This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.