0

I know I can extract the creationdate from a document from the objectid.

see : When was a document added to a MongoDB collection

I was wondering if there is a similar way to know when a document was updated?

OK, after the comments I received I used this question to create my changestream : How to set MongoDB Change Stream 'OperationType' in the C# driver?

My code looks as follows:

IMongoCollection<RootRecord> collection = db.GetCollection<RootRecord>("companies");            
//Get the whole document instead of just the changed portion
ChangeStreamOptions options = new ChangeStreamOptions() { FullDocument = ChangeStreamFullDocumentOption.UpdateLookup };

//The operationType can be one of the following: insert, update, replace, delete, invalidate
var pipeline = new EmptyPipelineDefinition<ChangeStreamDocument<RootRecord>>().Match("{ operationType: { $in: [ 'replace', 'insert', 'update' ] } }");

var changeStream = collection.Watch(pipeline, options).ToEnumerable().GetEnumerator();
changeStream.MoveNext();    //Blocks until a document is replaced, inserted or updated in the TestCollection
ChangeStreamDocument<RootRecord> next = changeStream.Current;
changeStream.Dispose();

But this now results in the following exception:

Command aggregate failed: exception: Unrecognized pipeline stage name: '$changeStream'.

What do I need to do to solve this exception?

Bart Schelkens
  • 1,235
  • 4
  • 21
  • 45
  • No, there is not. If you have relicaset, you can get it from oplog, but it is a limited time offer. – Alex Blex Jul 26 '18 at 12:17
  • @AlexBlex : so if I use a service that copies the data from the mongodb to a sqlserver, i would need to delete all the records in the sqlserver and then insert them again? Since there is no way to check if the documents have been updated? – Bart Schelkens Jul 26 '18 at 12:28
  • You would need to keep track of this information yourself, since Mongo doesn't implicitly do so. – deceze Jul 26 '18 at 12:30
  • That's a different matter. There are [change streams](https://docs.mongodb.com/manual/changeStreams/). – Alex Blex Jul 26 '18 at 12:39
  • Ancient version of mongodb? Change streams were added added in v3.6. – Alex Blex Jul 26 '18 at 17:24
  • @AlexBlex : I have used nuget to install the drivers. i get version v2.7.0. But my MongoDB itself is version 2.6. So I'll have to upgrade that one. – Bart Schelkens Jul 27 '18 at 06:21
  • 2.6 is very old. It was decommissioned in October 2016. Upgrade to 3.6 won't be easy if you want to keep the data. – Alex Blex Jul 27 '18 at 08:57

0 Answers0