Rule Definition
When using compound indexes, avoid having different indexes. Indeed, Mongo DB give the compound index, when we have index on more than two column, it is always good to have the indexes put the same way,it is highly recommended to have the indexes, either both positives(ascending) or either both negatives(descending).
Remediation
Use instead indexes, either both positives(ascending) or either both negatives(descending)
Violation Code Sample
MongoClient conn_str = new MongoClient("localhost", 27017);
DB db = conn_str.getDB("database");
DBCollection docColl = db.getCollection("document");
docColl.createIndex(new BasicDBObject("filed_1",1),
new BasicDBObject("_id", -1),
new BasicDBObject("filed_2", -1));
Fixed Code Sample
MongoClient conn_str = new MongoClient("localhost", 27017);
DB db = conn_str.getDB("database");
DBCollection docColl = db.getCollection("document");
docColl.createIndex(new BasicDBObject("filed_1",1),
new BasicDBObject("_id", 1),
new BasicDBObject("filed_2", 1));
Related Technologies
Technical Criterion
Programming Practices - Structuredness
About CAST Appmarq
CAST Appmarq is by far the biggest repository of data about real IT systems. It's built on thousands of analyzed applications, made of 35 different technologies, by over 300 business organizations across major verticals. It provides IT Leaders with factual key analytics to let them know if their applications are on track.