在删除数据库中的所有内容之后,我想重新创建数据库中的模型。
猫鼬(或者蒙戈本身)实际上是重新创建文档,而不是索引。那么,是否有一种方法来重置“猫鼬”,使其能够重新创建索引,就像第一次运行一样?
我之所以使用dropDatabase,是因为它在测试时似乎更容易。否则,我将不得不逐个移除所有的集合。
发布于 2013-09-22 10:41:00
虽然不推荐用于生产,但取决于您的方案,您可以将index属性添加到字段定义中,以指定要创建的索引:
var animalSchema = new Schema({
name: String,
type: String,
tags: { type: [String], index: true } // field level
});
animalSchema.index({ name: 1, type: -1 }); // schema level或,
var s = new Schema({ name: { type: String, sparse: true })
Schema.path('name').index({ sparse: true });或者,您可以在ensureIndex上调用Model (文档):
Animal.ensureIndexes(function (err) {
if (err) return handleError(err);
});https://stackoverflow.com/questions/18941181
复制相似问题