How to Compare MongoDB Collections如何比较MongoDB系列

Posted on: 20/06/2018 (last updated: 04/08/2021) by Graham Thomson

In this post, we’ll show you how to:在本文中,我们将向您展示如何:

All using Studio 3T’s Data Compare and Sync feature.所有这些都使用Studio 3T的数据比较和同步功能。

Don’t have Studio 3T on your machine yet? 你的机器上还没有Studio 3T吗?Head to our Downloads page for the latest version.请访问下载页面,获取最新版本。

Why you should diff your MongoDB你为什么要区分你的MongoDB

There are a number of situations where being able to conveniently analyze the differences between two database installations is extremely handy.在很多情况下,能够方便地分析两个数据库安装之间的差异非常方便。

For example, you have a problem with your production system. 例如,您的生产系统有问题。All you know about it is that the problem lies somewhere in the database, but WHERE in the database? 你所知道的只是问题在数据库中的某个地方,但在数据库中的什么地方呢?It worked fine yesterday, so if you compare the current database with yesterday’s backup, most likely you’ll find out what the problem is.它昨天运行得很好,因此如果将当前数据库与昨天的备份进行比较,很可能会发现问题所在。

Additionally, it could be time to release an upgrade to the production system, but there are new configuration parameters required. 此外,可能是时候对生产系统进行升级了,但需要新的配置参数。The upgrade can be performed with a calm confidence when you can easily compare against the staging server and clearly see which parameters need to be introduced, changed or removed.当您可以轻松地与登台服务器进行比较,并清楚地看到需要引入、更改或删除哪些参数时,可以从容自信地执行升级。

Or worse: an accidental update has removed vital configuration from the system, and it’s now crashing. 更糟的是:一次意外的更新已经从系统中删除了重要的配置,现在它正在崩溃。You knew you should have taken a backup, but there just wasn’t time. 你知道你应该找个替补,但就是没时间。And besides it was just a trivial change, so what could possibly go wrong?除此之外,这只是一个微不足道的变化,那么可能会出什么问题呢?

But now, all you can do is sit and stare at your screen in horror and gulp down the bitter taste of regret. 但现在,你所能做的就是坐下来,惊恐地盯着屏幕,咽下悔恨的苦涩。Unless of course, you can compare the differences and quickly identify which settings have been lost, and carry out an informed repair before anyone realizes your terrible blunder! 当然,除非你能比较这些差异,迅速识别哪些设置丢失了,并在任何人意识到你的可怕错误之前进行知情修复!(Of course, this NEVER HAPPENS on production systems, right? 🙂 )(当然,这在生产系统上从来不会发生,对吗?)

More generally, being able to see how your data is changing is very powerful. 更一般地说,能够看到数据的变化是非常强大的。Not only does it let you gain insight to your data, but it also provides confident control over the changes when you can see them explicitly and edit them directly.它不仅可以让您深入了解数据,还可以在您可以明确查看和直接编辑数据时,对更改进行可靠的控制。

Studio 3T’s Data Compare and SyncStudio 3T的数据比较和同步

First of all, download our MongoDB GUI in case you haven’t already. 首先,下载MongoDB GUI,以防您还没有下载。It’s available for Windows, Mac, and Linux.它适用于Windows、Mac和Linux。

Next, open Data Compare and Sync either by right-clicking on a collection or clicking on the Compare button.接下来,通过右键单击集合或单击比较按钮打开数据比较和同步

This will open up a view where we can select source and target connections, which looks like this:这将打开一个视图,我们可以在其中选择源和目标连接,如下所示:

Once you’ve chosen your source and target connections, it’s time to select the specific collections we want to compare.一旦选择了源和目标连接,就可以选择我们想要比较的特定集合了。

Choose collections to compare选择要比较的集合

Simply drag the source database on top of the target database, and voilà! 只需将源数据库拖到目标数据库上,瞧!All collections with the same name in the source and target databases are automatically selected for comparison (highlighted in yellow in the screenshot below).将自动选择源数据库和目标数据库中具有相同名称的所有集合进行比较(在下面的屏幕截图中以黄色突出显示)。

Of course, it’s not mandatory that collections need to have the same name. 当然,集合不一定要有相同的名称。For example, if you need to compare the source collection current_customers with the target collection existing_customers, simply drag current_customers on top of existing_customers and it will be added to the comparison (highlighted in purple).例如,如果需要将源集合当current_customers与目标集合existing_customers进行比较,只需将current_customers拖动到existing_customers之上,即可将其添加到比较中(以紫色突出显示)。

If you want to remove a particular pair of collections from the selection, simply right click it and choose Remove from comparison list. 如果要从选择中删除特定的一对集合,只需右键单击它,然后选择“从比较列表中删除”。Alternatively, click the Remove button on the right of the comparison table.或者,单击比较表右侧的“删除”按钮。

Configure your data comparison配置数据比较

Studio 3T’s Data Compare and Sync feature also lets you configure data comparison settings, which can come in handy in case you only want to compare certain fields, filter based on certain criteria, etc.Studio 3T的数据比较和同步功能还允许您配置数据比较设置,如果您只想比较特定字段、根据特定标准进行筛选等,则可以使用该功能。

To start, click on Configure to the right of the table. 要开始,请单击表格右侧的“配置”。This will open up the Comparison Configuration window which has four tabs: Match On, Compare, Filter, and Projection. 这将打开比较配置窗口,该窗口有四个选项卡:“匹配”、“比较”、“筛选”和“投影”。You can read about them in full here.你可以在这里完整地阅读他们。

Run the analysis进行分析

OK, now that we’ve selected what we want to compare, hit the Run comparison button right above the table.好的,现在我们已经选择了要比较的内容,点击表格上方的“运行比较”按钮。

