Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
向mongodb查询中的项目添加序列号_Mongodb_Sorting - Fatal编程技术网

向mongodb查询中的项目添加序列号

向mongodb查询中的项目添加序列号,mongodb,sorting,Mongodb,Sorting,我有MongoDB集合项目和以下文档: { "name": "First", "value": 8 }, { "name": "Second", "value": 2 }, { "name": "Third", "value": 5 } 我想按名称或值对它们进行排序,然后向每个项添加序号(索引属性)(第一项将有“索引

我有MongoDB集合
项目
和以下文档:

{ "name": "First", "value": 8 },
{ "name": "Second", "value": 2 },
{ "name": "Third", "value": 5 }
我想按
名称
对它们进行排序,然后向每个项添加序号(
索引
属性)(第一项将有
“索引”:1
,第二项
“索引”:2
,等等)。所以对于这个查询:

db.items.aggregate([{ $sort: { "value": 1 } }])
结果应该是:

{ "name": "Second", "value": 2, "index": 1 },
{ "name": "Third", "value": 5, "index": 2 },
{ "name": "First", "value": 8, "index": 3 }

这也适用于
$skip
。使用
{“$skip”:1}
,结果应该是索引为
2,3
的项,而不是
1,2
。使用纯MongoDB,没有任何应用程序逻辑,有什么方法可以做到这一点吗?

这可能是您可以在
$unwind
中使用的一个选项

  • $sort
    按已执行的值排序
  • $group
    通过
    名称
    并推入
    项目
  • $unwind
    解构
    项目
    并在
    includearayindex
  • $project
    必填字段和递增索引,因为它以
    0
    0开头

游乐场:

这可能是您可以在
$unwind
中使用的一个选项

  • $sort
    按已执行的值排序
  • $group
    通过
    名称
    并推入
    项目
  • $unwind
    解构
    项目
    并在
    includearayindex
  • $project
    必填字段和递增索引,因为它以
    0
    0开头

操场:

注:灵感来自@turvishal的回答,以满足以下要求

  • 从1开始的索引
  • 跳过OP想要的方式

  • 注意:灵感来自@turvishal的回答,以满足以下要求

  • 从1开始的索引
  • 跳过OP想要的方式

  • 您是否有跳过排序输出或向排序结果中添加数字的问题?@Gibbs向排序结果中添加数字的问题。您是否有跳过排序输出或向排序结果中添加数字的问题?@Gibbs向排序结果中添加数字的问题。谢谢您的回答,我确实忘记了添加
    $add
    操作,谢谢。是的,我希望OP能接受你的回答,因为你真正解决了主要问题。我做了我能做的(+1)::)保留rockingits好的我不介意,OP应该对解决方案感到满意。谢谢你的回答,我真的忘了添加
    $add
    操作,谢谢。是的,我希望OP接受你的回答,因为你真正解决了主要问题。我做了我能做的(+1)::)保持Rockingit好吧,我不介意,OP应该对解决方案感到满意。
    db.collection.aggregate([
      { $sort: { value: 1 } },
      {
        $group: {
          _id: 1,
          items: {
            $push: {
              name: "$name",
              value: "$value"
            }
          }
        }
      },
      {
        $unwind: {
          path: "$items",
          includeArrayIndex: "index"
        }
      },
      {
        $project: {
          _id: 0,
          name: "$items.name",
          value: "$items.value",
          index: { $add ["$index", 1] }
        }
      }
    ])
    
    db.collection.aggregate([
      {
        $sort: {
          value: 1
        }
      },
      {
        $group: {
          _id: 1,
          items: {
            $push: {
              name: "$name",
              value: "$value"
            }
          }
        }
      },
      {
        $unwind: {
          path: "$items",
          includeArrayIndex: "index"
        }
      },
      {
        $project: {
          _id: 0,
          name: "$items.name",
          value: "$items.value",
          index: { //Incrementing the index by 1
            $add: [
              "$index",
              1
            ]
          }
        }
      },
      { //Addition of skip 
        "$skip": 1
      }
    ])