【问题标题】:$concat field with index in $map mongodb? [duplicate]$map mongodb中带有索引的$concat字段? [复制]
【发布时间】:2018-11-25 00:13:14
【问题描述】:

我有以下收藏

{
    "_id" : ObjectId("5b16405a8832711234bcfae7"),
    "createdAt" : ISODate("2018-06-05T07:48:45.248Z"),
    "firstName": "Bruce",
    "lastName": "Wayne"
},
{
    "_id" : ObjectId("5b16405a8832711234bcfae8"),
    "createdAt" : ISODate("2018-06-05T07:48:45.248Z"),
    "firstName": "Clerk",
    "lastName": "Kent"
},
{
    "_id" : ObjectId("5b16405a8832711234bcfae9"),
    "createdAt" : ISODate("2018-06-05T07:48:45.248Z"),
    "firstName": "Peter",
    "lastName": "Parker"
}

我需要$project 一个键索引和$concat 'INV-00' + 根元素的索引

我的输出应该是这样的

{
    "_id" : ObjectId("5b16405a8832711234bcfae7"),
    "createdAt" : ISODate("2018-06-05T07:48:45.248Z"),
    "firstName": "Bruce",
    "lastName": "Wayne",
    "index": "INV-001"
},
{
    "_id" : ObjectId("5b16405a8832711234bcfae8"),
    "createdAt" : ISODate("2018-06-05T07:48:45.248Z"),
    "firstName": "Clerk",
    "lastName": "Kent",
    "index": "INV-002"
},
{
    "_id" : ObjectId("5b16405a8832711234bcfae9"),
    "createdAt" : ISODate("2018-06-05T07:48:45.248Z"),
    "firstName": "Peter",
    "lastName": "Parker",
    "index": "INV-003"
}

我可以使用$dateToString 或其他方式将createdAt 格式更改为此Thu Jan 18 2018吗???

提前致谢!!!

【问题讨论】:

  • 更改 createdAt 键格式将如何帮助您推断文档的索引?试图了解你想要做什么
  • 我可以使用 $dateToString 或其他方式将 createdAt 格式更改为 2018 年 1 月 18 日星期四 只是想知道这可能吗?...与索引部分无关@天文

标签: node.js mongodb mongoose mongodb-query aggregation-framework


【解决方案1】:

虽然我肯定会建议您在客户端而不是在 MongoDB 内部执行此操作,但您可以通过以下方式获得所需的内容 - 相当暴力但有效:

db.collection.aggregate([
    // you should add a $sort stage here to make sure you get the right indexes
{
    $group: {
        _id: null, // group all documents into the same bucket
        docs: { $push: "$$ROOT" } // just to create an array of all documents
    }
}, {
    $project: {
        docs: { // transform the "docs" field
            $map: { // into something
                input: { $range: [ 0, { $size: "$docs" } ] }, // an array from 0 to n - 1 where n is the number of documents
                as: "this", // which shall be accessible using "$$this"
                in: {
                    $mergeObjects: [ // we join two documents
                        { $arrayElemAt: [ "$docs", "$$this" ] }, // one is the nth document in our "docs" array
                        { "index": { $concat: [ 'INV-00', { $substr: [ { $add: [ "$$this", 1 ] }, 0, -1 ] } ] } } // and the second document is the one with our "index" field
                    ]
                }
            }
        }
    }
}, {
    $unwind: "$docs" // flatten the result structure
}, {
    $replaceRoot: {
        newRoot: "$docs" // restore the original document structure
    }
}])

【讨论】:

  • @NeilLunn:a)您似乎没有阅读我的起始免责声明,这实际上与您在上述对该问题的评论中所建议的相同。 b) 并非每个人的收藏中都有数十亿份文件。 c)我很清楚的 BSON 限制不适用于聚合管道。
猜你喜欢
  • 1970-01-01
  • 2011-12-27
  • 1970-01-01
  • 1970-01-01
  • 2020-07-20
  • 2011-08-28
  • 2019-05-02
  • 2016-11-23
  • 1970-01-01
相关资源
最近更新 更多