MongoDB及其驱动程序可以保留文档元素的顺序 [英] Can MongoDB and its drivers preserve the ordering of document elements

查看:139
本文介绍了MongoDB及其驱动程序可以保留文档元素的顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在考虑使用MongoDB来存储包含键/值对列表的文档。存储它的安全但丑陋和臃肿的方式是

I am considering using MongoDB to store documents that include a list of key/value pairs. The safe but ugly and bloated way to store this is as

[ ['k1' : 'v1'] , ['k2' : 'v2'],  ...]

但是文档元素本质上是在底层BSON中排序的数据结构,原则上:

But document elements are inherently ordered within the underlying BSON data structure, so in principle:

{k1 : 'v1', 
 k2 : 'v2',  ...}

应该足够了。但是我希望大多数语言绑定会将这些解释为关联数组,因此可能会对排序进行加扰。所以我需要知道的是:

should be enough. However I expect most language bindings will interpret these as associative arrays, and thus potentially scramble the ordering. So what I need to know is:


  • MongoDB本身承诺是否保留第二种形式的项目排序。

  • 语言绑定是否有一些API可以提取它的有序形式 - 即使通常的方便API返回一个关联数组。

  • Does MongoDB itself promise to preserve item ordering of the second form.
  • Do language bindings have some API which can extract it ordered form -- even if the usual "convenient" API returns an associative array.

我最感兴趣的是Javascript和PHP,但我也想了解其他语言。感谢任何帮助,或者只是链接到我可以进行RTM的一些文档。

I am mostly interested in Javascript and PHP here, but I would also like to know about other languages. Any help is appreciated, or just a link to some documentation where I can go RTM.

推荐答案

从版本2.6开始, MongoDB尽可能保留字段的顺序。但是, _id 字段始终是第一个重命名字段可以导致重新排序。但是,我一般不会依赖这样的细节。正如最初的问题所提到的那样,还有其他要考虑的层次,每个层面必须为订单的稳定性提供某种保证......

From Version 2.6 on, MongoDB preserves the order of fields where possible. However, the _id field always comes first an renaming fields can lead to re-ordering. However, I'd generally try not to rely on details like this. As the original question mentions, there are also additional layers to consider which each must provide some sort of guarantee for the stability of the order...

原始答案:

不,MongoDB 不保证字段的排序

No, MongoDB does not make guarantees about the ordering of fields:


无法保证更新后字段顺序将保持一致或相同。

"There is no guarantee that the field order will be consistent, or the same, after an update."

特别是,就地更新会更改文档大小通常会更改字段的顺序。例如,如果您 $ set 一个旧值为number且新值为 NumberLong 的字段,则为field通常会重新订购。

In particular, in-place updates that change the document size will usually change the ordering of fields. For example, if you $set a field whose old value was of type number and the new value is NumberLong, fields usually get re-ordered.

然而,数组保持正确的订购:

However, arrays preserve ordering correctly:

[ {'key1' : 'value1'}, {'key2' : 'value2'}, ... ]

我不明白为什么这个丑陋和臃肿。存储复杂对象列表并不容易。但是,滥用对象作为列表肯定是丑陋的:对象具有关联数组语义(即,只能有一个给定名称的字段),而列表/数组则不:

I don't see why this is "ugly" and "bloated" at all. Storing a list of complex objects couldn't be easier. However, abusing objects as lists is definitely ugly: Objects have associative array semantics (i.e. there can only be one field of a given name), while lists/arrays don't:

// not ok:
db.foo2.insert({"foo" : "bar", "foo" : "lala" });
db.foo2.find();
{ "_id" : ObjectId("4ef09cd9b37bc3cdb0e7fb26"), "foo" : "lala" }

// a list can do that
db.foo2.insert({ 'array' : [ {'foo' : 'bar'}, { 'foo' : 'lala' } ]});
db.foo2.find();
{ "_id" : ObjectId("4ef09e01b37bc3cdb0e7fb27"), "array" : 
      [ { "foo" : "bar" }, { "foo" : "lala" } ] }

请记住,MongoDB是一个对象数据库,而不是键/值存储。

Keep in mind that MongoDB is an object database, not a key/value store.

这篇关于MongoDB及其驱动程序可以保留文档元素的顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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