List of usage examples for com.mongodb DBCollection insert
public WriteResult insert(final List<? extends DBObject> documents, final WriteConcern aWriteConcern, @Nullable final DBEncoder dbEncoder)
From source file:com.impetus.client.mongodb.MongoDBClient.java
License:Apache License
/** * On collections flush.//from ww w .j a v a 2 s . c om * * @param collections * collection containing records to be inserted in mongo db. */ private void onFlushCollection(Map<String, List<DBObject>> collections) { for (String tableName : collections.keySet()) { DBCollection dbCollection = mongoDb.getCollection(tableName); KunderaCoreUtils.printQuery("Persist collection:" + tableName, showQuery); try { dbCollection.insert(collections.get(tableName).toArray(new DBObject[0]), getWriteConcern(), encoder); } catch (MongoException ex) { throw new KunderaException( "document is not inserted in " + dbCollection.getFullName() + " collection. Caused By:", ex); } } }
From source file:ezbake.data.mongo.MongoDriverHandler.java
License:Apache License
public EzWriteResult insert_driver(String collection, EzInsertRequest req, EzSecurityToken token) throws TException, EzMongoDriverException { // if (!collection.equals("fs.chunks") && !collection.equals("fs.files")) { collection = appName + "_" + collection; // }// w w w. j a v a 2s.c o m appLog.debug("insert_driver() to collection: {}", collection); TokenUtils.validateSecurityToken(token, handler.getConfigurationProperties()); EzWriteResult ewr = new EzWriteResult(); try { DBCollection c = handler.db.getCollection(normalizeCollection(collection)); ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(req.getDbObjectList())); List<DBObject> list = (List<DBObject>) ois.readObject(); ois = new ObjectInputStream(new ByteArrayInputStream(req.getWriteConcern())); WriteConcern writeConcern = (WriteConcern) ois.readObject(); ois = new ObjectInputStream(new ByteArrayInputStream(req.getDbEncoder())); DBEncoder dbEncoder = (DBEncoder) ois.readObject(); HashMap<String, String> auditParamsMap = new HashMap<>(); auditParamsMap.put("action", "insert_driver"); auditParamsMap.put("collectionName", collection); auditParamsMap.put("list", handler.printMongoObject(list)); auditParamsMap.put("writeConcern", handler.printMongoObject(writeConcern)); auditParamsMap.put("dbEncoder", handler.printMongoObject(dbEncoder)); handler.auditLog(token, AuditEventType.FileObjectCreate, auditParamsMap); WriteResult res = null; Boolean isDriverUnitTestMode = req.isIsUnitTestMode(); if (!isDriverUnitTestMode) { // check if the user has the auths to insert these records List<DBObject> insertableList = new ArrayList<>(); for (DBObject dbObject : list) { try { handler.getMongoInsertHelper().checkAbilityToInsert(token, null, dbObject, null, false, true); insertableList.add(dbObject); } catch (ClassCastException | VisibilityParseException | EzMongoBaseException e) { appLog.error(e.toString()); appLog.debug("User does not have the auths to insert record: {}", dbObject); } } res = c.insert(insertableList, writeConcern, dbEncoder); } else { res = c.insert(list, writeConcern, dbEncoder); } if (list != null && list.size() == 1 && list.get(0) instanceof GridFSInputFile) { GridFSInputFile g = (GridFSInputFile) list.get(0); Object o = g.getId(); if (o instanceof ObjectId) { ObjectId id = (ObjectId) o; cm.getCache(GRID_FS_INPUT_FILE_MAP_CACHE).put(new Element(id.toString(), id)); } } appLog.debug("WriteResult: {}", res); ByteArrayOutputStream bOut = new ByteArrayOutputStream(); new ObjectOutputStream(bOut).writeObject(res); ewr.setWriteResult(bOut.toByteArray()); } catch (Exception e) { appLog.error(e.toString()); EzMongoDriverException eme = new EzMongoDriverException(); eme.setEx(ser(e)); throw eme; } return ewr; }
From source file:org.cryptorchat.json.JsonDBObject.java
License:Apache License
public void insertToCollection(final DBCollection collection) { final JsonDBObject[] array = { this }; final Object oid = get("_id"); if (oid != null && !ObjectId.isValid(oid.toString())) { objectId = new ObjectId(); put("_id", objectId); }/*from w ww . j av a 2 s . co m*/ collection.insert(array, collection.getWriteConcern(), new JsonDBEncoder()); }