When the run is completed, a tab will open for each pair of collections. 运行完成后,将为每对集合打开一个选项卡。Let’s take a closer look at accounts.让我们仔细看看“账目”。

In the image above, we can see the comparison took 792 milliseconds and found seven differences in total: five differing documents, one document that only appeared on the source side, and one document that only appeared on the target side.在上图中,我们可以看到比较耗时792毫秒,共发现了七个差异:五个不同的文档,一个仅出现在源端的文档,以及一个仅出现在目标端的文档。

The Overview tab shows exactly what was compared. “概述”选项卡显示了所比较的内容。This is useful if we save the results to come back to them later, by which time the comparison configuration may have been updated. 如果我们保存结果以便稍后返回,这将非常有用,届时比较配置可能已经更新。The overview page of the results provides a permanent record of what was actually compared.结果的概览页面提供了实际比较结果的永久记录。

Spot the difference(s)找出差异

OK, now it’s time for the main course! 好了,现在该上主菜了!Click on the Differences tab.单击“差异”选项卡。

Differing documents are highlighted in yellow, source-only documents in green and target-only in red, though in nice pastel shades that are easy on the eye.不同的文档以黄色突出显示,仅源文档以绿色突出显示,目标文档仅以红色突出显示,但颜色柔和,易于观察。

You can even just show the differing documents only for a cleaner, more targeted overview by clicking on View Options:您甚至可以通过单击“查看选项”来显示不同的文档,以获得更清晰、更有针对性的概述:

Now let’s expand a document.现在让我们展开一个文档。

Here we can see the same highlighting pattern: yellow shows fields that are different, green shows source-only fields, and red shows target-only fields.在这里,我们可以看到相同的高亮模式:黄色显示不同的字段,绿色显示源字段,红色显示目标字段。

You can expand or collapse all documents in one right-click.只需单击鼠标右键,即可展开或折叠所有文档。

Export comparison results出口比较结果

For a more convenient data analysis and easier sharing across teams, simply click on the Export button under the Differences tab to export results as a CSV file.为了更方便地进行数据分析,更容易在团队之间共享,只需单击“差异”选项卡下的“导出”按钮,即可将结果导出为CSV文件。

Sync MongoDB documents同步MongoDB文档

We realize our source-only document should indeed be on the target collection, so we right click Copy Document to Target and confirm the copy operation.我们意识到源文档确实应该在目标集合上,所以我们右键单击“将文档复制到目标”并确认复制操作。

Alright. Now we realize the target-only document is old and should no longer be present on the target.好吧现在,我们意识到仅针对目标的文档已经过时,不应该再出现在目标上。

No problem: We simply right-click and select Delete Document.没问题:我们只需右键单击并选择“删除文档”。

Sync MongoDB fields同步MongoDB字段

OK, we’ve done some syncing at document level, now we sync individual fields. 好的,我们已经在文档级别进行了一些同步,现在我们同步各个字段。View Options > Show Differing Fields Only will really come in handy here.查看“选项”>“仅显示不同的字段”在这里非常有用。

Looking at the first document, it looks like ‘pet:manatee’ on the target side is incorrect, but instead should be ‘pet:cat’ as it is on the source side.查看第一个文档,目标端的“宠物:海牛”看起来不正确,但应该是“宠物:猫”,因为它在源端。

Right click on either ‘pet:manatee’ or ‘pet:cat’. 右键单击“宠物:海牛”或“宠物:猫”。Since the field exists on both source and target, you can choose to either ‘Copy Field from Source’ or ‘Copy Field to Target’, respectively.由于源和目标上都存在字段,因此可以分别选择“从源复制字段”或“将字段复制到目标”。

You might also come across missing fields in the source document that should be copied over from the target document.您可能还会遇到源文档中缺少的字段,这些字段应该从目标文档复制过来。

In this case, you would simply right click and select ‘Copy Field to Source’ if you clicked on the target side, or ‘Copy Field from Target’ if you clicked on the source side.在这种情况下,如果在目标端单击,只需右键单击并选择“将字段复制到源”,如果在源端单击,则选择“从目标复制字段”。

Studio 3T’s Data Compare and Sync lets you do a bi-directional sync on the same document, in that some changes are copied from the target document to the source, and other changes from the source to the target. Studio 3T的数据比较和同步功能允许您对同一文档进行双向同步,其中一些更改从目标文档复制到源文档,而其他更改则从源文档复制到目标文档。Pretty neat, eh?很整洁,是吗? 🙂

Sync MongoDB data via in-place editing通过就地编辑同步MongoDB数据

Alright, we’re done with the veg and potatoes, let’s center in on the steak!好了,蔬菜和土豆都吃完了,我们开始吃牛排吧!

For small changes, you don’t even need to invoke the right-click menu. 对于小的更改,您甚至不需要调用右键单击菜单。Simply double-click a field and edit it directly – the diff status is updated in real time.只需双击一个字段并直接编辑它,就会实时更新差异状态。

It’s been a pleasure很荣幸

And that concludes our journey through how to compare MongoDB collections! 这就结束了我们如何比较MongoDB系列的旅程!I hope this tutorial on how to diff MongoDB has left you a little bit more powerful than when our journey first began.我希望这篇关于如何区分MongoDB的教程能让您比我们刚开始的旅程更强大一点。

Complement your new data comparison skills by checking out Schema Explorer, another nifty Studio 3T feature that will help tremendously in your data analysis. 通过查看Schema Explorer来补充新的数据比较技能,架构资源管理器是Studio 3T的另一个漂亮功能,它将极大地帮助您进行数据分析。And if you haven’t downloaded our MongoDB GUI yet, here’s another chance如果您还没有下载MongoDB GUI,那么还有一个机会 🙂