admin管理员组文章数量:1393930
I'm trying to remove an index from my mongoDB collection in node.js application using mongoose. I tried using model.collection.dropIndex("username")
but it gives me an error UnhandledPromiseRejectionWarning: MongoError: index not found with name [username]
.
Here is my Schema
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);
When I perform the query in mongo shell from the terminal using mand db.usertable.find({})
, I can see that the results still have username
field. I also tried after removing the username
field from schema file, but even that didn't help.
Thanks in advance.
I'm trying to remove an index from my mongoDB collection in node.js application using mongoose. I tried using model.collection.dropIndex("username")
but it gives me an error UnhandledPromiseRejectionWarning: MongoError: index not found with name [username]
.
Here is my Schema
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);
When I perform the query in mongo shell from the terminal using mand db.usertable.find({})
, I can see that the results still have username
field. I also tried after removing the username
field from schema file, but even that didn't help.
Thanks in advance.
Share Improve this question asked May 13, 2019 at 11:10 Vinay MehtaVinay Mehta 1372 silver badges10 bronze badges2 Answers
Reset to default 6This drops all the indexes of the collection except for the object id
db.collection.dropIndexs();
to delete a certain index
first type the mand
db.collecction.getIndexes();
You will see something like above the in the red square is the index name .
db.collection.dropIndex( { "indexname": 1 } )
Creating an index using a unique name:
db.collection('users')
.createIndex(
{ email: 1 },
{ unique: true,
name: 'users.email.unique_index' }, // here we set index name
);
and then drop the index using it's name:
db.collection('users').dropIndex('users.email.unique_index');
本文标签: javascriptHow to drop index from mongodb schema using mongooseStack Overflow
版权声明:本文标题:javascript - How to drop index from mongodb schema using mongoose? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744080467a2587539.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论