Created
September 23, 2013 04:25
-
-
Save phpusr/6666452 to your computer and use it in GitHub Desktop.
Работа с MongoDB в Java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package mongotest; | |
import com.mongodb.*; | |
import java.net.UnknownHostException; | |
/** | |
* @author phpusr | |
* Date: 22.09.13 | |
* Time: 14:23 | |
*/ | |
/** | |
* Работа с MongoDB в Java | |
*/ | |
public class MongoJava { | |
private Mongo mongoHost; | |
private DB mongoDB; | |
public MongoJava(String dbName) throws UnknownHostException { | |
this.mongoHost = new Mongo("localhost"); | |
mongoDB = mongoHost.getDB(dbName); | |
// boolean auth = mongoDB.authenticate("", "".toCharArray()); | |
// System.out.println("auth: " + auth); | |
} | |
public static void main(String[] args) throws UnknownHostException { | |
MongoJava mongoJava = new MongoJava("phpusr"); | |
mongoJava.printDBs(); | |
mongoJava.printCollections(); | |
mongoJava.basicInsert("test"); | |
mongoJava.findOne("testData"); | |
mongoJava.insertMany("test", 100); | |
mongoJava.printColl("test"); | |
mongoJava.printBySimpleQuery("test"); | |
mongoJava.printByQuery("test", new BasicDBObject("i", new BasicDBObject("$ne", 15).append("$gt", 10))); //i != 15 & i > 10 | |
mongoJava.printByQuery("test", new BasicDBObject("i", new BasicDBObject("$gt", 50))); //i > 50 | |
mongoJava.printByQuery("test", new BasicDBObject("i", new BasicDBObject("$gt", 20).append("$lte", 30))); //20 < i <= 30 | |
mongoJava.createIndex("test", -1); | |
mongoJava.dropDB("phpusr"); | |
} | |
/** Вывод коллекций из БД */ | |
public void printCollections() { | |
System.out.println("\n>>Colls [" + mongoDB.getName() + "]"); | |
for (String coll : mongoDB.getCollectionNames()) { | |
System.out.println(coll); | |
} | |
} | |
/** Вывод БД на сервере */ | |
public void printDBs() { | |
System.out.println("\n>DBs"); | |
for (String db : mongoHost.getDatabaseNames()) { | |
System.out.println(db); | |
} | |
} | |
/** Получение коллекции по имени */ | |
public DBCollection getCollection(String collName) { | |
System.out.println(">>Coll"); | |
DBCollection coll = mongoDB.getCollection(collName); | |
System.out.println(coll); | |
return coll; | |
} | |
/** Простая вставка в коллекцию */ | |
public void basicInsert(String collName) { | |
System.out.println("\n>Basic insert"); | |
BasicDBObject docIn = new BasicDBObject("x", 203).append("y", 102); | |
BasicDBObject doc = new BasicDBObject("name", "MongoDB"). | |
append("type", "database"). | |
append("count", "1"). | |
append("info", docIn); | |
getCollection(collName).insert(doc); | |
} | |
/** Вытаскивание первой записи из коллекции */ | |
public void findOne(String collName) { | |
System.out.println("\n>Find one"); | |
DBObject doc = getCollection(collName).findOne(); | |
System.out.println(doc); | |
} | |
/** Вставка большого кол-ва записей в коллекцию */ | |
public void insertMany(String collName, int count) { | |
DBCollection coll = getCollection(collName); | |
for (int i=0; i< count; i++) { | |
coll.insert(new BasicDBObject("i", i)); | |
} | |
} | |
/** Вывод записей по запросу - query */ | |
public void printByQuery(String collName, DBObject query) { | |
System.out.println("\n>>Print by Query"); | |
DBCollection coll = getCollection(collName); | |
System.out.println(">>>Coll count: " + coll.getCount(query)); | |
DBCursor cursor = coll.find(query); | |
try { | |
while (cursor.hasNext()) { | |
System.out.println(cursor.next()); | |
} | |
} finally { | |
cursor.close(); | |
} | |
} | |
/** Вывод коллекции */ | |
public void printColl(String collName) { | |
System.out.println("\n>Print coll"); | |
printByQuery(collName, null); | |
} | |
/** Вывод записей по простому запросу */ | |
public void printBySimpleQuery(String collName) { | |
System.out.println("\n>Print by simple Query"); | |
BasicDBObject query = new BasicDBObject("i", 71); | |
printByQuery("test", query); | |
} | |
/** Создать индекс по атрибуту */ | |
public void createIndex(String collName, int index) { | |
System.out.println("\n>Create index"); | |
DBCollection coll = getCollection(collName); | |
coll.createIndex(new BasicDBObject("i", index)); | |
for (DBObject object : coll.getIndexInfo()) { | |
System.out.println(object); | |
} | |
} | |
/** Удалить БД по имени */ | |
public void dropDB(String dbName) { | |
mongoHost.dropDatabase(dbName); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment