我知道使用Node.js更新MongoDB中嵌入对象中的特定字段值。假设我的数据库结构是

时间:2016-04-11 17:14:55

标签: node.js mongodb mlab

我知道使用Node.js更新MongoDB中嵌入对象中的特定字段值。假设我的数据库结构是

"Lemon Soda": [
        {
            "RecipeName": "Lemon Soda"
            "Likes": 121
        }
       ]

所以我可以使用update函数来更新嵌入式数组“Lemon Soda”里面的'Likes'值,如:`

 db.collection(db_collection).update(
                {"Lemon Soda.RecipeName": db_recipe},
                {$set: {"Gasbased.Likes": "122"}}, function (err, results) {
                    console.log("likes updated" + count);
                    console.log(results);
                }
            )
`

其中'db_collection'是存储集合名称的变量,'db_recipe'是存储“Lemon Soda”的变量。 现在假设我在一个名为'db_recipe'的变量中有'Lemon Soda',并且我想更改/更新'Likes'字段,该字段使用'db_recipe'代替"Lemon Soda.RecipeName": db_recipe在'Lemon Soda'嵌入式数组中。 我怎么能这样做,因为我们不能在我们的查询中使用变量,如

{"[db_recipe].RecipeName": db_recipe}

此致 Shiven

0 个答案:

没有答案