public static void main(String[] args) throws Exception {
Mongo m = new Mongo("10.18.96.160", 27017);
System.out.println(m.getDatabaseNames());
DB db = m.getDB("test");
System.out.println(db.getCollectionNames());
DBCollection collection = db.getCollection("foo1");
BasicDBObject info = new BasicDBObject();
info.put("name", "test");
BasicDBObject emails = new BasicDBObject();
emails.put("email_1", "test@test.cn");
emails.put("email_2", "test@test.com");
info.put("emails", emails);
collection.insert(info);
System.out.println(collection.findOne());
BasicDBObject condition = new BasicDBObject();
condition.put("email_1", "test@test.cn");
System.out.println(collection.find(condition));
}
Java访问MongoDB例子
点赞
收藏