gpt4 book ai didi

node.js - 更新具有数组和对象键的嵌套对象是动态的

转载 作者:可可西里 更新时间:2023-11-01 10:43:20 25 4
gpt4 key购买 nike

我有一种情况,我需要执行查询,其中有一个动态 key 生成来在数组中添加数据,比如

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

关于node.js - 更新具有数组和对象键的嵌套对象是动态的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28277036/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com