在where子句中使用两个字段时出现猫鼬类型转换问题
Mongoose casting issue when using two fields in a where clause
我仍然试图围绕mongoDB/mongoose包装我的头,并没有能够锻炼出下面查询的问题;
Link
.find()
.where('active').equals(true)
.where('access_count').gt(0).lte('access_limit')
.limit(5)
.sort('-created')
.exec(function(err,latest)
返回以下强制转换错误;
CastError: Cast to number failed for value "access_limit" at path "access_count"
at SchemaNumber.cast
这是由.where('access_count').gt(0).lte('access_limit')引起的,我认为这是由于文档上两个字段的比较?是否有一种正确的方法来做这件事,以及对调试这些问题有什么建议?
作为参考,模式定义为;
var LinkSchema = new Schema({
token: {type: String, unique: true, index: true, required: true }
, title: {type: String}
, url: {type: String, required: true}
, active: {type: Boolean, default: true}
, created: {type: Date, default: Date.now}
, access_count: {type: Number, default: 0}
, access_expiry: Date
, access_limit: {type: Number, default: 0}
})
要在查询中将一个字段与另一个字段进行比较,必须使用$where
子句:
Link
.find()
.where('active').equals(true)
.where('access_count').gt(0)
.$where('this.access_count <= this.access_limit')
.limit(5)
.sort('-created')
.exec(function(err,latest)
$where
可能很慢,因此尽可能使用正常的where
子句来减少$where
需要执行的文档数量。
相关文章:
- 密码字段的问题
- JSP 中的“日期”字段存在问题
- 当出现多个字段时,jquery时间条目问题
- 我在添加更多输入字段时遇到问题,文本字段中的值不会保留
- Rails的问题,haml + javascript在字段对象的模糊
- Magento自定义选项-清除输入字段问题
- 获取javascript计算字段时遇到问题
- Accounts.createUser问题.如何使电子邮件字段不唯一
- HTML:密码字段中的HTML5占位符属性问题 - 显示正常文本
- 尝试使用名称访问表中字段的日期选取器时出现问题
- jquery动态添加HTML字段遇到了神秘的问题
- 添加到前端问题中的购物车和产品选项字段 - Magento
- 将状态设置为“输入名称”字段的值时遇到问题
- 当我尝试计算密码字段时,下面的代码有什么问题
- 我有一个问题,使用 Javascript 或 Jquery 创建具有字符串计数的动态字段
- 更新现有数据时检索引导模式表单中的复选框字段的值时出现问题
- jQuery 选择错误:添加新的选择字段并对其进行组合时出现问题
- asp.net MVC 隐藏字段 ID 问题
- x -可编辑字段问题,单引号
- jquery如果选择器更改重置或清除文本输入字段问题