Indexes
Import restaurants collection
wget https://www.dropbox.com/s/bfi06m1fvemzj69/primer-dataset-small.json
mongoimport --uri "mongodb+srv://admatic:admatic123@admatic-cluster-7qyyr.mongodb.net/admatic-db" \
--collection restaurants --drop --file primer-dataset-small.json
2019-02-25T16:23:06.163+0000 connected to: localhost
2019-02-25T16:23:06.323+0000 dropping: admatic-db.restaurants
2019-02-25T16:23:06.696+0000 imported 10 documents
Code
mkdir -p src/main/java/com/admatic/primer
vim src/main/java/com/admatic/primer/IndexesPrimer.java
package com.admatic.primer;
import com.mongodb.client.MongoClient;
import com.mongodb.client.MongoClients;
import com.mongodb.client.MongoDatabase;
import org.bson.Document;
import java.util.logging.Level;
import java.util.logging.Logger;
public class IndexesPrimer {
private static void singleFieldIndex(MongoDatabase db) {
db.getCollection("restaurants").createIndex(new Document("cuisine", 1));
}
private static void createCompoundIndex(MongoDatabase db) {
db.getCollection("restaurants").createIndex(new Document("cuisine", 1).append("address.zipcode", -1));
}
public static void main(final String[] args) {
Logger mongoLogger = Logger.getLogger("org.mongodb.driver");
mongoLogger.setLevel(Level.SEVERE);
MongoClient mongoClient;
if (args.length == 0) {
mongoClient = MongoClients.create();
} else {
mongoClient = MongoClients.create(args[0]);
}
MongoDatabase database = mongoClient.getDatabase("admatic-db");
switch (args[1]) {
case "0":
System.out.println("singleFieldIndex");
singleFieldIndex(database);
break;
case "1":
System.out.println("createCompoundIndex");
createCompoundIndex(database);
break;
default:
System.out.println("Enter an option between 0 and 1");
}
mongoClient.close();
}
}
Compile
mvn compile
Run
Single Field Index
db.getCollection("restaurants").createIndex(new Document("cuisine", 1))
mvn exec:java -Dexec.mainClass=com.admatic.primer.IndexesPrimer \
-Dexec.args="mongodb+srv://admatic:admatic123@admatic-cluster-7qyyr.mongodb.net/test 0"
singleFieldIndex
Create Compound Index
db.getCollection("restaurants").createIndex(new Document("cuisine", 1).append("address.zipcode", -1))
mvn exec:java -Dexec.mainClass=com.admatic.primer.IndexesPrimer \
-Dexec.args="mongodb+srv://admatic:admatic123@admatic-cluster-7qyyr.mongodb.net/test 1"
createCompoundIndex
Verify
mongo "mongodb+srv://admatic-cluster-7qyyr.mongodb.net/test" --username admatic
MongoDB shell version v4.0.6
Enter password:
Welcome to the MongoDB shell.
For interactive help, type "help".
For more comprehensive documentation, see
http://docs.mongodb.org/
Questions? Try the support group
http://groups.google.com/group/mongodb-user
MongoDB Enterprise Admatic-Cluster-shard-0:PRIMARY>
use admatic-db
switched to db admatic-db
db.restaurants.getIndexes()
[
{
"v": 2,
"key": {
"_id": 1
},
"name": "_id_",
"ns": "admatic-db.restaurants"
},
{
"v": 2,
"key": {
"cuisine": 1
},
"name": "cuisine_1",
"ns": "admatic-db.restaurants"
},
{
"v": 2,
"key": {
"cuisine": 1,
"address.zipcode": -1
},
"name": "cuisine_1_address.zipcode_-1",
"ns": "admatic-db.restaurants"
}
]