Mongodb

Mongodb使用表達式對數組中的數組對象進行排序

  • December 31, 2019

請幫助在 mongodb 中進行查詢。我想要使​​用某些表達式對數組對象進行排序的 mongo 查詢。例子。我有像這樣的文件

               {
               "_id:"5e0846c922427bdc824d3943",
               "isActive" : true,
               "customFieldArray" : [
                   {
                       "custom_field_id" : "fname",
                       "field_value" : "kevin"
                   },
                   {
                       "custom_field_id" : "lname",
                       "field_value" : "patel"
                   },
                   {
                       "custom_field_id" : "city",
                       "field_value" : "surat"
                   }
               ],
               "creationMode" : "adminForm",
               "createdAt" : ISODate("2019-11-21T06:40:20.033Z"),
               "updatedAt" : ISODate("2019-11-21T06:40:20.033Z"),
               "gender" : "Boy"

           },
           {
               "_id":"5e08471d6053718c808cbab8",
               "isActive" : true,
               "customFieldArray" : [
                    {
                       "custom_field_id" : "fname",
                       "field_value" : "alice"
                   },
                   {
                       "custom_field_id" : "lname",
                       "field_value" : "somani"
                   },
                   {
                       "custom_field_id" : "city",
                       "field_value" : "bharuch"
                   }
               ],
               "creationMode" : "adminForm",
               "createdAt" : ISODate("2019-11-21T06:40:20.033Z"),
               "updatedAt" : ISODate("2019-11-21T06:40:20.033Z"),
               "gender" : "Boy"

           },
           {
               "_id":"5e0847392b8622f8100267e3",
               "isActive" : true,
               "customFieldArray" : [
                    {
                       "custom_field_id" : "fname",
                       "field_value" : "Isha"
                   },
                   {
                       "custom_field_id" : "lname",
                       "field_value" : "Trivedi"
                   },
                   {
                       "custom_field_id" : "city",
                       "field_value" : "Mumbai"
                   }
               ],
               "creationMode" : "adminForm",
               "createdAt" : ISODate("2019-11-21T06:40:20.033Z"),
               "updatedAt" : ISODate("2019-11-21T06:40:20.033Z"),
               "gender" : "Girl"

           }

並且當在 custom_field_id = “fname” 上傳遞過濾參數時,它將以以下升序或降序方式顯示結果。

               {
               "_id":"5e08471d6053718c808cbab8",
               "isActive" : true,
               "customFieldArray" : [
                    {
                       "custom_field_id" : "fname",
                       "field_value" : "alice"
                   },
                   {
                       "custom_field_id" : "lname",
                       "field_value" : "somani"
                   },
                   {
                       "custom_field_id" : "city",
                       "field_value" : "bharuch"
                   }
               ],
               "creationMode" : "adminForm",
               "createdAt" : ISODate("2019-11-21T06:40:20.033Z"),
               "updatedAt" : ISODate("2019-11-21T06:40:20.033Z"),
               "gender" : "Boy",

           },{
               "_id":"5e0847392b8622f8100267e3",
               "isActive" : true,
               "customFieldArray" : [
                    {
                       "custom_field_id" : "fname",
                       "field_value" : "Isha"
                   },
                   {
                       "custom_field_id" : "lname",
                       "field_value" : "Trivedi"
                   },
                   {
                       "custom_field_id" : "city",
                       "field_value" : "Mumbai"
                   }
               ],
               "creationMode" : "adminForm",
               "createdAt" : ISODate("2019-11-21T06:40:20.033Z"),
               "updatedAt" : ISODate("2019-11-21T06:40:20.033Z"),
               "gender" : "Girl",

           },
           {
               "_id:"5e0846c922427bdc824d3943",
               "isActive" : true,
               "customFieldArray" : [
                   {
                       "custom_field_id" : "fname",
                       "field_value" : "kevin"
                   },
                   {
                       "custom_field_id" : "lname",
                       "field_value" : "patel"
                   },
                   {
                       "custom_field_id" : "city",
                       "field_value" : "surat"
                   }
               ],
               "creationMode" : "adminForm",
               "createdAt" : ISODate("2019-11-21T06:40:20.033Z"),
               "updatedAt" : ISODate("2019-11-21T06:40:20.033Z"),
               "gender" : "Boy"

           }

聚合根據輸入 INPUT_EXPRESSION 和 SORT_ORDER對*文件進行排序。輸入表達式與customFieldArraycustom_field_id值匹配,*文件根據對應的 進行排序field_value

var INPUT_EXPRESSION = "fname";    // values can be "fname", "city" or "lname" 
var SORT_ORDER = 1;    // values can be 1 (ascending) or -1 (descending)

db.test.aggregate( [
 { 
     $addFields: { 
         customFieldTemp: {
             $filter: {
                  input: "$customFieldArray",
                     as: "arr",
                   cond: {
                            $eq: [ "$$arr.custom_field_id", INPUT_EXPRESSION ]
                   }
             }
         }
     } 
 },
 { 
     $addFields: { 
         customFieldTemp: { $arrayElemAt: [ "$customFieldTemp", 0 ] }
     } 
 },
 { 
     $sort: { "customFieldTemp.field_value" : SORT_ORDER } 
 },
 { 
     $project: { customFieldTemp: 0 } 
 }
] )

範例

從問題文章中的範例輸入文件中,如果 INPUT_EXPRESSION = “city”SORT_ORDER = 1,則文件排序為:( _id: "5e08471d6053718c808cbab8"“bharuch”)、_id: "5e0847392b8622f8100267e3"(“mumbai”) 和_id: "5e0846c922427bdc824d3943"(“surat”)。

引用自:https://dba.stackexchange.com/questions/256185