0

I have a list of documents with the following structure:

{
   _id : ...,
   TotalSeconds: 4021,
   InitialTimestamp: 1314547200000
}

Now I want to update all documents TotalSeconds using InitialTimestamp. The logic is: TotalSeconds = timenow() - InitialTimestamp.

What is the best way to do this in mongodb? I'm not sure that this db allows to make such advanced operations using update command. Maybe aggregation framework will help me?

Neil Lunn
  • 148,042
  • 36
  • 346
  • 317
Max Frai
  • 61,946
  • 78
  • 197
  • 306
  • The point here is really "are you expecting and atomic update" therefore once your now the "_id" value can you just update "TotalSeconds" without reading the current document values. The answer ( repeated many times is ) "no you cannot". There is no way for MongoDB to currently "reference" the existing value of a document field in any query or update function. Exceptions are alterations such as `$inc`. – Neil Lunn Nov 10 '14 at 11:16
  • @NeilLunn I don't care about this. In SQL I can get the result using the simple query: `update * from ... set TotalSeconds = TIMENOW() - InitialTimestamp`. – Max Frai Nov 10 '14 at 11:18
  • 1
    Please just calm down and take a read. You are not the first person to ask the question by a long way. That's why it's closed. – Neil Lunn Nov 10 '14 at 11:20

0 Answers0