Différences
Ci-dessous, les différences entre deux révisions de la page.
| Les deux révisions précédentes Révision précédente Prochaine révision | Révision précédente | ||
| java:mongodb [2018/04/16 08:28] – jcheron | java:mongodb [2019/08/31 14:21] (Version actuelle) – modification externe 127.0.0.1 | ||
|---|---|---|---|
| Ligne 1: | Ligne 1: | ||
| ====== MongoDb ====== | ====== MongoDb ====== | ||
| - | ===== Accès direct | + | ===== Librairies |
| + | |||
| + | * [[https:// | ||
| + | * [[https:// | ||
| + | |||
| + | |||
| + | ===== Sérialisation/ | ||
| + | Gson va permettre de sérialiser et désérialiser pour passer d' | ||
| <sxh java; | <sxh java; | ||
| Ligne 34: | Ligne 41: | ||
| } | } | ||
| </ | </ | ||
| + | |||
| + | |||
| + | ==== Gestion de l' | ||
| + | |||
| + | |||
| + | <sxh java; | ||
| + | public class ObjectIdAdapter | ||
| + | implements JsonSerializer< | ||
| + | |||
| + | @Override | ||
| + | public JsonElement serialize(ObjectId id, Type typeOfT, JsonSerializationContext context) { | ||
| + | JsonObject jo = new JsonObject(); | ||
| + | jo.addProperty(" | ||
| + | return jo; | ||
| + | } | ||
| + | |||
| + | @Override | ||
| + | public ObjectId deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { | ||
| + | try { | ||
| + | return new ObjectId(json.getAsJsonObject().get(" | ||
| + | } catch (Exception e) { | ||
| + | return null; | ||
| + | } | ||
| + | } | ||
| + | |||
| + | } | ||
| + | </ | ||
| + | |||
| + | ===== Opérations CRUD ===== | ||
| + | |||
| + | Classe à intégrer, à documenter et à tester (Tests unitaires) | ||
| + | |||
| + | <sxh java> | ||
| + | public class MyMongo { | ||
| + | protected DB db; | ||
| + | protected MongoClient mongoClient; | ||
| + | private DBCollection collection; | ||
| + | |||
| + | public DB getDb() { | ||
| + | return db; | ||
| + | } | ||
| + | |||
| + | public boolean connect(String dbname) throws UnknownHostException { | ||
| + | return this.connect(dbname, | ||
| + | } | ||
| + | |||
| + | public boolean connect(String dbname, String server, int port) throws UnknownHostException { | ||
| + | mongoClient = new MongoClient(server, | ||
| + | List< | ||
| + | db = mongoClient.getDB(dbname); | ||
| + | return dbnames.contains(dbname); | ||
| + | } | ||
| + | |||
| + | public DBCollection getCollection(String name) { | ||
| + | return db.getCollection(name); | ||
| + | } | ||
| + | |||
| + | public WriteResult insert(String collectionName, | ||
| + | setCollection(collectionName); | ||
| + | return insert(object); | ||
| + | } | ||
| + | |||
| + | public WriteResult insert(Object object) { | ||
| + | return collection.insert(DBOAdapter.objectToDBObject(object)); | ||
| + | } | ||
| + | |||
| + | public DBObject findOne() { | ||
| + | return collection.findOne(); | ||
| + | } | ||
| + | |||
| + | public DBObject findOne(String collectionName) { | ||
| + | setCollection(collectionName); | ||
| + | return findOne(); | ||
| + | } | ||
| + | |||
| + | public DBObject findOne(BasicDBObject query) { | ||
| + | return collection.findOne(query); | ||
| + | } | ||
| + | |||
| + | public DBObject findOne(String collectionName, | ||
| + | setCollection(collectionName); | ||
| + | return findOne(query); | ||
| + | } | ||
| + | |||
| + | /** | ||
| + | * Retourne tous les documents de la collection | ||
| + | * | ||
| + | * @param collectionName | ||
| + | * @return | ||
| + | */ | ||
| + | public Cursor find(String collectionName) { | ||
| + | setCollection(collectionName); | ||
| + | return find(); | ||
| + | } | ||
| + | |||
| + | public Cursor find() { | ||
| + | return collection.find(); | ||
| + | } | ||
| + | |||
| + | public WriteResult insert(String collectionName, | ||
| + | setCollection(collectionName); | ||
| + | BasicDBObject[] dbList = new BasicDBObject[objects.size()]; | ||
| + | int i = 0; | ||
| + | for (Model m : objects) { | ||
| + | dbList[i++] = DBOAdapter.objectToDBObject(m); | ||
| + | } | ||
| + | return collection.insert(dbList); | ||
| + | } | ||
| + | |||
| + | public void save(String collectionName, | ||
| + | setCollection(collectionName); | ||
| + | for (Model m : objects) { | ||
| + | collection.save(DBOAdapter.objectToDBObject(m)); | ||
| + | } | ||
| + | } | ||
| + | |||
| + | public Cursor find(String collectionName, | ||
| + | setCollection(collectionName); | ||
| + | return find(query); | ||
| + | } | ||
| + | |||
| + | public <T extends Model> List< | ||
| + | setCollection(clazz.getSimpleName()); | ||
| + | List< | ||
| + | while (cursor.hasNext()) { | ||
| + | result.add(DBOAdapter.dboToModel(cursor.next(), | ||
| + | } | ||
| + | return result; | ||
| + | } | ||
| + | |||
| + | public <T extends Model> List< | ||
| + | return load(find(query), | ||
| + | } | ||
| + | |||
| + | public <T extends Model> List< | ||
| + | return load(find(clazz.getSimpleName()), | ||
| + | } | ||
| + | |||
| + | public Cursor find(BasicDBObject query) { | ||
| + | return collection.find(query); | ||
| + | } | ||
| + | |||
| + | public void setCollection(String name) { | ||
| + | collection = db.getCollection(name); | ||
| + | } | ||
| + | |||
| + | public List< | ||
| + | return mongoClient.getDatabaseNames(); | ||
| + | } | ||
| + | |||
| + | public void close() { | ||
| + | mongoClient.close(); | ||
| + | } | ||
| + | |||
| + | public void dropCollection(String name) { | ||
| + | this.setCollection(name); | ||
| + | collection.drop(); | ||
| + | } | ||
| + | |||
| + | public void dropCollection(Class<? | ||
| + | this.dropCollection(clazz.getSimpleName()); | ||
| + | } | ||
| + | |||
| + | @SuppressWarnings({ " | ||
| + | public void dropCollections(Class... classes) { | ||
| + | for (Class clazz : classes) { | ||
| + | dropCollection(clazz); | ||
| + | } | ||
| + | } | ||
| + | |||
| + | } | ||
| + | </ | ||
| + | |||