For indexes with more than one key (i.e. compound indexes) the sequence of fields is important.
但是 ECMAScript 定义一个对象如下:
An object is a member of the type Object. It is an unordered collection of properties each of which contains a primitive value, object, or function.
在 node.js
中使用 MongoDB
时(例如使用 this module ),您使用的是服务器端 javascript,如下例所示。
当 MongoDB 需要一个 object
(又名无序的属性集合)时,如何指定序列?
collection.ensureIndex({
date : -1,
client : 1,
product : 1
});
最佳答案
在 MongoDB 中,文档中字段的顺序确实很重要,所有语言驱动程序都提供了一种以这种方式指定文档的方法,即使底层编程语言没有这样的概念。
MongoDB 在其 shell 中使用的文档格式类似于 JSON,但不是严格的 JSON。除其他外,始终保留字段顺序。
在 Javascript 中,标准将字段定义为无序的,因此实现可以随意忽略/不保留顺序。但在实践中,所有实现都保留了顺序。特别是 V8 引擎
保留了顺序,这是 node.js
中使用的引擎,所以没问题。
关于javascript - 如何为 node.js 中的 MongoDB 索引指定 javascript 对象中的属性顺序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18514188/