Is there a command that i can use via javascript in mongo shell that can be used to check if the particular index exists in my mongodb. I am building a script file that would create indexes. I would like that if I run this file multiple number of times then the indexes that already exists are not recreated. I am thinking of using this, will it work:
if(db.collection.getIndexes().find({'name':'index_name'}).count()==0) {
db.collection.createIndex( { abc: 1, def: 1 } , { name: "index_name" } )
}
or, there is any better way to do this ? Also if above code is wrong, please help me with this task.