diff --git a/src/db/mongo/users.ts b/src/db/mongo/users.ts index f76b6d357..1b6a846f6 100644 --- a/src/db/mongo/users.ts +++ b/src/db/mongo/users.ts @@ -1,4 +1,4 @@ -import { OptionalId, Document } from 'mongodb'; +import { OptionalId, Document, ObjectId } from 'mongodb'; import { toClass } from '../helper'; import { User } from '../types'; import { connect } from './helper'; @@ -55,7 +55,9 @@ export const updateUser = async (user: User): Promise => { if (user.email) { user.email = user.email.toLowerCase(); } + const { _id, ...userWithoutId } = user; + const filter = _id ? { _id: new ObjectId(_id) } : { username: user.username }; const options = { upsert: true }; const collection = await connect(collectionName); - await collection.updateOne({ username: user.username }, { $set: user }, options); + await collection.updateOne(filter, { $set: userWithoutId }, options); };