将mongo中的元素移动或移动到其他json元素

时间:2019-06-21 07:39:15

标签: mongodb

我需要将集合中的所有timestamp元素/字段移动到jsonObject中的MongoDB元素。是否有查询或函数。.以下是文档样本:

{
    "_id" : ObjectId("5ca307e333ad6127e0ddbe40"),
    "_class" : "com.google.mongo.docs.IvrMongoLog",
    "jsonObject" : {
        "message" : [ 
            {
                "time" : "03:00:23",
                "action" : "call ended succesfully ",
                "user_input" : "user hangup the call",
                "language" : "en"
            }
        ],
        "msisdn" : "89######66"
    },
    "timestamp" : "2019-06-01 12:00:00:00"
}

在这里,我想将timestamp元素移到jsonObject内部。像这样的东西:

{
    "_id" : ObjectId("5ca307e333ad6127e0ddbe40"),
    "_class" : "com.google.mongo.docs.IvrMongoLog",
    "jsonObject" : {
        "message" : [ 
            {
                "time" : "03:00:23",
                "action" : "call ended succesfully ",
                "user_input" : "user hangup the call",
                "language" : "en"
            }
        ],
        "msisdn" : "89######66",
        "timestamp" : "2019-06-01 12:00:00:00"
    }
}

1 个答案:

答案 0 :(得分:2)

您可以使用$rename

db.col.updateMany({}, { $rename: { 'timestamp': 'jsonObject.timestamp' } }