diff --git a/src/MongoDB.ts b/src/MongoDB.ts index 0f931db..464395d 100644 --- a/src/MongoDB.ts +++ b/src/MongoDB.ts @@ -194,7 +194,7 @@ class MongoDB if (typeof db !== 'string') throw new TypeError('Expecting collection name for the first argument'); - if (typeof query._id === 'string') + if (typeof query._id === 'string' && query._id.length === 24) query._id = new ObjectId(query._id); this.#logger.debug(`Incoming find query for ${db} with parameters ${inspect(query)}`); @@ -222,7 +222,7 @@ class MongoDB if (typeof db !== 'string') throw new TypeError('Expecting collection name for the first argument'); - if (typeof query._id === 'string') + if (typeof query._id === 'string' && query._id.length === 24) query._id = new ObjectId(query._id); this.#logger.debug(`Incoming findOne query for ${db} with parameters ${inspect(query)}`); @@ -250,7 +250,7 @@ class MongoDB throw new TypeError('Expecting collection name for the first argument'); if (!filter) throw new Error('Cannot run update many without a filter, if you mean to update every single document, pass an empty object'); - if (typeof filter._id === 'string') + if (typeof filter._id === 'string' && filter._id.length === 24) filter._id = new ObjectId(filter._id); this.#logger.debug(`Incoming update query for '${db}' with parameters\n${inspect(filter)}\nand data\n${inspect(data)}`); @@ -276,7 +276,7 @@ class MongoDB throw new Error('MongoDB not connected'); if (typeof db !== 'string') throw new TypeError('Expecting collection name for the first argument'); - if (typeof filter._id === 'string') + if (typeof filter._id === 'string' && filter._id.length === 24) filter._id = new ObjectId(filter._id); this.#logger.debug(`Incoming updateOne query for ${db} with parameters ${inspect(filter)}`); @@ -302,7 +302,7 @@ class MongoDB throw new Error('MongoDB not connected'); if (typeof db !== 'string') throw new TypeError('Expecting collection name for the first argument'); - if (typeof data._id === 'string') + if (typeof data._id === 'string' && data._id.length === 24) data._id = new ObjectId(data._id); this.#logger.debug(`Incoming insertOne query for ${db} with parameters ${inspect(data)}`); @@ -319,7 +319,7 @@ class MongoDB throw new Error('MongoDB not connected'); if (typeof db !== 'string') throw new TypeError('Expecting collection name for the first argument'); - if (typeof filter._id === 'string') + if (typeof filter._id === 'string' && filter._id.length === 24) filter._id = new ObjectId(filter._id); this.#logger.debug(`Incoming deleteOne query for ${db} with parameters ${inspect(filter)}`); @@ -338,7 +338,7 @@ class MongoDB throw new Error('MongoDB not connected'); if (typeof db !== 'string') throw new TypeError('Expecting collection name for the first argument'); - if (typeof filter._id === 'string') + if (typeof filter._id === 'string' && filter._id.length === 24) filter._id = new ObjectId(filter._id); const endTimer = MongoDB.#queryHistogram?.startTimer({ type: 'findOneAndDelete' }); @@ -364,7 +364,7 @@ class MongoDB throw new Error('MongoDB not connected'); if (typeof db !== 'string') throw new TypeError('Expecting collection name for the first argument'); - if (typeof filter._id === 'string') + if (typeof filter._id === 'string' && filter._id.length === 24) filter._id = new ObjectId(filter._id); this.#logger.debug(`Incoming push query for ${db}, with upsert ${upsert} and with parameters ${inspect(filter)} and data ${inspect(data)}`); @@ -390,7 +390,7 @@ class MongoDB throw new Error('MongoDB not connected'); if (typeof db !== 'string') throw new TypeError('Expecting collection name for the first argument'); - if (typeof filter._id === 'string') + if (typeof filter._id === 'string' && filter._id.length === 24) filter._id = new ObjectId(filter._id); this.#logger.debug(`Incoming random query for ${db} with parameters ${inspect(filter)} and amount ${amount}`);