简体   繁体   English

在MongoDB中获取“更新前”文档

[英]Getting 'Before Update' document in MongoDB

Is there a way to get the 'before udpate' document in MongoDB via shell or through any frameworks like Spring Data [ Java ] ? 有没有一种方法可以通过外壳程序或通过Spring Data [Java]之类的框架在MongoDB中获取“ udpate之前”的文档?

In some way can I set up a trigger that outputs two things when I hit update commands in MongoDB shell. 我可以通过某种方式设置一个触发器,当我在MongoDB shell中命中更新命令时输出两件事。

  • print original document first 首先打印原始文件
  • print updated document 打印更新的文档

So far I have been exploring oplog contents but I am unable to do such a [triggerish] event as oplog's update entry has only those fields that are going to added/updated/removed to query criteria provided. 到目前为止,我一直在探索oplog的内容,但是我无法做这样的[触发]事件,因为oplog的update条目只有要添加/更新/删除来提供查询条件的那些字段。

The findAndModify command takes an option ( new : true/false ) to specify whether you return the new version (true) or the original version of the document (false, this is the default). findAndModify命令采用一个选项( new : true/false )指定返回的是新版本(true)还是文档的原始版本(false,这是默认值)。

Since you can have either version returned and you know what you are doing to modify the document, it should be possible to return the original (print it out), then apply the update to the document in your code and print it out again. 由于可以返回任一版本,并且知道修改文档的方式,因此应该可以返回原始文档(将其打印出来),然后将更新应用到代码中的文档中,然后再次打印出来。

The alternative would be to use the original returned by findAndModify , then re-query for the same document, but you would have no way to guarantee that the document had not been changed by another operation in the meantime. 替代方法是使用findAndModify返回的原始findAndModify ,然后重新查询同一文档,但是您将无法保证同时没有其他操作更改过该文档。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM