1

虽然我理解外键约束对于 NoSql 数据库没有意义,但如果它允许我重命名字段,它是否不应该确保它更新索引? http://www.mongodb.org/display/DOCS/Updating#Updating-%24rename

{ $rename : { old_field_name : new_field_name } }

但如果我有

db.mycollections.ensureIndex({old_field_name:1});

如果索引自动更新不是很好吗?

是不是因为 system.indexes 只是另一个表,并且这样的自动更新意味着某种外键约束,所以索引更新没有完成?还是我错过了某些标志?

4

2 回答 2

2

它不这样做。

您的问题的答案“如果索引自动更新不是很好吗?” 是,“不,不是真的”

如果您认为重命名字段是一个好主意,您可以同时自己添加新索引。您可能需要在代码中进行许多其他更改以反映字段的重命名(查询、更新、map reduce 操作……),那么您为什么认为它应该将索引重新创建作为应该发生的事情?自动执行一项非常罕见的操作,而这只是您需要手动执行的众多操作中的一件事?

如果您关心此功能,请去请求它,10Gen 对建议的反应非常好,但如果答案是“为什么这很重要?” ,我不会感到惊讶。

于 2012-04-20T03:18:51.567 回答
1

引用 Mike O'Brien 的话:

$rename 运算符类似于在单个原子操作中执行 $set/$unset。对于需要取值并将其移动到另一个字段的情况,这是一种快捷方式,无需分两步进行(一个获取字段的值,另一个设置新的值)。

Doing a $rename does mean the data is changing. If I use $rename to rename a field named "x" to "y", but the field named "y" already existed in the document, the old value for "y" is overwritten, and the field "x" will no longer exist anymore. If either "x" or "y" is indexed, then the operation will update those indexes to reflect the final values resulting from the operation. The same applies when using rename to move a field from within an embedded document up to the top level (e.g. renaming "a.b" to "c") or vice versa.

The behavior suggested in the SO question (i.e., renaming a field maintains the relationship between the field it was moved to and its value in the index) then things can get really confusing and make it difficult to reason about what the "correct" expected behavior is for certain operations. For example, if I create an index on field "A" in a collection, rename "A" to "B" in one of the documents, and then do things like: update({"A" : }, {"$set":{"B":}}) // find a document where A= and set its value of B to update({"B" : }, {"$set":{"A":}}) // find a document where B= and set its value of A to

Should these be equivalent? In general, having the database maintain indexes across a collection by field name is a design decision that keeps behavior predictable and simple.

于 2012-04-27T05:24:09.600 回答