List of usage examples for com.mongodb.client MongoCollection insertOne
void insertOne(TDocument document);
From source file:com.bluedragon.mongo.MongoCollectionInsert.java
License:Open Source License
public cfData execute(cfSession _session, cfArgStructData argStruct) throws cfmRunTimeException { MongoDatabase db = getMongoDatabase(_session, argStruct); String collection = getNamedStringParam(argStruct, "collection", null); if (collection == null) throwException(_session, "please specify a collection"); cfData data = getNamedParam(argStruct, "data", null); if (data == null) throwException(_session, "please specify data to insert"); try {/*w w w . j av a 2 s .c o m*/ MongoCollection<Document> col = db.getCollection(collection); if (data.getDataType() == cfData.CFARRAYDATA) { cfArrayData idArr = cfArrayData.createArray(1); List<Document> list = new ArrayList<Document>(); cfArrayData arrdata = (cfArrayData) data; for (int x = 0; x < arrdata.size(); x++) { Document doc = getDocument(arrdata.getData(x + 1)); idArr.addElement(new cfStringData(String.valueOf(doc.get("_id")))); list.add(doc); } long start = System.currentTimeMillis(); col.insertMany(list); _session.getDebugRecorder().execMongo(col, "insert", null, System.currentTimeMillis() - start); return idArr; } else { Document doc = getDocument(data); long start = System.currentTimeMillis(); col.insertOne(doc); _session.getDebugRecorder().execMongo(col, "insert", null, System.currentTimeMillis() - start); return new cfStringData(String.valueOf(doc.get("_id"))); } } catch (MongoException me) { throwException(_session, me.getMessage()); return null; } }
From source file:com.bluedragon.mongo.MongoCollectionSave.java
License:Open Source License
public cfData execute(cfSession _session, cfArgStructData argStruct) throws cfmRunTimeException { MongoDatabase db = getMongoDatabase(_session, argStruct); String collection = getNamedStringParam(argStruct, "collection", null); if (collection == null) throwException(_session, "please specify a collection"); cfData data = getNamedParam(argStruct, "data", null); if (data == null) throwException(_session, "please specify data to save"); try {//from w w w . j a va 2 s.c om Document doc = getDocument(data); MongoCollection<Document> col = db.getCollection(collection); long start = System.currentTimeMillis(); if (doc.containsKey("_id")) { col.updateOne(new Document("_id", doc.get("_id")), new Document("$set", doc)); } else { col.insertOne(doc); } _session.getDebugRecorder().execMongo(col, "save", doc, System.currentTimeMillis() - start); return cfBooleanData.TRUE; } catch (Exception me) { throwException(_session, me.getMessage()); return null; } }
From source file:com.centurylink.mdw.mongo.MongoDocumentDb.java
License:Apache License
@Override public void createDocument(String ownerType, Long documentId, String content) { MongoCollection<org.bson.Document> collection = getMongoDb().getCollection(ownerType); org.bson.Document myDoc = null; if (content.startsWith("{")) { try {/*from w w w . ja v a2 s. c o m*/ // Parse JSON to create BSON CONTENT Document org.bson.Document myJsonDoc = org.bson.Document.parse(content); if (!myJsonDoc.isEmpty()) { if (content.contains(".") || content.contains("$")) myJsonDoc = encodeMongoDoc(myJsonDoc); // Plus append _id and isJSON:true field myDoc = new org.bson.Document("CONTENT", myJsonDoc).append("document_id", documentId) .append("isJSON", true); } } catch (Throwable ex) { myDoc = null; } // Assume not JSON then } if (myDoc == null) { // Create BSON document with Raw content if it wasn't JSON, plus append _id and isJSON:false myDoc = new org.bson.Document("CONTENT", content).append("document_id", documentId).append("isJSON", false); } collection.insertOne(myDoc); if (!checkForDocIdIndex(ownerType)) createMongoDocIdIndex(ownerType); }
From source file:com.centurylink.mdw.service.data.process.EngineDataAccessDB.java
License:Apache License
public Long createDocument(Document doc, Package pkg) throws SQLException { Long docId = db.isMySQL() ? null : getNextId("MDW_COMMON_INST_ID_SEQ"); String query = "insert into DOCUMENT " + "(DOCUMENT_ID, CREATE_DT, DOCUMENT_TYPE, OWNER_TYPE, OWNER_ID, STATUS_CODE, STATUS_MESSAGE) " + "values (?, " + now() + ", ?, ?, ?, ?, ?)"; Object[] args = new Object[6]; args[0] = docId;/*from ww w .j a va 2 s. c o m*/ args[1] = doc.getDocumentType(); args[2] = doc.getOwnerType(); args[3] = doc.getOwnerId(); if (doc.getStatusCode() == null) args[4] = 0; else args[4] = doc.getStatusCode(); if (doc.getStatusMessage() == null) args[5] = ""; else args[5] = doc.getStatusMessage(); if (db.isMySQL()) docId = db.runInsertReturnId(query, args); else if (db.isOracle()) db.runUpdate(query, args); else db.runUpdate(query, String.valueOf(args)); doc.setDocumentId(docId); if (hasMongo()) { MongoCollection<org.bson.Document> collection = DatabaseAccess.getMongoDb() .getCollection(doc.getOwnerType()); org.bson.Document myDoc = null; if (doc.getContent(pkg).trim().startsWith("{") && doc.getContent(pkg).trim().endsWith("}")) { try { org.bson.Document myJsonDoc = org.bson.Document.parse(doc.getContent(pkg)); // Parse JSON to create BSON CONTENT Document if (!myJsonDoc.isEmpty()) { if (doc.getContent(pkg).contains(".") || doc.getContent(pkg).contains("$")) myJsonDoc = DatabaseAccess.encodeMongoDoc(myJsonDoc); myDoc = new org.bson.Document("CONTENT", myJsonDoc).append("_id", docId).append("isJSON", true); // Plus append _id and isJSON:true field } } catch (Throwable ex) { myDoc = null; } // Assume not JSON then } if (myDoc == null) // Create BSON document with Raw content if it wasn't JSON, plus append _id and isJSON:false myDoc = new org.bson.Document("CONTENT", doc.getContent(pkg)).append("_id", docId).append("isJSON", false); collection.insertOne(myDoc); } else { // store in DOCUMENT_CONTENT query = "insert into DOCUMENT_CONTENT (DOCUMENT_ID, CONTENT) values (?, ?)"; args = new Object[2]; args[0] = docId; args[1] = doc.getContent(pkg); db.runUpdate(query, args); } return docId; }
From source file:com.cognifide.aet.vs.metadata.MetadataDAOMongoDBImpl.java
License:Apache License
@Override public Suite saveSuite(Suite suite) throws StorageException, ValidatorException { MongoCollection<Document> metadata = getMetadataCollection(new SimpleDBKey(suite)); suite.validate(null);/*from www. j a va 2 s . c o m*/ LOGGER.debug("Saving suite {} to metadata collection.", suite); metadata.insertOne(Document.parse(GSON.toJson(suite, SUITE_TYPE))); return getSuite(new SimpleDBKey(suite), suite.getCorrelationId()); }
From source file:com.cognifide.aet.vs.metadata.MetadataDAOMongoDBImpl.java
License:Apache License
/** * Updates suite in .metadata collection only if older version exist, * Also updates version and timestamp of a suite. * * @param suite new suite version to save * @return updated suite./*from ww w .java2 s. co m*/ */ @Override public Suite updateSuite(Suite suite) throws StorageException, ValidatorException { MongoCollection<Document> metadata = getMetadataCollection(new SimpleDBKey(suite)); LOGGER.debug("Updating suite {} in metadata collection.", suite); if (isNewestSuite(suite)) { suite.incrementVersion(); suite.setRunTimestamp(new Suite.Timestamp(System.currentTimeMillis())); suite.validate(null); metadata.insertOne(Document.parse(GSON.toJson(suite, SUITE_TYPE))); } else { throw new StorageException("Trying to update old version or not existing suite."); } return getSuite(new SimpleDBKey(suite), suite.getCorrelationId()); }
From source file:com.epam.dlab.auth.dao.UserInfoDAOMongoImpl.java
License:Apache License
@Override public void saveUserInfo(UserInfo ui) { //UserInfo first cached and immediately becomes available //Saving can be asynch BasicDBObject uiDoc = new BasicDBObject(); uiDoc.put("_id", ui.getAccessToken()); uiDoc.put("name", ui.getName()); uiDoc.put("firstName", ui.getFirstName()); uiDoc.put("lastName", ui.getLastName()); uiDoc.put("roles", ui.getRoles()); uiDoc.put("remoteIp", ui.getRemoteIp()); uiDoc.put("awsUser", ui.isAwsUser()); uiDoc.put("expireAt", new Date(System.currentTimeMillis())); uiDoc.put("awsKeys", ui.getKeys()); MongoCollection<BasicDBObject> security = ms.getCollection("security", BasicDBObject.class); security.insertOne(uiDoc); log.debug("Saved persistent {}", ui); }
From source file:com.epam.dlab.mongo.MongoDbConnection.java
License:Apache License
/** * Insert document to Mongo./*from w w w . j a v a 2s . c o m*/ * * @param collection the name of collection. * @param document the document. * @throws AdapterException */ public void insertOne(MongoCollection<Document> collection, Document document) throws AdapterException { try { collection.insertOne(document); } catch (Exception e) { throw new AdapterException("Cannot insert document into collection " + collection.getNamespace() + ": " + e.getLocalizedMessage(), e); } }
From source file:com.facebook.presto.mongodb.MongoSession.java
License:Apache License
private Document getTableMetadata(SchemaTableName schemaTableName) throws TableNotFoundException { String schemaName = schemaTableName.getSchemaName(); String tableName = schemaTableName.getTableName(); MongoDatabase db = client.getDatabase(schemaName); MongoCollection<Document> schema = db.getCollection(schemaCollection); Document doc = schema.find(new Document(TABLE_NAME_KEY, tableName)).first(); if (doc == null) { if (!collectionExists(db, tableName)) { throw new TableNotFoundException(schemaTableName); } else {/*from w ww . j a v a2 s.c o m*/ Document metadata = new Document(TABLE_NAME_KEY, tableName); metadata.append(FIELDS_KEY, guessTableFields(schemaTableName)); schema.createIndex(new Document(TABLE_NAME_KEY, 1), new IndexOptions().unique(true)); schema.insertOne(metadata); return metadata; } } return doc; }
From source file:com.facebook.presto.mongodb.MongoSession.java
License:Apache License
private void createTableMetadata(SchemaTableName schemaTableName, List<MongoColumnHandle> columns) throws TableNotFoundException { String schemaName = schemaTableName.getSchemaName(); String tableName = schemaTableName.getTableName(); MongoDatabase db = client.getDatabase(schemaName); Document metadata = new Document(TABLE_NAME_KEY, tableName); ArrayList<Document> fields = new ArrayList<>(); if (!columns.stream().anyMatch(c -> c.getName().equals("_id"))) { fields.add(new MongoColumnHandle("_id", OBJECT_ID, true).getDocument()); }/* www.ja v a 2 s.com*/ fields.addAll(columns.stream().map(MongoColumnHandle::getDocument).collect(toList())); metadata.append(FIELDS_KEY, fields); MongoCollection<Document> schema = db.getCollection(schemaCollection); schema.createIndex(new Document(TABLE_NAME_KEY, 1), new IndexOptions().unique(true)); schema.insertOne(metadata); }