MongoDb背景索引和唯一索引 [英] MongoDb background indexing and unique index

查看:591
本文介绍了MongoDb背景索引和唯一索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在MongoDb中创建索引时。有两个选项:

When you create an index in MongoDb. There are 2 options:


  • 执行前台索引并锁定所有写入操作

  • 做背景索引仍然允许记录同时写入

我的问题是:

如何在后台构建类似唯一索引的内容?如果在进行索引构建时插入重复文档会怎样?

How can something like unique index be built in the background? What if a duplicated document is inserted while index building is happening?

推荐答案

参考 MongoDB文档 -


如果在mongod进程终止时正在进行后台索引构建,则当实例重新启动时,索引构建将作为前台索引构建重新启动。如果索引构建遇到任何错误,例如重复键错误,mongod将退出并显示错误。

If a background index build is in progress when the mongod process terminates, when the instance restarts the index build will restart as foreground index build. If the index build encounters any errors, such as a duplicate key error, the mongod will exit with an error.

所以有两个可能性 -

So there are two possibilities-


  1. 如果索引创建已完成,那么您尝试插入的文档将立即给您带来错误。

  2. 或者如果索引创建正在后台进行,那么您将能够插入文档(因为在插入时索引不是100%)。但是后来当索引创建过程试图将索引放在您的重复文档上时,它将退出并出错。这与您有重复文档并尝试创建前景索引的行为相同。

这篇关于MongoDb背景索引和唯一索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