Node.js 动态更新具有数组和对象键的嵌套对象
我有一种情况需要执行查询,在这种情况下,需要动态生成密钥来在数组中添加数据,比如Node.js 动态更新具有数组和对象键的嵌套对象,node.js,mongodb,mongodb-query,Node.js,Mongodb,Mongodb Query,我有一种情况需要执行查询,在这种情况下,需要动态生成密钥来在数组中添加数据,比如 var tp = 'unique_key'; db.sr_caller_info.update({caller:9967771131, month:201501},{$push :{ data:{tp:{ "abc" : 12, "xyz" : 30, "start_epoch" : "", "answer_epoch" : "", "end_epoch" : "",
var tp = 'unique_key';
db.sr_caller_info.update({caller:9967771131, month:201501},{$push :{ data:{tp:{
"abc" : 12,
"xyz" : 30,
"start_epoch" : "",
"answer_epoch" : "",
"end_epoch" : "",
"file" : "xxx",
}}}})
我的数据库结构如下
{
"caller": xxxxx,
"circle": "xxxx",
"data": {
"unique_key1": [
{
"abc": 12,
"xyz": 30,
"start_epoch": "",
"answer_epoch": "",
"end_epoch": "",
"file": "xxx",
},
{
"abc": 12,
"xyz": 30,
"start_epoch": "",
"answer_epoch": "",
"end_epoch": "",
"file": "xxx",
}
],
"unique_key": [
{
"abc": 12,
"xyz": 30,
"start_epoch": "",
"answer_epoch": "",
"end_epoch": "",
"file": "xxx",
},
{
"abc": 12,
"xyz": 30,
"start_epoch": "",
"answer_epoch": "",
"end_epoch": "",
"file": "xxx",
},
{
"abc": 12,
"xyz": 30,
"start_epoch": "",
"answer_epoch": "",
"end_epoch": "",
"file": "xxx",
}
]
},
"month": 201501
}
我使用的是nodejs mongo驱动程序1.4
所以我想通过创建动态嵌套键来进行更新
谢谢这个怎么样:
var actual_obj = 'obj2';
var mongoPushObj = {};
pushObj[actual_obj + '.array'] = { "namefield": "text3" };
db.collection('mongoCollection').update({ 'id':1234 }, { $push: pushObj });
谢谢,伙计,它解决了