1. <tfoot id='d5Neg'></tfoot>

    2. <small id='d5Neg'></small><noframes id='d5Neg'>

      <i id='d5Neg'><tr id='d5Neg'><dt id='d5Neg'><q id='d5Neg'><span id='d5Neg'><b id='d5Neg'><form id='d5Neg'><ins id='d5Neg'></ins><ul id='d5Neg'></ul><sub id='d5Neg'></sub></form><legend id='d5Neg'></legend><bdo id='d5Neg'><pre id='d5Neg'><center id='d5Neg'></center></pre></bdo></b><th id='d5Neg'></th></span></q></dt></tr></i><div id='d5Neg'><tfoot id='d5Neg'></tfoot><dl id='d5Neg'><fieldset id='d5Neg'></fieldset></dl></div>
        <bdo id='d5Neg'></bdo><ul id='d5Neg'></ul>
    3. <legend id='d5Neg'><style id='d5Neg'><dir id='d5Neg'><q id='d5Neg'></q></dir></style></legend>

    4. 如何在 Mongoose 排序结束时保持空值?

      时间:2023-09-08
          <i id='Mqss7'><tr id='Mqss7'><dt id='Mqss7'><q id='Mqss7'><span id='Mqss7'><b id='Mqss7'><form id='Mqss7'><ins id='Mqss7'></ins><ul id='Mqss7'></ul><sub id='Mqss7'></sub></form><legend id='Mqss7'></legend><bdo id='Mqss7'><pre id='Mqss7'><center id='Mqss7'></center></pre></bdo></b><th id='Mqss7'></th></span></q></dt></tr></i><div id='Mqss7'><tfoot id='Mqss7'></tfoot><dl id='Mqss7'><fieldset id='Mqss7'></fieldset></dl></div>

            <tbody id='Mqss7'></tbody>
            • <bdo id='Mqss7'></bdo><ul id='Mqss7'></ul>
              <tfoot id='Mqss7'></tfoot>

                <small id='Mqss7'></small><noframes id='Mqss7'>

                <legend id='Mqss7'><style id='Mqss7'><dir id='Mqss7'><q id='Mqss7'></q></dir></style></legend>
              1. 本文介绍了如何在 Mongoose 排序结束时保持空值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                我正在查询一个集合并根据一个属性对结果进行排序.有些文档还没有该属性值,即 null.在排序和限制(asc 或 desc)之后,我想在结果末尾保留具有空值的文档.Mongoose 中是否有一种简单的方法可以使用单个查询来做到这一点?如果没有,我如何分别使用两个查询,因为我必须限制结果以及分页?

                I am querying a collection and sorting results based on a property. Some documents don't the that property value yet i.e. null. I want to keep the documents with null values at the end of the results after sorting and limiting (asc or desc). Is there a simple way in Mongoose to do that using a single query? If not, how can I use two queries separately since I have to limit the results as well for pagination?

                var dealSchema = new Schema({
                
                    // For Presentation Purposes Only
                    dealId: { type: Number, index: true, unique: true },
                
                    // BRAND Info
                    brand: { type: ObjectId, ref: 'brand', index: true },
                    brandUser: { type: ObjectId, ref: 'user', index: true, required: true },
                    campaign: { type: ObjectId, ref: 'campaign', index: true },
                
                    metrics: {
                        totalCost: Number,
                        totalValue: Number,
                        roi: { type: Number, index: true },
                        cpe: { type: Number, index: true }
                    }
                
                })
                

                我想根据metrics.cpe"按升序排序,但希望 null 或 0 值位于列表末尾.示例:

                I want to sort based on 'metrics.cpe' in ascending order, but want null or 0 values to be at the end of the list. Example:

                [0.1, 0.4, 0.7, 1.5, 2.5, 0, 0, null, null]
                

                示例文档

                {
                  "_id": "590cdf29c102ae31208fa43a",
                  "campaign": "587e6a880c844c6c2ea01563",
                  "brand": "5a4cff5f8eaa1c26ca194acc",
                  "brandUser": "57fd30cf0df04f1100153e07",
                  "metrics": {
                    "roi": 0,
                    "cpe": 0,
                    "totalValue": 0,
                    "totalCost": 6000
                  }
                }
                

                推荐答案

                我不确定我要说的解决方案.我无法测试这一点,因为我现在没有设置 mongo db,但我认为你可以使用 <collection>.aggregate 以及 $project$sort 来实现这一点.

                Am not sure about the solution am about to say. I cant test this out as I dont have a mongo db set right now, but I think that you can use <collection>.aggregate along with $project and $sort to achieve this.

                示例代码:

                db.inventory.aggregate(
                   [
                      {
                         $project: {
                            item: 1,
                            description: { $ifNull: [ "$amount", -1*(<mimimum value>)* ] }
                         }
                      },
                      { 
                         $sort : { 
                           amount : (-1 or 1 depending on the order you want)
                         }
                      }
                   ]
                )
                

                希望对你有帮助!!

                这篇关于如何在 Mongoose 排序结束时保持空值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:如何在 MongoDB 中创建长时间戳的 TTL 索引 下一篇:如何制作 Parse.Query.AND?

                相关文章

                最新文章

                1. <i id='qE3QQ'><tr id='qE3QQ'><dt id='qE3QQ'><q id='qE3QQ'><span id='qE3QQ'><b id='qE3QQ'><form id='qE3QQ'><ins id='qE3QQ'></ins><ul id='qE3QQ'></ul><sub id='qE3QQ'></sub></form><legend id='qE3QQ'></legend><bdo id='qE3QQ'><pre id='qE3QQ'><center id='qE3QQ'></center></pre></bdo></b><th id='qE3QQ'></th></span></q></dt></tr></i><div id='qE3QQ'><tfoot id='qE3QQ'></tfoot><dl id='qE3QQ'><fieldset id='qE3QQ'></fieldset></dl></div>

                    <small id='qE3QQ'></small><noframes id='qE3QQ'>

                    <legend id='qE3QQ'><style id='qE3QQ'><dir id='qE3QQ'><q id='qE3QQ'></q></dir></style></legend>
                    • <bdo id='qE3QQ'></bdo><ul id='qE3QQ'></ul>
                    <tfoot id='qE3QQ'></tfoot>