【问题标题】:Single array of objects sort and slice not working单个对象数组排序和切片不起作用
【发布时间】:2021-06-29 17:18:29
【问题描述】:

我在这样的集合中有一个条目:

{
    "_id" : ObjectId("60c6f7a5ef86bd1a5402e928"),
    "cid" : 1,
    "array1" : [
       { "type": "car", value: 20 },
       { "type": "bike", value: 50 },
       { "type": "bus", value: 5 },
       { "type": "cycle", value: 100 },
...... 9000 more entry something like this
     ],
"array2" : [
       { "type": "laptop", value: 200 },
       { "type": "desktop", value: 15 },
       { "type": "tablet", value: 55 },
       { "type": "mobile", value: 90 },
...... 9000 more entry something like this
     ]
}

现在我想为分页目的对数据进行排序和切片。

为此,我编写了适用于切片情况但不适用于排序情况的查询。 这是我的查询,适用于切片情况

let val = await SomeCollectionName.findOne(
{ cid: 1 },
{ _id: 1 , array1: { $slice: [0, 10] } } ---> its return the 10 data. Initially it return from 0 to 10, then next call $slice: [10, 10]
).exec();
if (val) {
  //console.log('Got the value')
}
console.log(error)

这是我使用切片添加排序时的查询

let val = await SomeCollectionName.findOne(
{ cid: 1 },
{ _id: 1 , array1: { $sort: { value: -1 }, $slice: [0, 10] } }
).exec();
if (val) {
  //console.log('Got the value')
}
console.log(error)

有没有人指导我哪里错了或建议我什么是获取数据的有效方法。

更新

我正在从上述问题中得到答案,并为两个数组寻找相同的实现。 一切都是一样的。之前我在处理 1 个数组,这次我必须处理两个数组。

只是想知道这些事情是如何发生的 我编写了聚合查询,但一个数组结果很好,但其他数组在整个数组中返回相同的数据。

根据使用排序和切片处理单个数组的建议,这是我的查询

db.collection.aggregate([
  {
    "$match": {
      "cid": 1
    }
  },
  {
    $unwind: "$array1"
  },
  {
    $unwind: "$array2"
  },
  {
    "$sort": {
      "array1.value": -1,
      "array2.value": -1,
    }
  },
  {
    $skip: 0
  },
  {
    $limit: 3
  },
  {
   $group:{
    "_id":"$_id",
    "array1":{$push:"$array1"},
    "array2":{$push:"$array2"}
    }
   }
])

【问题讨论】:

标签: mongodb sorting mongoose slice


【解决方案1】:

问题是 findOne() 在其投影参数中不支持 $sort。

您可以改为使用聚合来实现预期结果,

db.collection.aggregate([
  {
    "$match": {
      "cid": 1
    }
  },
  {
    $unwind: "$array1"
  },
  {
    "$sort": {
      "array1.value": -1
    }
  },
  {
    $skip: 0
  },
  {
    $limit: 3
  },
  {
    $group: {
      "_id": "$_id",
      "array1": {
        $push: {
          "type": "$array1.type",
          "value": "$array1.value"
        }
      },
      "array2": {
        "$first": "$array2"
      }
    },
    
  },
  {
    $unwind: "$array2"
  },
  {
    "$sort": {
      "array2.value": -1
    }
  },
  {
    $skip: 0
  },
  {
    $limit: 3
  },
  {
    $group: {
      "_id": "$_id",
      "array2": {
        $push: {
          "type": "$array2.type",
          "value": "$array2.value"
        }
      },
      "array1": {
        "$first": "$array1"
      }
    },
    
  }
])

Aggregation

$unwind

【讨论】:

  • 是否可以在同一个查询中对两个数组做同样的事情。像目前我们正在对单个数组进行排序和切片
  • 是的,你也可以展开第二个数组。
  • 我试过了,但一个数组数据返回正常,但另一个在整个数组中返回相同的数据
  • 我已经更新了这个问题。你能检查并建议我吗
  • 您不能在 array1 之后立即展开 array2。因为您正在展开前一阶段的输出,即 array1 的展开。在这种情况下,你可以做的是在 array1 的 $group 之后从 $unwind 应用相同的阶段,认为这对我来说听起来不是最好的方法。
猜你喜欢
  • 1970-01-01
  • 2018-09-18
  • 2020-12-19
  • 2011-10-07
  • 2021-02-06
  • 2021-09-03
  • 2015-08-25
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多