我遇到的情况是,我必须使字段名称动态化,由变量和一些常量组成。我确实知道如何仅使用变量来创建字段名称。
var index = parseInt(req.body.index);
db.collection("user_data").update(
{ "user_id": user_id },
{ $addToSet: { "share.index.comment": usr_data } },
function (err, result) {
在字段空间中,我想使用具有恒定共享和注释的索引变量
最佳答案
你的意思是这样的:
var index = parseInt(req.body.index);
var commentData = { };
commentData["share." + index + ".comment"] = usr_data;
db.collection("user_data").update(
{ "user_id": user_id },
{ "$addToSet": commentData },
function (err, result) {
}
)
这样,“index”的值实际上用于语句中所需的“点符号”字符串。
关于javascript - 如何为nodejs和mongodb进行常量和变量混合查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44512224/