使用dynamic密钥推送到MongoDB数组

我试图通过dynamic密钥推送数据

数据库结构:

{ "obj1":{ "array":[ { "field1":"text1" }, { "field2":"text2" } ] }, "id":123; }, { "obj2":{ "array":[ { "field1":"text1" }, { "field2":"text2" } ] }, "id":1234; } 

我试图使用variables作为地图path中的键:

 var a = 'obj2'; db.collection('fooCollection').update({'id':1234},{$push:{a.array:{ "field3":"text3"}}}); 

如果我做:

 db.collection('fooCollection').update({'id':1234},{$push:{"obj2.array":{ "field3":"text3"}}}); 

它的工作原理,但我非常需要使用dynamic密钥。

这不能用对象文字来完成。 尝试这个:

 var a = 'obj2'; var pushObj = {}; pushObj[a + '.array'] = { "field3": "text3" }; db.collection('fooCollection').update({ 'id':1234 }, { $push: pushObj });