MongoDB:文档何时更新

时间:2018-07-26 12:15:08

标签: mongodb

我知道我可以从objectid中提取文档的创建日期。

请参阅:When was a document added to a MongoDB collection

我想知道是否有类似的方式来知道文档何时更新?

好的,收到评论后,我用这个问题来创建我的changestream: How to set MongoDB Change Stream 'OperationType' in the C# driver?

我的代码如下:

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();

但这现在导致以下异常:

  

命令聚合失败:异常:无法识别的管道阶段名称:   “ $ changeStream”。

我需要做什么来解决此异常?

0 个答案:

没有答案