// Update a documentconst result = await this.users.updateOne( {_id: userId}, {$set: {lastLogin: new Date()}})// Update and return the updated documentconst 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 documentsconst result = await this.users.updateMany( {status: 'pending'}, {$set: {reminded: true}})// Update a specific field in a document by pathawait 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()} })
// Delete a documentconst result = await this.users.deleteOne({_id: userId})// Delete multiple documentsconst result = await this.users.deleteMany({ lastLogin: {$lt: new Date('2022-01-01')}})
// Count with filterconst activeCount = await this.users.countDocuments({status: 'active'})// Fast estimation for large collectionsconst totalCount = await this.users.estimatedDocumentCount()
Orionjs MongoDB collections include built-in DataLoader functionality for optimizing database access patterns.
Copy
// Load a document by IDconst user = await this.users.loadById(userId)
For more detailed information about using DataLoader functionality, including solving the N+1 query problem and performance considerations, see the dedicated DataLoader documentation.