I’m in migration from Meteor 2.16 to 3.3.2.
Sometimes, when I insert data on MongoDB with insertAsync method, on server I get the error “MongoServerError: $in needs an array”, data is saved.
Somebody had see this error ?
Does your code base have any query which use $in in the condition? I sometime had this error, and it’s exactly what it said. I passed undefined to that variable.
I am not sure it is possible to use $in in an insert statement. That is a query operator, so your error is not generated by an insert.
You need to be cautious with $in and have your arrays validated.
Better than explaining it with my own words, check this out (below). The bigger risk comes from Meteor being bundled with a version of MongoDB driver and you would need to test or check the new version behavior every time you change your Meteor version. If your driver ignores undefined (as mentioned below) your query matches all documents.
In recent versions of the MongoDB shell and drivers, the BSON Undefined type is deprecated.
- During writes: Drivers automatically convert
undefinedvalues tonullwhen inserting or updating documents. The field is stored asnull. - During queries: When you pass an
undefinedvalue to the$inoperator in a modern driver, the driver often ignores the field entirely from the query filter, or the query may look for documents where the field isnull(which also matches missing fields). This behavior does not result in returning all records.
How Queries are Affected
- If the driver ignores
undefined: The filter effectively becomes an empty object{}. An empty query filter matches all documents in the collection. - If the driver converts
undefinedtonull: A query likedb.collection.find({ field: { $in: [undefined] } })in a modern driver/shell would effectively becomedb.collection.find({ field: { $in: [null] } })or simplydb.collection.find({ field: null }).- A query for
{ field: null }in MongoDB matches documents where:- The field’s value is explicitly
null. - The field does not exist in the document at all.
- The field’s value is explicitly
- This will return only a subset of documents, not all records, unless every single document in your collection happens to either have a
nullvalue for that specific field or completely lack the field.
- A query for
Recommendation
The BSON Undefined type is deprecated. To ensure predictable behavior, you should:
- Avoid using
undefinedas a value in your application logic when interacting with MongoDB. - Use
nullexplicitly if you need to represent a missing or empty value. - Use the
$existsoperator to specifically query for documents where a field is missing (e.g.,db.collection.find({ field: { $exists: false } })).
Thank you guys, I need to investigate problem.
Usefull is advice from @paulishca to not using undefined but change with null.
Problem solved, I make a mistake in publish code for some collections, forgot to “await” a promise. This cause error in log server when pub/sub system works.
So, no problem in Meteor 3.3.2 ! ![]()