mongodb创建索引(java)
解决方法:
String collectionName = "collectionName";
List<BasicDBObject> bsons = new ArrayList<BasicDBObject>();
bsons.add(new BasicDBObject().append("account", 1));//1升序,-1降序
bsons.add(new BasicDBObject().append("ac_id", 1));//1升序,-1降序
MongoCollection<Document> collection = getDb().getCollection(collectionName);
for(BasicDBObject bson:bsons){
collection.createIndex(bson);
}
其中mongodb链接getDb()请参考:http://www.yayihouse.com/yayishuwu/chapter/1420