我有两个要下采样的数组,并保留其中一个索引.我的查询目前可以工作,但它想要保留的索引被移到数组的后面.如何在索引之前对两个连接的数组进行排序?

Query:

db.collection.update({
  "_id": "KVLCC_FL_V1_Fx_NN_1"
},
[
  {
    $set: {
      "df.data.Epoch": {
        $map: {
          input: {
            $concatArrays: [
              {
                $range: [
                  0,
                  {
                    $size: "$df.data.Epoch"
                  },
                  10
                ]
              },
              [
                {
                  $indexOfArray: [
                    "$df.data.Epoch",
                    200.0
                  ]
                }
              ]
            ]
          },
          as: "index",
          in: {
            $arrayElemAt: [
              "$df.data.Epoch",
              "$$index"
            ]
          }
        }
      },
      "df.data.Loss": {
        $map: {
          input: {
            $concatArrays: [
              {
                $range: [
                  0,
                  {
                    $size: "$df.data.Epoch"
                  },
                  10
                ]
              },
              [
                {
                  $indexOfArray: [
                    "$df.data.Epoch",
                    200.0
                  ]
                }
              ]
            ]
          },
          as: "index",
          in: {
            $arrayElemAt: [
              "$df.data.Loss",
              "$$index"
            ]
          }
        }
      }
    }
  }
])

Input

[
  {
    "_id": "KVLCC_FL_V1_Fx_NN_1",
    "_cls": "PlottingData",
    "_created_by": "cian.byrne",
    "_modified_by": "cian.byrne",
    "best_model_epoch": 4200.0,
    "df": {
      "data": {
        "Epoch": [
          0.0,
          200.0,
          400.0,
          600.0,
          800.0,
          1000.0,
          1200.0,
          1400.0,
          1600.0,
          1800.0,
          2000.0,
          2200.0,
          2400.0,
          2600.0,
          2800.0,
          3000.0,
          3200.0,
          3400.0,
          3600.0,
          3800.0,
          4000.0,
          4200.0,
          4400.0,
          4600.0,
          4800.0,
          5000.0,
          5200.0,
          5400.0,
          5600.0,
          5800.0,
          6000.0,
          6200.0,
          6400.0,
          6600.0,
          6800.0,
          7000.0,
          7200.0,
          7400.0,
          7600.0,
          7800.0,
          8000.0,
          8200.0,
          8400.0,
          8600.0,
          8800.0,
          9000.0,
          9200.0,
          9400.0,
          9600.0,
          9800.0,
          10000.0,
          10200.0,
          10400.0,
          10600.0,
          10800.0,
          11000.0,
          11200.0,
          11400.0,
          11600.0,
          11800.0,
          12000.0,
          12200.0,
          12400.0,
          12600.0,
          12800.0,
          13000.0,
          13200.0,
          13400.0,
          13600.0,
          13800.0,
          14000.0,
          14200.0,
          14400.0,
          14600.0,
          14800.0,
          15000.0,
          15200.0,
          15400.0,
          15600.0,
          15800.0,
          16000.0,
          16200.0,
          16400.0,
          16600.0,
          16800.0,
          17000.0,
          17200.0,
          17400.0,
          17600.0,
          17800.0,
          18000.0,
          18200.0,
          18400.0,
          18600.0,
          18800.0,
          19000.0,
          19200.0,
          19400.0,
          19600.0,
          19800.0,
          19990.0
        ],
        "Loss": [
          0.06867625160459633,
          0.0016397159897495028,
          8.232398220272552E-4,
          6.775707972243301E-4,
          4.489408345685778E-4,
          2.917215254253416E-4,
          1.9186506476001467E-4,
          1.3758187272715685E-4,
          1.2493897751147704E-4,
          1.1541674533799862E-4,
          1.0649950315778829E-4,
          9.781559886855942E-5,
          9.02215432011161E-5,
          8.367948119200934E-5,
          7.811427335601343E-5,
          7.346417222218641E-5,
          6.950470835636746E-5,
          6.615059024721208E-5,
          6.33605867341399E-5,
          6.108318271821619E-5,
          5.929961396323725E-5,
          5.785751815850572E-5,
          5.66078643747544E-5,
          5.575514016288512E-5,
          5.490662687384637E-5,
          5.4275713865291395E-5,
          5.367539818879205E-5,
          5.311907919764326E-5,
          5.2673995849290626E-5,
          5.211076472495092E-5,
          5.168543824245904E-5,
          5.125842093557059E-5,
          5.053790301043555E-5,
          4.9908111976487975E-5,
          4.960618507642075E-5,
          4.829734415482695E-5,
          4.7464273211136486E-5,
          4.6148099429591614E-5,
          4.439996655969897E-5,
          4.281351079340219E-5,
          4.0903927787967375E-5,
          3.966564459170491E-5,
          3.8482641271535875E-5,
          3.805266163357444E-5,
          3.767043343357998E-5,
          3.735670042644782E-5,
          3.674643635627615E-5,
          3.611821828035745E-5,
          3.5825270581725845E-5,
          3.58994404537605E-5,
          3.562802514136092E-5,
          3.490789039262893E-5,
          3.451970186551463E-5,
          3.4615366551192576E-5,
          3.450501472887952E-5,
          3.407997332656331E-5,
          3.361330450884516E-5,
          3.322511360047369E-5,
          3.293475797707948E-5,
          3.279222747881219E-5,
          3.28951127991267E-5,
          3.304805974640318E-5,
          3.255722881984856E-5,
          3.224398028834877E-5,
          3.260477365161867E-5,
          3.2016552433270515E-5,
          3.217527870953099E-5,
          3.234731259436457E-5,
          3.1846973848411254E-5,
          3.166517530176324E-5,
          3.1847863310516914E-5,
          3.204966548883148E-5,
          3.211315165944342E-5,
          3.204628412479789E-5,
          3.1914853877084775E-5,
          3.177698125159182E-5,
          3.1660111155823836E-5,
          3.156741941424468E-5,
          3.149204956705131E-5,
          3.142555504546063E-5,
          3.136085494497657E-5,
          3.129326371368332E-5,
          3.122192171759931E-5,
          3.115274542156675E-5,
          3.1102835614209874E-5,
          3.1101085756010995E-5,
          3.118033068371349E-5,
          3.132809554019229E-5,
          3.1397330800609355E-5,
          3.133790020495167E-5,
          3.0907137414636935E-5,
          3.059938845657662E-5,
          3.0962577902471116E-5,
          3.047035907495237E-5,
          3.0465281107359487E-5,
          2.9821520532225177E-5,
          2.9886149687907263E-5,
          2.9793037437862734E-5,
          2.9501736086822074E-5,
          2.9003898368964265E-5,
          2.870838411555099E-5
        ]
      }
    }
  }
]

