更新后MongoDB字段顺序和文档位置更改 [英] MongoDB field order and document position change after update

查看:1233
本文介绍了更新后MongoDB字段顺序和文档位置更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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:

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

然后,如果您这样做:

db.collection.find();

它将显示:

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

您可以看到在将更新的字段推送到文档末尾的位置,字段顺序如何更改.此外,文档本身被推到收集器的末尾.我知道这是一个无schema的"数据库,它可能不是一个大问题,但看起来并不漂亮" :).有没有一种方法可以在不更改顺序的情况下进行就地更新?

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?

推荐答案

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

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字段顺序和文档位置更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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