List of usage examples for com.mongodb DBCollection insert
public WriteResult insert(final List<? extends DBObject> documents)
From source file:org.graylog2.database.MongoBridge.java
License:Open Source License
/** * Inserts a GELF message into the messages collection. * * @param message The GELF message//from w w w .jav a 2 s . co m * @throws Exception */ public void insertGelfMessage(GELFMessage message) throws Exception { // Check if all required parameters are set. if (!message.allRequiredFieldsSet()) { throw new Exception("Missing GELF message parameters. version, short_message and host are required."); } DBCollection coll = MongoConnection.getInstance().getMessagesColl(); BasicDBObject dbObj = new BasicDBObject(); // Some fields must not be set if this message was converted from a syslog message. if (!message.convertedFromSyslog()) { dbObj.put("gelf", true); dbObj.put("version", message.getVersion()); } dbObj.put("message", message.getShortMessage()); dbObj.put("full_message", message.getFullMessage()); dbObj.put("file", message.getFile()); dbObj.put("line", message.getLine()); dbObj.put("host", message.getHost()); dbObj.put("facility", message.getFacility()); dbObj.put("level", message.getLevel()); dbObj.put("timestamp", message.getTimestamp()); // Add additional fields. XXX PERFORMANCE Map<String, String> additionalFields = message.getAdditionalData(); Set<String> set = additionalFields.keySet(); Iterator<String> iter = set.iterator(); while (iter.hasNext()) { String key = iter.next(); String value = additionalFields.get(key); dbObj.put(key, value); } dbObj.put("created_at", Tools.getUTCTimestamp()); // Documents in capped collections cannot grow so we have to do that now and cannot just add 'deleted => true' later. dbObj.put("deleted", false); dbObj.put("streams", message.getStreamIds()); coll.insert(dbObj); }
From source file:org.graylog2.database.MongoBridge.java
License:Open Source License
public void writeHistoricServerValue(String key, Object value) { BasicDBObject obj = new BasicDBObject(); obj.put("type", key); obj.put("value", value); obj.put("created_at", Tools.getUTCTimestamp()); DBCollection coll = MongoConnection.getInstance().getHistoricServerValuesColl(); coll.insert(obj); }
From source file:org.graylog2.ServerValue.java
License:Open Source License
private DBObject findOrCreateMongoInstance(String instanceId) { DBCollection sv = MongoConnection.getInstance().getDatabase().getCollection("server_values"); // Doing it this way because java driver sucks in querying for subdoc keys. :( DBCursor cur = sv.find(); // find all while (cur.hasNext()) { DBObject obj = cur.next();/*from w w w . j av a 2s . com*/ if (obj.containsField(instanceId)) { // Found the instance. return obj; } } // There is no such instance in the collection yet if we arrive here. DBObject instance = new BasicDBObject(); instance.put(instanceId, null); sv.insert(instance); return instance; }
From source file:org.greenmongoquery.db.service.impl.MongoServiceImpl.java
License:Open Source License
@Override public String insertDocument(String json, String dbname, String collectionName, Mongo mongo) { String result = null;// ww w. j ava2 s . c om DB db = mongo.getDB(dbname); DBCollection coll = db.getCollection(collectionName); DBObject dbObject = (DBObject) JSON.parse(json); coll.insert(dbObject); logger.info("insert object " + dbObject.toString()); result = "insert document : _id= " + dbObject.get("_id").toString(); return result; }
From source file:org.gsafeproject.audit.dao.EventDao.java
License:Open Source License
public Event[] save(Event... events) { if (events == null || events.length <= 0) { return events; }// w w w .j a v a2 s.c o m for (Event event : events) { if (event == null) { continue; } DBCollection coll = db.getCollection(COLLECTION_NAME); BasicDBObject doc = new BasicDBObject("action", event.getAction())// .append("actor", event.getActor())// .append("timestamp", event.getDate())// .append("resource", event.getResource())// .append("description", event.getDescription())// .append("metadata", event.getMetadata()); coll.insert(doc); ObjectId id = (ObjectId) doc.get("_id"); event.setId(id.toString()); } return events; }
From source file:org.hibernate.ogm.dialect.mongodb.MongoDBDialect.java
License:Open Source License
@Override public Association createAssociation(AssociationKey key) { DBCollection associations = getAssociationCollection(key); DBObject assoc = MongoHelpers.associationKeyToObject(key); assoc.put(ROWS_FIELDNAME, Collections.EMPTY_LIST); associations.insert(assoc); return new Association(new MongoDBAssociationSnapshot(assoc)); }
From source file:org.i3xx.step.clockmongo.service.impl.ClockPersistenceServiceImpl.java
License:Apache License
/** * @param col The collection to get the object from * @param symbol The symbol (key)//from w w w . j a va2s.c o m * @param timeout The timeout or -1 for no timeout * @return The DBObject */ private DBObject ensureObject(DBCollection col, String symbol) { DBObject dbo = null; DBObject query = new BasicDBObject("symbol", symbol); dbo = col.findOne(query); if (dbo == null) { String nspc = col.getName(); dbo = new BasicDBObject(); dbo.put("nspc", nspc); dbo.put("symbol", symbol); dbo.put("born", new Long(System.currentTimeMillis())); dbo.put("statement", ""); col.insert(dbo); } return dbo; }
From source file:org.ingini.mongodb.jongo.example.util.CollectionManager.java
License:Apache License
private static void fill(DBCollection collection, String collectionContentFilePath) { StringBuilder stringBuilder = new StringBuilder(); try {/*from ww w . ja v a2 s . c om*/ InputStreamReader inputStreamReader = new InputStreamReader( // CollectionManager.class.getClassLoader().getResourceAsStream(collectionContentFilePath), "UTF-8"); CharBuffer buf = CharBuffer.allocate(BUFFER_SIZE); for (int read = inputStreamReader.read(buf); read != EOF; read = inputStreamReader.read(buf)) { buf.flip(); stringBuilder.append(buf, START, read); } } catch (IOException e) { System.out.println("Unable to read input stream due to an exception! Exception: " + ExceptionUtils.getStackTrace(e)); throw new IllegalStateException(e); } BasicDBList parse = (BasicDBList) JSON.parse(stringBuilder.toString(), new MongoIdTransformerJSONCallback()); collection.insert(parse.toArray(new DBObject[parse.size()])); }
From source file:org.ingini.monogo.testbed.MongoManager.java
License:Apache License
private void fill(DBCollection collection, String collectionContentFilePath) { StringBuilder stringBuilder = new StringBuilder(); try {//from w ww . j a v a2s . com InputStreamReader inputStreamReader = new InputStreamReader( // CollectionManager.class.getClassLoader().getResourceAsStream(collectionContentFilePath), "UTF-8"); CharBuffer buf = CharBuffer.allocate(BUFFER_SIZE); for (int read = inputStreamReader.read(buf); read != EOF; read = inputStreamReader.read(buf)) { buf.flip(); stringBuilder.append(buf, START, read); } } catch (IOException e) { logger.error("Unable to read input stream due to an exception!", e); throw new IllegalStateException(e); } BasicDBList parse = (BasicDBList) JSON.parse(stringBuilder.toString(), new MongoIdTransformerJSONCallback()); collection.insert(parse.toArray(new DBObject[parse.size()])); }
From source file:org.jboss.aerogear.push.registration.DeviceRegistrationService.java
License:Apache License
/** * Stores the submitted token, OS and its version on a <i>global</i> database. * // w w w. j ava 2 s . c o m * @param token device token, submitted by the device * @param os Used Operating system * @param version version of the mobile OS */ public void registerDevice(String token, String os, String version) { DBCollection collection = database.getCollection(os); // new doc: DBObject device = new BasicDBObject(2); // we ignore the version.... device.put("os", os); device.put("token", token); collection.insert(device); }