Output

[
  {
    "_cls": "PlottingData",
    "_created_by": "cian.byrne",
    "_id": "KVLCC_FL_V1_Fx_NN_1",
    "_modified_by": "cian.byrne",
    "best_model_epoch": 4200,
    "df": {
      "data": {
        "Epoch": [
          0,
          2000,
          4000,
          6000,
          8000,
          10000,
          12000,
          14000,
          16000,
          18000,
          19990,
          200
        ],
        "Loss": [
          0.06867625160459633,
          0.00010649950315778829,
          5.929961396323725e-05,
          5.168543824245904e-05,
          4.0903927787967375e-05,
          3.562802514136092e-05,
          3.28951127991267e-05,
          3.1847863310516914e-05,
          3.136085494497657e-05,
          3.0907137414636935e-05,
          2.870838411555099e-05,
          0.0016397159897495028
        ]
      }
    }
  }
]

和一个playground :https://mongoplayground.net/p/_yfm_B0nq18

推荐答案

你可以通过一个$sortArray运算符来传递连接的数组,以在输入到map输入之前得到一个排序的array.

        $map: {
          input: {
            $sortArray: {
              input: {
                $concatArrays: [
                  {
                    $range: [
                      0,
                      {
                        $size: "$df.data.Epoch"
                      },
                      10
                    ]
                  },
                  [
                    {
                      $indexOfArray: [
                        "$df.data.Epoch",
                        200.0
                      ]
                    }
                  ]
                ]
              },
              sortBy: 1
            }
          },
          as: "index",
          in: {
            $arrayElemAt: [
              "$df.data.Loss",
              "$$index"
            ]
          }
        }

playground

Mongodb相关问答推荐

如何在MongoDB中通过限制和跳过查找项进行匹配

MongoDB对两个串联数组进行排序

为什么这个查询可以在MongoDB中使用?

从 Amazon S3(Next.js、Mongodb、Mongoose)删除图像

从MongoDB迁移到PostgreSQL:为PostgreSQL编写聚合管道查询

更快地识别嵌套对象数组中具有重复字段的文档

错误起草的 MongoDB 聚合管道 $match 阶段

在 ExpirePolicy 之后从 Store 中删除元素

MongoDB聚合 - 用另一个数组过滤数组

System.FormatException occurred in MongoDB.Bson.dll - XXX is not a valid 24 digit hex string

在 Mongoose 中清理用户输入

Mongo:统计一组文档中单词出现的次数

在 GridFS、express、mongoDB、node.js 中存储来自 POST 请求的数据流

Ruby 按键值分组哈希

使用 Java 驱动程序更新 MongoDB 中的数组

Mongo: query by key one level deep

RoboMongo:不显示所有文档

在mongoose中查询虚拟属性

MongoDB mongoexport 查询

MissingSchemaError:Schema hasn't been registered for model