Intereting Posts

如何在MongoDB中插入一个嵌套对象?

我有一个这样的自行车集合:

{ "fname": "foo", "indian":"hero-corps" "brands": [ { "region": "asia", "type": "terrain" } ] } 

并通过这样的post得到一个JSON(让我们把它命名为jsonBody ):

 { "indian": "hero-corps", "someKeyA": "someValueA", } 

我正在使用下面的mongo更新查询:

  db.collection(bikes).update({"indian":"hero-corps"},{$set:jsonBody}, {upsert:true}); 

问题在于它是在主对象内部插入,我只想用jsonBody在嵌套的对象品牌插入 。 我如何做到这一点?

实际结果:

 { "fname": "foo", "indian": "hero-corps", "brands": [ { "region": "asia", "type": "terrain" } ], "indian": "hero-corps", "someKeyA": "someValueA", } 

预期结果:

 { "fname": "foo", "indian": "hero-corps", "brands": [ { "region": "asia", "type": "terrain", "someKeyA": "someValueA", } ] } 

Solutions Collecting From Web of "如何在MongoDB中插入一个嵌套对象?"