Vin*_*hta 3 javascript mongoose node.js
我正在尝试使用 mongoose 从 node.js 应用程序中的 mongoDB 集合中删除索引。我尝试使用,model.collection.dropIndex("username")但它给了我一个错误UnhandledPromiseRejectionWarning: MongoError: index not found with name [username]。
这是我的架构
var mongoose = require("mongoose"),
Schema = mongoose.Schema;
var userTable = new Schema({
firstname: { type: String, required: true },
lastname: { type: String, required: true },
username: { type: String },
salt: { type: String },
passwordHash: { type: String },
email: { type: String, unique: true, required: true },
sessionToken: { type: String },
dateCreated: { type: String, default: new Date().toString() },
loginHistory: [String]
});
module.exports = mongoose.model("userTable", userTable);
Run Code Online (Sandbox Code Playgroud)
当我使用 command 从终端在 mongo shell 中执行查询时db.usertable.find({}),我可以看到结果仍然有username字段。我也尝试username从架构文件中删除该字段,但即使这样也没有帮助。
提前致谢。
这会删除集合中除对象 id 之外的所有索引
db.collection.dropIndexs();
Run Code Online (Sandbox Code Playgroud)
删除某个索引
首先输入命令
db.collecction.getIndexes();
Run Code Online (Sandbox Code Playgroud)
您会看到类似上面的红色方块中的内容是索引名称。
db.collection.dropIndex( { "indexname": 1 } )
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5528 次 |
| 最近记录: |