@Override public T save(T entity) { collection.save(entity); return entity; }
public U createUser(U user) { users.get().save(user); return user; }
public U updateUser(U user) { users.get().save(user); return user; }
public U createUser(U user) { users.get().save(user); return user; }
public U updateUser(U user) { users.get().save(user); return user; }
/** * Sets the collection. * * @param collection * the new collection */ @JsonIgnore public void setCollection(final MongoCollection collection) { this.collection = collection; // assuming this is called only once after creation, simply save the // entire state collection.save(this); collection.ensureIndex("{ _id: 1}"); collection.ensureIndex("{ _id: 1, timestamp:1 }"); }
@Override public void save(final T document) throws MongoDataException { try { getCollection().save(document); } catch (com.mongodb.DuplicateKeyException ex) { String msg = "Duplicate key for document " + document; log.error(msg, ex); throw new DuplicateKeyException(msg, ex); } catch (MongoException ex) { String msg = "Unable to save document " + document; log.error(msg, ex); throw new MongoDataException(msg, ex); } }
MongoClientUri uri = new MongoClientUri("mongodb://heroku_app1234:random_password@ds029017.mongolab.com:29017/heroku_app1234"); MongoClient client = new MongoClient(uri); Jongo jongo = new Jongo(client.getDB(uri.getDatabase())); MongoCollection collection = jongo.getCollection("myCollection"); collection.save(someBean); client.close();
private void doInsert(MongoCollection collection, String opQuery, Object[] parameters) throws DataServiceFault { if (opQuery != null) { if (parameters.length > 0) { if (opQuery.equals("#")) { collection.save(JSON.parse(parameters[0].toString())); } else { collection.insert(opQuery, parameters); } } else { collection.insert(opQuery); } } else { throw new DataServiceFault("Mongo insert statements must contain a query"); } }
@Override public int _updateByPrimaryKey(Table table) { try { if(table.getParams().containsKey("_id")){ DB db = database.getDB(); Jongo jongo = new Jongo(db); FindOne find = jongo.getCollection(table.getTableName()).findOne(new ObjectId(String.valueOf(table.getParams().get("_id")))); Map<String, Object> map = find.as(Map.class); boolean update = false; if (null != map) { Iterator<String> iter = table.getParams().keySet().iterator(); while (iter.hasNext()) { String key = iter.next(); if (map.containsKey(key) && !"_id".equals(key)) { map.put(key, table.getParams().get(key)); update = true; } } } if(update){ jongo.getCollection(table.getTableName()).save(map); } return 1; } } catch (MongoException e) { LOG.error("mongo update error", e); } return 0; }
Member joe = member("Joe", "123456", "Joe@example.com", "Adam", "Ben", "Josh"); int count = collection.save(joe).getN(); assertThat("Joe was saved", count, equalTo(1));
@Override public int _insert(Table table) { try { DB db = database.getDB(); Jongo jongo = new Jongo(db); ObjectId newOid = ObjectId.get(); table.getParams().put("_id", newOid); jongo.getCollection(table.getTableName()).save(table.getParams()); table.getParams().put("id", newOid.toString()); } catch (MongoException e) { LOG.error("mongo insert error", e); } return 1; }
public void setCredentials(String userRef, String passwordHash) { UserCredentials userCredentials = findCredentialsForUserRef(userRef); if (userCredentials == null) { userCredentials = new UserCredentials().setUserRef(userRef); } String hashed = credentialsStrategy.cryptCredentialsForStorage(userRef, passwordHash); usersCredentials.get().save( userCredentials .setPasswordHash(hashed) .setLastUpdated(DateTime.now())); }
jongo.getCollection(table.getTableName()).save(map);
public void setCredentials(String userRef, String passwordHash) { UserCredentials userCredentials = findCredentialsForUserRef(userRef); if (userCredentials == null) { userCredentials = new UserCredentials().setUserRef(userRef); } String hashed = credentialsStrategy.cryptCredentialsForStorage(userRef, passwordHash); usersCredentials.get().save( userCredentials .setPasswordHash(hashed) .setLastUpdated(DateTime.now())); }