/** * Convert an array of objects to mongo DBObjects using the Jackson ObjectMapper for this * collection. * * @param objects The array of objects to convert * @return The array of resulting DBObjects in the same order as the received objects. * @throws MongoException */ public DBObject[] convertToDbObjects(T... objects) throws MongoException { DBObject[] results = new DBObject[objects.length]; for (int i = 0; i < objects.length; i++) { results[i] = convertToDbObject(objects[i]); } return results; }
/** * Convert an object into a DBObject using the Jackson ObjectMapper for this collection. * * @param object The object to convert * @return a mongo DBObject serialized with the ObjectMapper for this collection. * @throws MongoException */ public DBObject convertToDbObject(T object) throws MongoException { return JacksonDBCollection.convertToDbObject(object, isEnabled(Feature.USE_STREAM_SERIALIZATION), view, objectMapper); }
/** * Inserts an object into the database. if the objects _id is null, one will * be generated you can get the _id that was generated by calling * getSavedObject() or getSavedId() on the result * * @param object * The object to insert * @return The result * @throws MongoException * If an error occurred */ public WriteResult<T, K> insert(T object) throws MongoException { DBObject dbObject = convertToDbObject(object); return new WriteResult<T, K>(this, dbCollection.insert(dbObject), dbObject); }
/** * calls {@link DBCollection#update(com.mongodb.DBObject, com.mongodb.DBObject, boolean, boolean)} with upsert=false * and multi=false * * @param id * the id of the object to update * @param object * object with which to update <tt>query</tt> * @return The result * @throws MongoException * If an error occurred */ public WriteResult<T, K> updateById(K id, T object) throws MongoException { return update(createIdQuery(id), convertToDbObject(object), false, false); }
/** * Saves an object to this collection (does insert or update based on the * object _id). * * @param object * the <code>DBObject</code> to save * @param concern * the write concern * @return The result * @throws MongoException * If an error occurred */ public WriteResult<T, K> save(T object, WriteConcern concern) throws MongoException { DBObject dbObject = convertToDbObject(object); return new WriteResult<T, K>(this, dbCollection.save(dbObject, concern), dbObject); }
/** * Inserts an object into the database. if the objects _id is null, one will * be generated you can get the _id that was generated by calling * getSavedObject() or getSavedId() on the result * * @param object * The object to insert * @param concern * the write concern * @return The result * @throws MongoException * If an error occurred */ public WriteResult<T, K> insert(T object, WriteConcern concern) throws MongoException { DBObject dbObject = convertToDbObject(object); return new WriteResult<T, K>(this, dbCollection.insert(dbObject, concern), dbObject); }
@Override @SuppressWarnings("unchecked") public Task<Void> writeState(final RemoteReference<?> reference, final Object state) { DB db = mongoClient.getDB(database); final DBCollection col = db.getCollection(RemoteReference.getInterfaceClass(reference).getSimpleName()); JacksonDBCollection<Object, String> coll = JacksonDBCollection.wrap( col, (Class<Object>) state.getClass(), String.class, mapper); DBObject obj = coll.convertToDbObject(state); obj.put("_id", String.valueOf(RemoteReference.getId(reference))); col.save(obj); return Task.done(); }