Computing Magazine

How to Create Index With MongoDB 3 Java Driver

Posted on the 19 May 2015 by Abhishek Somani @somaniabhi
Sample Java Program to Create Index in MongoDB 3 Java Driver . This snipped also contains older code which was used to create index . You just need to create a BasicDBObject with key as a name on which you want to create a index , and value will be 1 or -1 . if you create it with 1 then it will create a ascending index , and if you create with -1 , then it will create descending index .

package mongoDBExample;
import java.util.Arrays;
import java.util.List;
import org.bson.Document;
import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBObject;
import com.mongodb.MongoClient;
import com.mongodb.MongoClientURI;
import com.mongodb.client.ListIndexesIterable;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoCursor;
import com.mongodb.client.MongoDatabase;
public class MongoDBTest
{
public static void main(String[] args)
{
MongoClient mongoClient = new MongoClient(new MongoClientURI("mongodb://localhost:27017"));

/*MongoDatabase database = mongoClient.getDatabase("Test");
List books = Arrays.asList(1251,2512);
Document person = new Document("_id", "ABHSIHEK")
.append("name", "java roots")
.append("address", new BasicDBObject("street", "sad java")
.append("city", "mongos")
.append("state", "mongod")
.append("zip", 5151))
.append("books", books);

MongoCollection collection= database.getCollection("people");
collection.insertOne(person);*/

MongoDatabase database = mongoClient.getDatabase("Test");
MongoCollection collection= database.getCollection("people");
//create index on name field
//use 1 for ascending index , -1 for descending index
BasicDBObject index = new BasicDBObject("name",1);

collection.createIndex(index);
System.out.println(" index created successfully ");
ListIndexesIterable indexes = collection.listIndexes(BasicDBObject.class);
MongoCursor cursor = indexes.iterator();
while(cursor.hasNext())
{
BasicDBObject getCreatedIdx = cursor.next();
System.out.println("index is : "+ getCreatedIdx.toString());
}



mongoClient.close();
/**
* Older code which is deprecated in 3.0
*/
/*
DB db = mongoClient.getDB("Test");
List books = Arrays.asList(27464, 747854);
DBObject obj = new BasicDBObject("_id", "javaroots").append("name", "java roots")
.append("address", new BasicDBObject("street", "awesome java")
.append("city", "mongos")
.append("state", "mongod")
.append("zip", 5151))
.append("books", books);;

DBCollection col = db.getCollection("people");
col.insert(obj);
col.createIndex("name");*/


}
}
Post Comments And Suggestions !!

You Might Also Like :

Back to Featured Articles on Logo Paperblog

These articles might interest you :

  • The Verdi Project: Rigoletto

    Verdi Project: Rigoletto

    In which our composer creates a sensation and changes the world of opera, forever. by Paul J. Pelkonen Tito Gobbi (with Renata Scotto) looking suitably... Read more

    The 23 April 2018 by   Superconductor
    CULTURE, THEATRE & OPERA
  • the Ship is Sinking

    Mishpacha Magazine (Hebrew edition) has been running an initiative recently promoting new young couples in the Haredi community take on their own mortgages... Read more

    The 23 April 2018 by   Gldmeier
    RELIGION, SOCIETY, SPIRITUALITY
  • Flower Power: Beyond the Beauty

    If we could see the miracle of a single flower clearly, our whole life would change.” – Lord Buddha. Life is a journey that is full of special moments; like... Read more

    The 23 April 2018 by   Meet Rv
  • Dress To Impress! 5 Dresses For Your First Date

    Dress Impress! Dresses Your First Date

    Are you looking to make a great first impression on a new man when you show up for your first date with him? Are you conflicted as to how to dress, what... Read more

    The 23 April 2018 by   Meet Rv
  • A RAINY DAY in VOLCANOES NATIONAL PARK, HAWAII

    RAINY VOLCANOES NATIONAL PARK, HAWAII

    In the Kilauea Iki Crater in Volcanoes National Park, HawaiiA week ago, on our trip to the Big Island of Hawaii, Art and I were hiking across what felt like a... Read more

    The 23 April 2018 by   Carolinearnoldtravel
    TRAVEL
  • Congress Has Grilled Facebook CEO with Tough Questions Over Data Misuse

    Congress Grilled Facebook with Tough Questions Over Data Misuse

    Mark Zuckerberg has faced Serious Allegations on Facebook Data breaching Scandal in two days Congress testimony –as it happened. Read more

    The 23 April 2018 by   Ruby Mariah
    GADGETS, TECH
  • Video of the Week: Anatomy of the Toes

    Video Week: Anatomy Toes

    In this lesson, we will look at the anatomy of the toes. I will also discuss how your toes move, what their functions are, and which kinds of common problems... Read more

    The 23 April 2018 by   Ninazolotow
    FITNESS, HEALTH, HEALTHY LIVING

Magazines