-
Type: Improvement
-
Resolution: Done
-
Priority: Major - P3
-
None
-
Affects Version/s: None
-
Component/s: Write Ops
-
None
-
Query
-
(copied to CRM)
AF's $project allows us to compose arbitrary expressions using referenced fields from the document and primitive arithmetic operators.
In contrast, update() only allows us to $inc a field or $set it, but not e.g. to multiply it by a constant (I see this was added in 2.6) or by another field.
As an example, suppose I want to update a document containing the average rating of a product and the number of ratings it got.
I want to be able to add a new rating in an atomic update by writing something like:
db.products.update({ _id: 7 }, { $mul: { averageRating: '$ratingCount' }, $add: { averageRating: 5 }, $inc: { ratingCount: 1 }, $div: { averageRating: '$ratingCount' } });
Alternatively I'd like to be able to run a user-provided JS function as part of the update, like the $where operator for queries, except this function can update the document.
- is duplicated by
-
SERVER-3089 Ability to make use of a subdocument's data whose contents were used to satisfy a query using the $ operator
- Closed
-
SERVER-15918 Unable to create field with update which is calculated with size array.
- Closed
- is related to
-
SERVER-20510 Add stored procedure functionality
- Closed
-
SERVER-9366 Expose projection (aggregation) operators in regular find queries
- Closed
- related to
-
SERVER-458 JavaScript $function in update
- Closed
-
SERVER-1765 self referential updates? WAS: allow access to old row value during update
- Closed
-
SERVER-22893 Update should provide more than $inc $min $max etc.
- Closed
-
SERVER-22940 Add support of more math operations on update
- Closed