mongodb如何使用javascript无序对象文字创建来保证复合索引顺序?

时间:2017-02-17 08:30:24

标签: mongodb

例如db.products.createIndex({“item”:1,“stock”:1})
引自https://docs.mongodb.com/manual/tutorial/sort-results-with-indexes/ You can specify a sort on all the keys of the index or on a subset; however, the sort keys must be listed in the same order as they appear in the index. 由于javascript对象中没有订单,mongodb如何从中获取订单?

1 个答案:

答案 0 :(得分:1)

实际上,在mongo shell(SpiderMonkey)使用的Javascript引擎中,对象中字段的顺序被保留。当你在mongo shell中调用“createIndex”时,shell会以正确的顺序构造一个包含字段的BSON对象,并将createIndexes命令发送到MongoDB。