mongodb - 具有自动文档计数的 Mongoose 字段
问题描述
我正在尝试创建一个 Mongoose 模式,其中包含一个按以下格式自动递增的字段:number-year
. 因此,例如,如果我今年(2018 年)创建了 3 个文档,他们应该在该字段中包含以下值:1-2018, 2-2018, 3-2018, ...
我正在使用该函数estimatedDocumentCount
来计算集合中的文档数。这是我的代码示例:
var db = mongoose.connection;
var FooSchema = new mongoose.Schema({
code: {
type: String,
index: true,
match: /\d{1,}-\d{4}/, // Code is in format nr-yyyy
default: function() {
return Foo.estimatedDocumentCount({})
.then(count => `${count}-${(new Date()).getFullYear()}`)
.catch(error => console.error(error))
}
}
});
var Foo = module.exports = mongoose.model('Foo', FooSchema);
不幸的是,我收到以下错误:
TypeError: Foo.estimatedDocumentCount is not a function
这是package.json
:
"mongodb": "^2.2.33",
"mongoose": "^4.13.2",
先感谢您。
解决方案
推荐阅读
- php - 具有样式并替换为随机字符串的精确 html 元素的正则表达式模式
- python - 求解耦合振荡器的运动方程
- react-native - 到达最后一个屏幕后对本机导航重置堆栈或选项卡做出反应
- gitlab - 如何在 GitLab CI 社区版中获取目标分支哈希?
- scala - 阿帕奇火花错误:
:28:错误:未找到:值 wcData - mysql - 如何从我的登录页面清除自动完成
- python - 如何修复 OpenCV 无法在我的 pycharm 中导入
- python - Python十六进制字符串到整数转换错误
- vb.net - 在 vb.net 中部署后 rdlc 无法正确打印
- django - 为什么我们需要用 gunicorn 部署 Django?