Hey guys,
I’m having a little issue with MongoDB and a query with a undefined field. For example, we use this query:
let oldTransaction = Transaction.findOne({transactionApiId:this.transactionApiId,transactionId, requestId});
In some cases, transactionId is undefined. The problem is, that this query returns a document that has a defined transactionId value. So the query gets changed to:
let oldTransaction = Transaction.findOne({transactionApiId:this.transactionApiId, requestId});
It ignores the transactionId field completely. Is this the normal behavior? In the console, the query can’t be executed due to
"errmsg" : "cannot compare to undefined",
Mongoose converts all undefined values to null values. But Meteor seems to remove the fields. Thats some behavior I didn’t expect.
any command issued on the client gets converted to an EJSON string. Part of that process removes any key where the value is undefined so {key: undefined} -> {}
In Mongo, you can use {key: {$exists: false}} which will allow you to find entries that have undefined values, rather than excluding the key.
However using the collection either wrapped by meteor collections or using the driver directly (as exposed by rawCollection()) the attribute is ignored.
So for example in this server side code, the undefined value omits the attribute from the selector turning it into a naked selector: