mongodb 更新后MongoDB字段顺序和文档位置发生变化

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5046835/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 11:58:56  来源:igfitidea点击:

MongoDB field order and document position change after update

mongodb

提问by techexpert

I am learning MongoDB and I noticed that whenever I do an update on a document the field being updated is pushed to the end of the order, so if I had something like:

我正在学习 MongoDB,我注意到每当我对文档进行更新时,正在更新的字段都会被推送到订单的末尾,所以如果我有类似的东西:

db.collection.save({field1: value1, field2: value2, ..., field 10: value10});
db.collection.update({field1: value1}, {$set: {field2: new_value}});

then if you do:

那么如果你这样做:

db.collection.find();

it will display:

它将显示:

{ "field1":"value1", ..., "field10":"value10", "field2":"new_value"}

You can see how the field order changes where the updated field is being pushed to the end of the document. In addition, the document itself is being pushed to the end of the collectoin. I know that it's a "schema-less" DB and it may not be a huge problem, but it just doesn't look "pretty" :). Is there a way to do an in-place update without changing the order?

您可以看到字段顺序如何更改,更新的字段被推送到文档末尾。此外,文档本身正在被推送到集合的末尾。我知道这是一个“无模式”的数据库,它可能不是一个大问题,但它看起来并不“漂亮”:)。有没有办法在不更改顺序的情况下进行就地更新?

回答by Bernie Hackett

MongoDB allocates space for a new document based on a certain padding factor. If your update increases the size of the document beyond the size originally allocated the document will be moved to the end of the collection. The same concept applies to fields in a document.

MongoDB 根据特定的填充因子为新文档分配空间。如果您的更新增加了文档的大小,超出了最初分配的大小,则该文档将被移动到集合的末尾。相同的概念适用于文档中的字段。

回答by helmy

FYI, in MongoDB 2.6updates will preserve field order, with the following exceptions:

仅供参考,在MongoDB 2.6 中,更新将保留字段顺序,但以下情况除外:

  1. The _id field is always the first field in the document.
  2. Updates that include renaming of field names may result in the reordering of fields in the document.
  1. _id 字段始终是文档中的第一个字段。
  2. 包括重命名字段名称的更新可能会导致文档中字段的重新排序。

回答by Raman

Both document structure and collection structure in MongoDB based on JSON principles. JSON is a setof key/value pairs (in particular fieldName/fieldValue for document and index/document for collection). From this point of view it doesn't seem that you can rely on order at all.

MongoDB 中的文档结构和集合结构都基于 JSON 原理。JSON 是一键/值对(特别是文档的 fieldName/fieldValue 和集合的索引/文档)。从这个角度来看,您似乎根本无法依赖订单。

回答by Sridhar

In the case of the documents if the field size changes, it writes out a new document with the fields sorted by field name. This behavior can be seen with the following statements

在文档的情况下,如果字段大小发生变化,它会写出一个新文档,其中包含按字段名称排序的字段。可以通过以下语句看到此行为

Case 1: No change in size of field, so no change in field order

案例1:字段大小没有变化,所以字段顺序没有变化

> db.testcol.find()
> db.testcol.save({a:1,c:3,b:2})
> db.testcol.find()
{ "_id" : ObjectId("4d5efc3bec5855af36834f5a"), "a" : 1, "c" : 3, "b" : 2 }
> db.testcol.update({a:1},{$set:{c:22}})
> db.testcol.find()
{ "_id" : ObjectId("4d5efc3bec5855af36834f5a"), "a" : 1, "c" : 22, "b" : 2 }

Case 2: Field size changes and the fields are reodered

案例 2:字段大小发生变化并且字段被重新编码

> db.testcol.find()
> db.testcol.save({a:1,c:"foo",b:2,d:4})
> db.testcol.find()
{ "_id" : ObjectId("4d5efdceec5855af36834f5e"), "a" : 1, "c" : "foo", "b" : 2, "d" : 4 }
> db.testcol.update({a:1},{$set:{c:"foobar"}})
> db.testcol.find()
{ "_id" : ObjectId("4d5efdceec5855af36834f5e"), "a" : 1, "b" : 2, "c" : "foobar", "d" : 4 }

Is there a particular reason why you do not want the fields reordered? The above was using 1.8.0_rc0 on OS X

您不希望重新排序字段是否有特殊原因?以上是在 OS X 上使用 1.8.0_rc0

回答by Tyler Brock

I've created a project that creates a custom mongorc.js which sorts the document keys by default for you. It's called Mongo Hacker

我创建了一个创建自定义 mongorc.js 的项目,该项目默认为您排序文档键。它被称为Mongo 黑客

回答by Juan Pablo Fernandez

In order to have the fields in the order I wanted, I inserted all the documents with the properties in the correct order to another Collection. Then I removed the old Collection and renamed the new Collection to the original name. Obviously backup your data first.

为了按我想要的顺序排列字段,我将所有具有正确顺序的属性的文档插入到另一个集合中。然后我删除了旧的 Collection 并将新的 Collection 重命名为原始名称。显然先备份您的数据。

回答by Sérgio

Raman , is right , we can't sort an dictionay , but we can sort the visualization of the dictonary, so we can't sort filed order of an documentation , but I can see it ordered .

拉曼,是的,我们不能对字典进行排序,但是我们可以对字典的可视化进行排序,所以我们无法对文档的归档顺序进行排序,但我可以看到它是有序的。

For example in perl to_json have option canonical

例如在 perl to_json 中有选项 canonical

print to_json( $data, { utf8 => 1, pretty => 1, convert_blessed => 1, canonical => 1 } );

(canonical option) will output JSON objects by sorting their keys. This is adding a comparatively high overhead . (of course, we do more a sorting operation ...)

(规范选项)将通过对键进行排序来输出 JSON 对象。这增加了一个比较高的开销。(当然,我们多做一个排序操作...)