• unstable sort


    $sort (aggregation) — MongoDB Manual https://docs.mongodb.com/manual/reference/operator/aggregation/sort/

    Definition

    $sort

    Sorts all input documents and returns them to the pipeline in sorted order.

    The $sort stage has the following prototype form:

    copy
    copied
    { $sort: { <field1>: <sort order>, <field2>: <sort order> ... } }
    

    $sort takes a document that specifies the field(s) to sort by and the respective sort order. <sort order> can have one of the following values:

    ValueDescription
    1 Sort ascending.
    -1 Sort descending.
    { $meta: "textScore" } Sort by the computed textScore metadata in descending order. See Text Score Metadata Sort for an example.

    If sorting on multiple fields, sort order is evaluated from left to right. For example, in the form above, documents are first sorted by <field1>. Then documents with the same <field1> values are further sorted by <field2>.

    Behavior

    Sort Stability

    In MongoDB, sorts are inherently stable, unless sorting on a field which contains duplicate values:

    • stable sort is one that returns the same sort order each time it is performed
    • an unstable sort is one that may return a different sort order when performed multiple times

    If a stable sort is desired, include at least one field in your sort that contains exclusively unique values. The easiest way to guarantee this is to include the _id field in your sort query.

    Consider the following restaurant collection:

    copy
    copied
    db.restaurants.insertMany( [
       { "_id" : 1, "name" : "Central Park Cafe", "borough" : "Manhattan"},
       { "_id" : 2, "name" : "Rock A Feller Bar and Grill", "borough" : "Queens"},
       { "_id" : 3, "name" : "Empire State Pub", "borough" : "Brooklyn"},
       { "_id" : 4, "name" : "Stan's Pizzaria", "borough" : "Manhattan"},
       { "_id" : 5, "name" : "Jane's Deli", "borough" : "Brooklyn"},
    ] );
    

    The following command uses the $sort stage to sort on the borough field:

    copy
    copied
    db.restaurants.aggregate(
       [
         { $sort : { borough : 1 } }
       ]
    )
    

    In this example, the sort is unstable, since the borough field contains duplicate values for both Manhattan and Brooklyn. Documents are returned in alphabetical order by borough, but the order of those documents with duplicate values for borough might not the be the same across multiple executions of the same sort. For example, here are the results from two different executions of the above command:

     
    { "_id" : 3, "name" : "Empire State Pub", "borough" : "Brooklyn" }
    { "_id" : 5, "name" : "Jane's Deli", "borough" : "Brooklyn" }
    { "_id" : 1, "name" : "Central Park Cafe", "borough" : "Manhattan" }
    { "_id" : 4, "name" : "Stan's Pizzaria", "borough" : "Manhattan" }
    { "_id" : 2, "name" : "Rock A Feller Bar and Grill", "borough" : "Queens" }
    
    { "_id" : 5, "name" : "Jane's Deli", "borough" : "Brooklyn" }
    { "_id" : 3, "name" : "Empire State Pub", "borough" : "Brooklyn" }
    { "_id" : 4, "name" : "Stan's Pizzaria", "borough" : "Manhattan" }
    { "_id" : 1, "name" : "Central Park Cafe", "borough" : "Manhattan" }
    { "_id" : 2, "name" : "Rock A Feller Bar and Grill", "borough" : "Queens" }
    

    While the values for borough are still sorted in alphabetical order, the order of the documents containing duplicate values for borough (i.e. Manhattan and Brooklyn) is not the same.

    To achieve a stable sort, add a field which contains exclusively unique values to the sort. The following command uses the $sort stage to sort on both the borough field and the _id field:

    copy
    copied
    db.restaurants.aggregate(
       [
         { $sort : { borough : 1, _id: 1 } }
       ]
    )
    

    Since the _id field is always guaranteed to contain exclusively unique values, the returned sort order will always be the same across multiple executions of the same sort.

    Definition

    $sort

    Sorts all input documents and returns them to the pipeline in sorted order.

    The $sort stage has the following prototype form:

    copy
    copied
    { $sort: { <field1>: <sort order>, <field2>: <sort order> ... } }
    

    $sort takes a document that specifies the field(s) to sort by and the respective sort order. <sort order> can have one of the following values:

    ValueDescription
    1 Sort ascending.
    -1 Sort descending.
    { $meta: "textScore" } Sort by the computed textScore metadata in descending order. See Text Score Metadata Sort for an example.

    If sorting on multiple fields, sort order is evaluated from left to right. For example, in the form above, documents are first sorted by <field1>. Then documents with the same <field1> values are further sorted by <field2>.

    Behavior

    Sort Stability

    In MongoDB, sorts are inherently stable, unless sorting on a field which contains duplicate values:

    • stable sort is one that returns the same sort order each time it is performed
    • an unstable sort is one that may return a different sort order when performed multiple times

    If a stable sort is desired, include at least one field in your sort that contains exclusively unique values. The easiest way to guarantee this is to include the _id field in your sort query.

    Consider the following restaurant collection:

    copy
    copied
    db.restaurants.insertMany( [
       { "_id" : 1, "name" : "Central Park Cafe", "borough" : "Manhattan"},
       { "_id" : 2, "name" : "Rock A Feller Bar and Grill", "borough" : "Queens"},
       { "_id" : 3, "name" : "Empire State Pub", "borough" : "Brooklyn"},
       { "_id" : 4, "name" : "Stan's Pizzaria", "borough" : "Manhattan"},
       { "_id" : 5, "name" : "Jane's Deli", "borough" : "Brooklyn"},
    ] );
    

    The following command uses the $sort stage to sort on the borough field:

    copy
    copied
    db.restaurants.aggregate(
       [
         { $sort : { borough : 1 } }
       ]
    )
    

    In this example, the sort is unstable, since the borough field contains duplicate values for both Manhattan and Brooklyn. Documents are returned in alphabetical order by borough, but the order of those documents with duplicate values for borough might not the be the same across multiple executions of the same sort. For example, here are the results from two different executions of the above command:

     
    { "_id" : 3, "name" : "Empire State Pub", "borough" : "Brooklyn" }
    { "_id" : 5, "name" : "Jane's Deli", "borough" : "Brooklyn" }
    { "_id" : 1, "name" : "Central Park Cafe", "borough" : "Manhattan" }
    { "_id" : 4, "name" : "Stan's Pizzaria", "borough" : "Manhattan" }
    { "_id" : 2, "name" : "Rock A Feller Bar and Grill", "borough" : "Queens" }
    
    { "_id" : 5, "name" : "Jane's Deli", "borough" : "Brooklyn" }
    { "_id" : 3, "name" : "Empire State Pub", "borough" : "Brooklyn" }
    { "_id" : 4, "name" : "Stan's Pizzaria", "borough" : "Manhattan" }
    { "_id" : 1, "name" : "Central Park Cafe", "borough" : "Manhattan" }
    { "_id" : 2, "name" : "Rock A Feller Bar and Grill", "borough" : "Queens" }
    

    While the values for borough are still sorted in alphabetical order, the order of the documents containing duplicate values for borough (i.e. Manhattan and Brooklyn) is not the same.

    To achieve a stable sort, add a field which contains exclusively unique values to the sort. The following command uses the $sort stage to sort on both the borough field and the _id field:

    copy
    copied
    db.restaurants.aggregate(
       [
         { $sort : { borough : 1, _id: 1 } }
       ]
    )
    

    Since the _id field is always guaranteed to contain exclusively unique values, the returned sort order will always be the same across multiple executions of the same sort.

  • 相关阅读:
    【转】EditText获取焦点不自动弹出键盘设置--失去焦点的方法,不错
    【转】eclipse android 设置及修改生成apk的签名文件 -- custom debug keystore
    【转】Adnroid4.0 签名混淆打包(conversion to dalvik format failed with error 1)
    【转】Android Fragment 基本介绍--不错
    【转】java代码中实现android背景选择的selector-StateListDrawable的应用
    【转】android官方侧滑菜单DrawerLayout详解
    【转】微信Android SDK示例代码及运行方法
    【转】Eclipse中设置ButterKnife进行注解式开发步骤 -- 不错
    【转】ButterKnife基本使用--不错
    python 命令行:help(),'more'不是内部或外部命令,也不是可运行的程序或批处理文件
  • 原文地址:https://www.cnblogs.com/rsapaper/p/14243940.html
Copyright © 2020-2023  润新知