[SERVER-1765] self referential updates? WAS: allow access to old row value during update Created: 09/Sep/10  Updated: 06/Dec/22  Resolved: 29/Jun/19

Status: Closed
Project: Core Server
Component/s: Write Ops
Affects Version/s: None
Fix Version/s: None

Type: New Feature Priority: Major - P3
Reporter: David Judd Assignee: Backlog - Query Team (Inactive)
Resolution: Won't Do Votes: 37
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Duplicate
is duplicated by SERVER-20510 Add stored procedure functionality Closed
Related
is related to SERVER-458 JavaScript $function in update Closed
is related to SERVER-11345 Allow update to compute expressions u... Closed
Assigned Teams:
Query
Participants:

 Description   

Right now, you can't access the old value of a row during an update. You have to decide what each field you want to update should look like on the client side. So you can't do:
{ $set : { "creation_timestamp" : coalesce(old.creation_timestamp, <now>) }}
or:
{ $set : { "priority" : max(old.priority, 3) }}
or:
{ $set : { "widgets_being_processed" : old.widgets_needing_processing }}

... or any equivalent.

You can do this kind of thing with two queries, but that has potential performance and concurrency problems.

From what I know of the Mongo architecture I don't see why access to this information should be impossible. I don't know what the syntax would be, of course.

See also my original email question: http://groups.google.com/group/mongodb-user/browse_thread/thread/1c7b424cf9ef48e8/877621f662ca4cc0



 Comments   
Comment by Asya Kamsky [ 29/Jun/19 ]

SERVER-40381 implemented support for aggregation expressions to specify update for 4.2. 

You can see some examples here.

This gives complete access to all existing fields in the document to make updates described in this ticket.

Comment by zhouao [ 15/Sep/18 ]

这个问题提出快十年了,都没得到解决,mongodb开发组效率太低了。

Comment by Madhava Vinnakota [ 13/Apr/16 ]

This will be great feature to have! I hope it will be taken up soon!

Comment by Reynaldo Enriquez [ 12/Nov/15 ]

Any update? Thanks in Advance!!!

Comment by Jia Hongchao [ 16/Sep/15 ]

Any update?

Comment by Ricardo Gonzalez [ 13/Aug/15 ]

This issue was created on 2010 Is there any possibility that this issue could be addressed this year?
Any reference you could provide about what is the best practice for this? I'm learning mongodb and it's kind of shocking not to have this functionality. Tks in advance!

Comment by David Parker [ 27/Sep/14 ]

I'm looking for this capability as well. Seems like it should be a fundamental feature with update.
Looking forward to when the solution is implemented.

Comment by Yashika Ketan Gupta [ 02/Jun/14 ]

Whats the progress? Can we use/append/prepend a string to old value of field while updating that field??

Comment by Kevin J. Rice [ 11/Apr/13 ]

We're looking for this, too. In our document:

doc =

{ ... 'keyname' : 'ABCDE' ... }

We'd like to do the following as in-place modifications to strings:

  • append a string to an existing string ($append: {'keyname' : 'FG'}

    ) resulting in 'ABCDEFG';

  • prepend a string to an existing string ($prepend: {'keyname' : 'FG'}

    ) resulting in 'FGABCDE';

  • remove characters from the front of a string ($lcut : {'keyname' : 2 }

    ) resulting in 'CDE';

  • remove characters from the end of a string ($rcut : {'keyname' : 2 }

    ) resulting in 'ABC';

  • trim whitespace from left, right or both sides;
  • change a string into a substring (start, length) of itself;
  • remove characters before or after a substring;
  • regex substitutions (Perl-like) on a string. For instance:

    { $regexSub:{'field':'keyname','searchRegex':'(.*)CD(.*)','replRegex':'\1__new__\2','flags':'gi'}} 

    resulting in 'AB_new_E';

Comment by Abdullah Battal [ 24/Mar/13 ]

Any progress on this issue?

Comment by Eliot Horowitz (Inactive) [ 09/Sep/10 ]

We should make the syntax work for queries as well

Generated at Thu Feb 08 02:57:58 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.