$map mongodb中带有索引的$concat字段? [英] $concat field with index in $map mongodb?

查看:11
本文介绍了$map mongodb中带有索引的$concat字段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下收藏

{
    "_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' + 根元素的索引

I need to $project one more key index with $concat with 'INV-00' + index of the root element

我的输出应该是这样的

{
    "_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 还是别的什么???

and can I change createdAt format to this Thu Jan 18 2018 using $dateToString or something else???

提前致谢!!!

推荐答案

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

While I would certainly recommend you to do that on the client side as opposed to inside MongoDB, here is how you could get what you want - pretty brute-force but working:

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
    }
}])

这篇关于$map mongodb中带有索引的$concat字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