我需要将集合中的所有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"
}
}
答案 0 :(得分:2)
您可以使用$rename
db.col.updateMany({}, { $rename: { 'timestamp': 'jsonObject.timestamp' } }