简体   繁体   English

MongoDB $ sort聚合的Javascript属性顺序

[英]Javascript property order for MongoDB $sort aggregation

ECMAScript defines JavaScript objects as an unordered collection of properties . ECMAScript将JavaScript对象定义为属性的无序集合

However, the MongoDB $sort operator in the aggregation pipeline specifies documents will be sorted in the order the properties are listed on the $sort property . 但是,聚合管道中的MongoDB $sort运算符指定将按照$sort属性上列出的属性的顺序对文档进行排序

db.users.aggregate(
   [{ $sort : { age : -1, posts: 1 } }]
);

This operation sorts the documents in the users collection, in descending order according by the age field and then in ascending order according to the value in the posts field. 此操作对用户集合中的文档进行排序,根据年龄字段以降序排列,然后根据职位字段中的值以升序排列。

Does MongoDB have a different definition for javascript objects since apparently MongoDB depends on property order? 由于MongoDB显然取决于属性顺序,因此MongoDB是否对javascript对象有不同的定义?

Does MongoDB have a different definition for javascript objects since apparently MongoDB depends on property order? 由于MongoDB显然取决于属性顺序,因此MongoDB是否对javascript对象有不同的定义?

MongoDB's server-side document representation is a JSON-like binary format called BSON , which has richer types than JavaScript/JSON and preserves field order. MongoDB的服务器端文档表示形式是类似于JSON的二进制格式,称为BSON ,其类型比JavaScript / JSON丰富,并且保留字段顺序。 MongoDB sorts server-side queries (eg. find().sort() or aggregation's $sort stage) using BSON Comparison/Sort Order . MongoDB使用BSON比较/排序顺序 $sort服务器端查询(例如find().sort()或聚合的$sort阶段)进行排序

When you interact with MongoDB through the mongo shell or a driver, the communication to the MongoDB server is using the MongoDB Wire Protocol with messages encapsulated in BSON. 通过mongo shell或驱动程序与MongoDB进行交互时,与MongoDB服务器的通信使用的是MongoDB Wire协议 ,消息封装在BSON中。 It is the driver and application author's responsibility to ensure object representations use an order-preserving data structure where required. 驱动程序和应用程序作者有责任确保在必要时对象表示使用保留顺序的数据结构。 This is typically an ordered hash/map/dictionary depending on the language implementation and common terminology. 根据语言的实现方式和通用术语,这通常是一个有序的哈希/地图/词典。

You should check your driver documentation for any specific suggestions. 您应该检查驱动程序文档以获取任何特定建议。 For example, PyMongo (the official Python driver) provides a SON subclass of the standard Python dict that maintains ordering of keys and provides some extra helpers for mapping Python types to BSON. 例如,PyMongo(官方Python驱动程序)提供了标准Python dictSON子类,该子类维护键的顺序,并提供了一些额外的帮助程序来将Python类型映射到BSON。

Modern JavaScript engines do tend to maintain an implicit ordering (insertion order of fields in objects) but there are objects such as Map that can be used when an explicit guarantee is required. 现代JavaScript引擎的确倾向于保持隐式顺序(对象中字段的插入顺序),但是当需要显式保证时,可以使用诸如Map之类的对象。 In the specific example of the mongo shell, the behaviour of the integrated JavaScript engine is expected to be order-preserving. mongo shell的特定示例中,预计集成JavaScript引擎的行为将保持顺序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM