// Update a document
const result = await this.users.updateOne(
{_id: userId},
{$set: {lastLogin: new Date()}}
)
// Update and return the updated document
const updatedUser = await this.users.updateAndFind(
{_id: userId},
{$set: {status: 'active'}}
)
// Find one document and update it (same as updateAndFind)
const user = await this.users.findOneAndUpdate(
{_id: userId},
{$set: {status: 'active'}}
)
// Update multiple documents
const result = await this.users.updateMany(
{status: 'pending'},
{$set: {reminded: true}}
)
// Update a specific field in a document by path
await this.users.updateItem(
userDocument, // pass the actual document object
'addresses.0.isPrimary', // field path to update
true // new value
)
// Upsert (insert if not exists)
const result = await this.users.upsert(
{email: 'user@example.com'},
{
$set: {lastSeen: new Date()},
$setOnInsert: {createdAt: new Date()}
}
)