javascript - 如何为 node.js 中的 MongoDB 索引指定 javascript 对象中的属性顺序?

标签 javascript mongodb object indexing

MongoDB documentation状态:

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/

相关文章:

javascript - js中如何获取事件对象的值

javascript - 接收鼠标拖动的坐标

mongodb - 带有Grails,GORM和MongoDB的UUID的问题

javascript - Promises 传播中间对象(NodeJS + MongoDB)

javascript - 对象属性未定义 (javascript)

javascript - 我怎样才能拥有多个 javascript 函数而不需要每个函数都有 document.ready()

javascript - 如何基于 java 包等键将数组转换为 JSON 以查看 TreeView

javascript - 如何动态构建Mongodb聚合语句?

javascript - jQuery 获取 "event invoking"元素

php - 返回值与传递对象引用