根据这篇mongodb文章,可以自动增加一个字段,我想使用计数器收集方式.
这个例子的问题是我没有成千上万的人使用mongo控制台在数据库中键入数据.相反,我试图使用猫鼬.
所以我的架构看起来像这样:
var entitySchema = mongoose.Schema({
testvalue:{type:String,default:function getNextSequence() {
console.log('what is this:',mongoose);//this is mongoose
var ret = db.counters.findAndModify({
query: { _id:'entityId' },
update: { $inc: { seq: 1 } },
new: true
}
);
return ret.seq;
}
}
});
Run Code Online (Sandbox Code Playgroud)
我在同一个数据库中创建了计数器集合,并添加了一个_id为'entityId'的页面.从这里我不知道如何使用mongoose更新该页面并获得递增的数字.
计数器没有架构,我希望它保持这种方式,因为这实际上不是应用程序使用的实体.它只应在模式中用于自动增量字段.
我正在使用 Nest.js 并尝试使用包含子文档字段数组的装饰器创建一个架构。
\n在导入/导出架构并将其转换为模型方面,我没有任何麻烦,直到:
\n\n我的文件中收到以下错误service。
经过几个小时的谷歌搜索,我发现真正的原因是在array子文档字段背后,而且只有它们。一旦我移除该members字段。架构和模型就可以了。并且不必对以下描述的解决方案或与 相关的任何其他答案extends Mongoose.Document执行任何操作。(如果你已经这样做了)
我发现大多数情况与子文档相关,但与数组子文档无关。我想问:
\n并删除此错误:
\nS2344: Type \'Guild\' does not satisfy the constraint \'Document<any, {}>\'. \xc2\xa0\xc2\xa0\n The types returned by \'delete(...).$where(...).cast(...)\' are incompatible between these types.\n Type \'UpdateQuery<Guild>\' is not assignable to type \'UpdateQuery<Document<any, {}>>\'.\n Type \'UpdateQuery<Guild>\' is not assignable to type \'_UpdateQuery<_AllowStringsForIds<LeanDocument<Document<any, {}>>>>\'.\nTypes of …Run Code Online (Sandbox Code Playgroud)