List of usage examples for com.mongodb DBCollection insert
public WriteResult insert(final List<? extends DBObject> documents)
From source file:net.handle.server.MongoDBHandleStorage.java
License:Open Source License
/** * ****************************************************************** * Creates the specified handle in the "database" with the specified * initial values/*from ww w .j a v a2 s. c o m*/ * <p/> * The document looks like this: * <p/> * handle:handle here: na\identifier * handles:[ * { * index: * type: * data: * ttl_type: * ttl: * timestamp: * refs: * admin_read: * admin_write: * pub_read: * pub_write: * } * , * { * index: * type: * data: * ttl_type: * ttl: * timestamp: * refs: * admin_read: * admin_write: * pub_read: * pub_write: * } * ] * ******************************************************************* */ public void createHandle(byte handle[], HandleValue values[]) throws HandleException { if (readOnly) throw new HandleException(HandleException.STORAGE_RDONLY, "Server is read-only"); final String handleStr = Util.decodeString(handle); // if the handle already exists, throw an exception if (handleExists(handle)) throw new HandleException(HandleException.HANDLE_ALREADY_EXISTS, handleStr); if (values == null) throw new HandleException(HandleException.INVALID_VALUE); BasicDBList handles = new BasicDBList(); for (HandleValue value : values) { // not the handle, // but index, type, data, ttl_type, ttl, timestamp, refs, // admin_read, admin_write, pub_read, pub_write BasicDBObject hv = setHandleValue(value); handles.add(hv); } BasicDBObject h = new BasicDBObject("handle", handleStr); h.put("handles", handles); final DBCollection collection = getCollection(handle); collection.insert(h); }
From source file:net.ion.framework.db.mongo.jdbc.Executor.java
License:Apache License
private int insert(Insert in) throws MongoSQLException { if (in.getColumns() == null) throw new MongoSQLException.BadSQL("have to give column names to insert"); DBCollection coll = getCollection(in.getTable()); if (!(in.getItemsList() instanceof ExpressionList)) throw new UnsupportedOperationException("need ExpressionList"); BasicDBObject o = new BasicDBObject(); List valueList = ((ExpressionList) in.getItemsList()).getExpressions(); if (in.getColumns().size() != valueList.size()) throw new MongoSQLException.BadSQL("number of values and columns have to match"); for (int i = 0; i < valueList.size(); i++) { o.put(in.getColumns().get(i).toString(), toConstant((Expression) valueList.get(i))); }/*from w w w. ja va 2s . c o m*/ coll.insert(o); return 1; // TODO - this is wrong }
From source file:net.jurre.edutil.persistence.MongoDB.java
License:Open Source License
public void saveEDDNData(String data) { BasicDBObject dataObj = (BasicDBObject) JSON.parse(data); String schema = dataObj.getString("$schemaRef"); dataObj.removeField("$schemaRef"); dataObj.append("schemaRef", schema); if (dataObj.getString("schemaRef").equalsIgnoreCase("http://schemas.elite-markets.net/eddn/commodity/2")) { BasicDBObject message = (BasicDBObject) dataObj.get("message"); boolean stationupdated = updateStationMarketData(message.getString("stationName"), message.getString("systemName"), message.get("commodities")); if (stationupdated) dataObj.append("stationUpdated", true); }/*from w ww . j a va2s . c o m*/ DBCollection collection = this.db.getCollection(MongoDB.EDDN_COLLECTION); collection.insert(dataObj); }
From source file:net.luminis.useradmin.mongodb.MongoDBRepositoryStore.java
License:Apache License
@Override public boolean addRole(Role role) throws IOException { if (role == null) { throw new IllegalArgumentException("Role cannot be null!"); }//ww w.ja v a 2s .c o m try { DBCollection coll = getCollection(); DBCursor cursor = coll.find(getTemplateObject(role)); try { if (cursor.hasNext()) { // Role already exists... return false; } } finally { cursor.close(); } // Role does not exist; insert it... DBObject data = m_helper.serialize(role); WriteResult result = coll.insert(data); return (result.getLastError() != null) && result.getLastError().ok(); } catch (MongoException e) { m_log.log(LogService.LOG_WARNING, "Add role failed!", e); throw new IOException("AddRole failed!", e); } }
From source file:net.onrc.openvirtex.db.DBManager.java
License:Apache License
/** * Create document in db from persistable object obj *//*from w w w . j a v a 2 s . co m*/ public void createDoc(Persistable obj) { // Suppress error stream when MongoDB raises java.net.ConnectException in another component (and cannot be caught) PrintStream ps = System.err; System.setErr(null); try { DBCollection collection = this.collections.get(obj.getDBName()); collection.insert(new BasicDBObject(obj.getDBObject())); } catch (Exception e) { // Do not log when duplicate key // Virtual network was already stored and we're trying to create it again on startup if (e instanceof MongoException.DuplicateKey) log.warn("Skipped saving of virtual network with duplicate tenant id"); else log.error("Failed to insert document into database: {}", e.getMessage()); } finally { // Restore error stream System.setErr(ps); } }
From source file:net.scran24.datastore.mongodb.MongoDbDataStore.java
License:Apache License
@Override public void addUser(String survey_id, SecureUserRecord user) throws DataStoreException, DuplicateKeyException { DBCollection col = getUsersCollection(survey_id); BasicDBList roles = new BasicDBList(); roles.addAll(user.roles);//from w w w. j av a 2 s. c o m BasicDBList permissions = new BasicDBList(); permissions.addAll(user.permissions); BasicDBObject customFields = new BasicDBObject(); for (String k : user.customFields.keySet()) customFields.put(k, user.customFields.get(k)); try { col.insert( new BasicDBObject(FIELD_USERNAME, user.username).append(FIELD_PASSWORD, user.passwordHashBase64) .append(FIELD_SALT, user.passwordSaltBase64).append(FIELD_ROLES, roles) .append(FIELD_PERMISSIONS, permissions).append(FIELD_USERDATA, customFields)); } catch (DuplicateKey e) { throw new DuplicateKeyException(e); } catch (MongoException e) { throw new DataStoreException(e); } }
From source file:net.scran24.datastore.mongodb.MongoDbDataStore.java
License:Apache License
@Override public void incrementPopularityCount(List<String> foodCodes) throws DataStoreException { DBCollection col = getPopularityCollection(); Set<String> missing = new HashSet<String>(); missing.addAll(foodCodes);//from ww w .j av a 2 s . c o m // FIXME // This query is incorrect: foods may appear multiple times if // foodCodes, // but the counter will only be incremented once, although this is // efficient. // Change this to do that correctly without generating a request for // each // code. DBObject query = new BasicDBObject(FIELD_CODE, new BasicDBObject("$in", foodCodes)); try { DBCursor cursor = col.find(query); for (DBObject o : cursor) missing.remove(o.get(FIELD_CODE)); List<DBObject> newRecords = new ArrayList<DBObject>(); for (String code : missing) newRecords.add(new BasicDBObject(FIELD_CODE, code).append(FIELD_COUNT, 0)); col.insert(newRecords); col.update(query, new BasicDBObject("$inc", new BasicDBObject(FIELD_COUNT, 1)), false, true); } catch (MongoException e) { throw new DataStoreException(e); } }
From source file:net.scran24.datastore.mongodb.MongoDbDataStore.java
License:Apache License
@Override public void saveSurvey(String survey_id, String username, NutritionMappedSurveyRecord survey) throws DataStoreException { DBCollection col = getSurveysCollection(survey_id); try {//w ww . j a v a2 s. co m col.insert(serialiser.serialize(survey, username)); } catch (MongoException e) { throw new DataStoreException(e); } }
From source file:net.scran24.datastore.mongodb.MongoDbDataStore.java
License:Apache License
@Override public void saveMissingFoods(List<MissingFoodRecord> missingFoods) throws DataStoreException { DBCollection col = getMissingFoodsCollection(); try {//from ww w . j a v a2 s. com col.insert(serialiser.missingFoodsAsDBObject(missingFoods)); } catch (MongoException e) { throw new DataStoreException(e); } }
From source file:net.sf.okapi.lib.tmdb.mongodb.Repository.java
License:Open Source License
public Repository(String connStr) { String host = "localhost"; //--parse the url-- String[] params = connStr.split("/"); if (params.length == 1) { name = params[0];/* w w w. j a va 2 s .c o m*/ } else if (params.length > 1) { host = params[0]; name = params[1]; } //--verify-- if (host == null || host.trim().length() == 0) { return; } if (name == null || name.trim().length() == 0) { return; } try { connection = new Mongo(host); repository = connection.getDB(name); DBCollection repo = repository.getCollection(Repository.REPO_COLL); BasicDBObject query = new BasicDBObject(); query.put("name", name); DBObject dbObj = repo.findOne(query); if (dbObj == null) { BasicDBObject doc = new BasicDBObject(); doc.put("name", name); doc.put("description", "Default Description"); repo.insert(doc); //TODO: Unless description is not used this "REPO" table is not needed } } catch (UnknownHostException e) { throw new RuntimeException(e); } catch (MongoException e) { throw new RuntimeException(e); } }