0

In my Mongo db collection have 2 date fields TimeStamp, OpEndTime, I need to take order by OpEndTime-TimeStamp. It is working with a small amount of data. For huge data throw exception out of ram space. So need to create the index with difference of OpEndTime and TimeStamp. I don't know how to create it. I am using C# mongo db driver

I tried like below DBContext.ClientDb.Repository(collection).Indexes.CreateOne(Builders.IndexKeys.Ascending(i =>i.OpEndTime- i.TimeStamp));

but am getting error like below

An exception of type 'System.InvalidOperationException' occurred in MongoDB.Driver.dll but was not handled in user code Additional information: Unable to determine the serialization information for i => Convert((i.OpEndTime - i.TimeStamp)).

  • You're trying to create an index of the result of a calculation. Does Mongo support that? – ProgrammingLlama Mar 12 '20 at 07:07
  • I would suggest adding the result of the operation as a separate column – Ian Newson Mar 12 '20 at 07:08
  • I don't know exactly, Mogodb will support or not. Currently trying to avoid one extra column – user2972244 Mar 12 '20 at 07:18
  • Any particular reason you want to avoid the extra column? Duplication of data is common in NoSQL applications generally. – Ian Newson Mar 12 '20 at 07:31
  • Updating the collection will take more time, Thats why – user2972244 Mar 12 '20 at 14:13
  • _"Updating the collection will take more time"_ - Huh? You already have to set `i.OpEndTime` and `i.TimeStamp` at some point. How will also updating a calculated field take significantly more time? Considering that if you had calculated indexes, you would still need time to update the index. – ProgrammingLlama Mar 13 '20 at 00:33

0 Answers0