使用Mongoose进行动态查询
Make dynamic query with Mongoose
我正在尝试使用Mongoose创建一个动态条件,但它不像我想象的那样工作。
代码是这样的
id = req.params.questionId;
index = req.params.index;
callback = function(err,value){
if(err){
res.send(err)
}else{
res.send(value)
}
};
conditions = {
"_id": id,
"array._id": filterId
};
updates = {
$push: {
"array.$.array2."+index+".answeredBy": userId
}
};
options = {
upsert: true
};
Model.update(conditions, updates, options, callback);
如您所见,我试图使用"index"变量创建动态条件。有可能这样做吗?
提前感谢!
您需要分两步创建updates
对象:
var updates = { $push: {} };
updates.$push["array.$.array2." + index + ".answeredBy"] = userId;
现在node.js 4+支持计算属性名,你可以一步完成:
var updates = { $push: {
["array.$.array2." + index + ".answeredBy"]: userId
} };
相关文章:
- 如何使用pouchdb创建动态查询(couchdb可能也是如此)
- 新手查询动态添加属性
- j查询动态表计算
- 如何根据查询字符串值创建动态超链接
- 我对这个动态查询做错了什么
- 动态添加数据时按数据属性查询JQuery选择器
- 具有动态查询的Webpack加载器
- 从Ember.js中的选择框动态更改当前ajax查找查询
- mongoDB动态查询中的日期筛选器
- 从 URL - AngularJS 获取动态查询字符串参数
- 角度 ui 路由器动态查询字符串定义
- 使用可动态查询样式化列表
- 推特的提前输入示例/动态查询
- 在Mongodb + Mongoose.js中动态查询嵌套对象
- 在MongoDB中动态查询字段
- 如何在 where 子句中使用动态查询字段
- 使用firebase进行动态查询
- 动态查询字符串php
- 如何从动态查询结果传递选定的sql行id到javascript
- 使用Mongoose进行动态查询