Syntax
Update Syntax

Update Syntax

CapybaraDB supports a robust and flexible update syntax fully compatible with MongoDB, allowing you to modify, add, or remove fields and elements in your documents.


1: Field Update Operators

Modify or remove specific fields in a document.

OperatorDescriptionExample
$setUpdates the value of a field.{ "$set": { "name": "Alice" } }
$unsetRemoves a field from the document.{ "$unset": { "age": "" } }
$renameRenames a field.{ "$rename": { "oldName": "newName" } }
$incIncrements the value of a field.{ "$inc": { "score": 5 } }
$mulMultiplies the value of a field.{ "$mul": { "price": 1.2 } }
$minUpdates the field if the new value is smaller.{ "$min": { "age": 18 } }
$maxUpdates the field if the new value is greater.{ "$max": { "age": 30 } }
$setOnInsertSets a field only if the document is inserted (used with upsert).{ "$setOnInsert": { "joined": true } }

2: Array Update Operators

Modify fields that are arrays, allowing for dynamic manipulation of their contents.

OperatorDescriptionExample
$pushAdds an item to an array.{ "$push": { "tags": "mongodb" } }
$pullRemoves items matching a condition from an array.{ "$pull": { "tags": "deprecated" } }
$addToSetAdds an item to an array only if it does not exist.{ "$addToSet": { "tags": "uniqueTag" } }
$popRemoves the first or last item from an array.{ "$pop": { "comments": -1 } }
$pullAllRemoves all matching values from an array.{ "$pullAll": { "tags": ["oldTag", "unusedTag"] } }
$eachAdds multiple values to an array.{ "$push": { "tags": { "$each": ["tag1", "tag2"] } } }
$sliceLimits the size of an array.{ "$push": { "tags": { "$each": ["newTag"], "$slice": 5 } } }
$sortSorts array elements.{ "$push": { "scores": { "$each": [], "$sort": -1 } } }

3: Bitwise Update Operators

Perform bitwise operations on numeric fields.

OperatorDescriptionExample
$bitPerforms bitwise operations.{ "$bit": { "flags": { "and": 5 } } }

4: Positional Array Operators

Update specific elements in arrays using positional or filtered criteria.

OperatorDescriptionExample
$Updates the first matching array element.{ "$set": { "items.$": "updatedValue" } }
$[]Updates all elements in an array.{ "$set": { "scores.$[]": 100 } }
$[<identifier>]Updates array elements that match a filter.{ "$set": { "scores.$[passing]": 100 } }

Array Filter Example: Use with $[<identifier>] to target specific elements:

{
  "arrayFilters": [{ "passing": { "$gte": 50 } }]
}

5: Document Replacement

Replace an entire document with a new one, retaining the _id field.

{
  "name": "Alice",
  "age": 30,
  "status": "active"
}

6: Combining Multiple Updates

Use multiple update operators in a single operation for complex modifications.

{
  "$set": { "status": "active" },
  "$inc": { "score": 10 },
  "$unset": { "oldField": "" }
}

Usage Notes

  • Atomic Operations: Updates are applied atomically to a document.
  • Upsert: Use the upsert option to insert a document if no matching document is found.
  • Array Filters: Use arrayFilters to target specific elements in arrays when using $[] or $[<identifier>].
  • Document Replacement: Fields not included in the replacement document will be removed (except _id).

How can we improve this documentation?

Got question? Email us