Mongodb 将mongo中的元素移动或移位到其他json元素
我需要将集合中的所有Mongodb 将mongo中的元素移动或移位到其他json元素,mongodb,Mongodb,我需要将集合中的所有timestamp元素/字段移动到MongoDB中的jsonObject元素。是否有这样做的查询或函数。。以下是一份文件样本: { "_id" : ObjectId("5ca307e333ad6127e0ddbe40"), "_class" : "com.google.mongo.docs.IvrMongoLog", "jsonObject" : { "message" : [ {
timestamp
元素/字段移动到MongoDB
中的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"
}
在这里,我想将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"
}
}
你可以用
您想修改现有的数据库结构,还是在查询时仅重塑您的文档?在所有文档中,我希望timstamp能够在jsonObject中一劳永逸地移动。查询
db.ivrMongoLog.updateMany({},{$rename:{'timestamp':'jsonObject.timestamp'})
解决了我的问题。非常感谢@mickl。
db.col.updateMany({}, { $rename: { 'timestamp': 'jsonObject.timestamp' } }