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

查看:33
本文介绍了更新后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"}

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

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天全站免登陆