/** * Insert documents into a collection. If the collection does not exists on the server, then it will be created. If the new document * does not contain an '_id' field, it will be added. * * @param documents {@code DBObject}'s to be inserted * @param writeConcern {@code WriteConcern} to be used during operation * @return the result of the operation * @throws com.mongodb.DuplicateKeyException if the write failed to a duplicate unique key * @throws com.mongodb.WriteConcernException if the write failed due some other failure specific to the insert command * @throws MongoException if the operation failed for some other reason * @mongodb.driver.manual tutorial/insert-documents/ Insert Documents */ public WriteResult insert(final WriteConcern writeConcern, final DBObject... documents) { return insert(documents, writeConcern); }
/** * Insert documents into a collection. If the collection does not exists on the server, then it will be created. If the new document * does not contain an '_id' field, it will be added. * * @param documents list of {@code DBObject}'s to be inserted * @param aWriteConcern {@code WriteConcern} to be used during operation * @return the result of the operation * @throws com.mongodb.DuplicateKeyException if the write failed to a duplicate unique key * @throws com.mongodb.WriteConcernException if the write failed due some other failure specific to the insert command * @throws MongoException if the operation failed for some other reason * @mongodb.driver.manual tutorial/insert-documents/ Insert Documents */ public WriteResult insert(final List<? extends DBObject> documents, final WriteConcern aWriteConcern) { return insert(documents, aWriteConcern, null); }
/** * Insert documents into a collection. If the collection does not exists on the server, then it will be created. If the new document * does not contain an '_id' field, it will be added. * * @param documents {@code DBObject}'s to be inserted * @param aWriteConcern {@code WriteConcern} to be used during operation * @param encoder {@code DBEncoder} to be used * @return the result of the operation * @throws com.mongodb.DuplicateKeyException if the write failed to a duplicate unique key * @throws com.mongodb.WriteConcernException if the write failed due some other failure specific to the insert command * @throws MongoException if the operation failed for some other reason * @mongodb.driver.manual tutorial/insert-documents/ Insert Documents */ public WriteResult insert(final DBObject[] documents, final WriteConcern aWriteConcern, final DBEncoder encoder) { return insert(asList(documents), aWriteConcern, encoder); }
/** * Insert documents into a collection. If the collection does not exists on the server, then it will be created. If the new document * does not contain an '_id' field, it will be added. * * @param documents {@code DBObject}'s to be inserted * @param writeConcern {@code WriteConcern} to be used during operation * @return the result of the operation * @throws com.mongodb.DuplicateKeyException if the write failed to a duplicate unique key * @throws com.mongodb.WriteConcernException if the write failed due some other failure specific to the insert command * @throws MongoException if the operation failed for some other reason * @mongodb.driver.manual tutorial/insert-documents/ Insert Documents */ public WriteResult insert(final DBObject[] documents, final WriteConcern writeConcern) { return insert(asList(documents), writeConcern); }
/** * Insert a document into a collection. If the collection does not exists on the server, then it will be created. If the new document * does not contain an '_id' field, it will be added. * * @param document {@code DBObject} to be inserted * @param writeConcern {@code WriteConcern} to be used during operation * @return the result of the operation * @throws com.mongodb.DuplicateKeyException if the write failed to a duplicate unique key * @throws com.mongodb.WriteConcernException if the write failed due some other failure specific to the insert command * @throws MongoException if the operation failed for some other reason * @mongodb.driver.manual tutorial/insert-documents/ Insert Documents */ public WriteResult insert(final DBObject document, final WriteConcern writeConcern) { return insert(asList(document), writeConcern); }
/** * Insert documents into a collection. If the collection does not exists on the server, then it will be created. If the new document * does not contain an '_id' field, it will be added. * * @param documents list of {@code DBObject} to be inserted * @return the result of the operation * @throws com.mongodb.DuplicateKeyException if the write failed to a duplicate unique key * @throws com.mongodb.WriteConcernException if the write failed due some other failure specific to the insert command * @throws MongoException if the operation failed for some other reason * @mongodb.driver.manual tutorial/insert-documents/ Insert Documents */ public WriteResult insert(final List<? extends DBObject> documents) { return insert(documents, getWriteConcern()); }
/** * Insert documents into a collection. If the collection does not exists on the server, then it will be created. If the new document * does not contain an '_id' field, it will be added. Collection wide {@code WriteConcern} will be used. * * @param documents {@code DBObject}'s to be inserted * @return the result of the operation * @throws com.mongodb.DuplicateKeyException if the write failed to a duplicate unique key * @throws com.mongodb.WriteConcernException if the write failed due some other failure specific to the insert command * @throws MongoException if the operation failed for some other reason * @mongodb.driver.manual tutorial/insert-documents/ Insert Documents */ public WriteResult insert(final DBObject... documents) { return insert(asList(documents), getWriteConcern()); }
protected <T> Key<T> insert(final DBCollection dbColl, final T entity, final InsertOptions options) { final LinkedHashMap<Object, DBObject> involvedObjects = new LinkedHashMap<Object, DBObject>(); dbColl.insert(singletonList(entityToDBObj(entity, involvedObjects)), enforceWriteConcern(options, entity.getClass()) .getOptions()); return postSaveOperations(singletonList(entity), involvedObjects, dbColl.getName()).get(0); }
private <T> Iterable<Key<T>> insert(final DBCollection dbColl, final Iterable<T> entities, final InsertOptions options) { if (!entities.iterator().hasNext()) { return emptyList(); } final Map<Object, DBObject> involvedObjects = new LinkedHashMap<Object, DBObject>(); final List<DBObject> list = new ArrayList<DBObject>(); com.mongodb.InsertOptions insertOptions = options.getOptions(); for (final T entity : entities) { if (options.getWriteConcern() == null) { insertOptions = enforceWriteConcern(options, entity.getClass()).getOptions(); } list.add(toDbObject(entity, involvedObjects)); } dbColl.insert(list, insertOptions); return postSaveOperations(entities, involvedObjects, dbColl.getName()); } /*
/** * Update an existing document or insert a document depending on the parameter. If the document does not contain an '_id' field, then * the method performs an insert with the specified fields in the document as well as an '_id' field with a unique objectId value. If * the document contains an '_id' field, then the method performs an upsert querying the collection on the '_id' field: * <ul> * <li>If a document does not exist with the specified '_id' value, the method performs an insert with the specified fields in * the document.</li> * <li>If a document exists with the specified '_id' value, the method performs an update, * replacing all field in the existing record with the fields from the document.</li> * </ul> * * @param document {@link DBObject} to save to the collection. * @param writeConcern {@code WriteConcern} to be used during operation * @return the result of the operation * @throws com.mongodb.DuplicateKeyException if the write failed to a duplicate unique key * @throws com.mongodb.WriteConcernException if the write failed due some other failure specific to the insert or update command * @throws MongoException if the operation failed for some other reason * @mongodb.driver.manual tutorial/modify-documents/#modify-a-document-with-save-method Save */ public WriteResult save(final DBObject document, final WriteConcern writeConcern) { Object id = document.get(ID_FIELD_NAME); if (id == null) { return insert(document, writeConcern); } else { return replaceOrInsert(document, id, writeConcern); } }
public static void loadData() throws IOException { InputStream input = Thread.currentThread().getContextClassLoader().getResourceAsStream(DATA_FILE); BufferedReader reader = new BufferedReader(new InputStreamReader(input)); String jsonLine = ""; while ((jsonLine = reader.readLine()) != null) { if (StringUtils.isNotBlank(jsonLine)) { String currentLine = jsonLine.trim(); if (jsonLine.startsWith(COLLECTION_NAME_LINE)) { setCollectionFromName(currentLine); } else { currentCollection.insert((DBObject) JSON.parse(jsonLine), WriteConcern.ACKNOWLEDGED); } } } input.close(); LOG.info("Load data finished."); }
/** * Insert documents into a collection. If the collection does not exists on the server, then it will be created. If the new document * does not contain an '_id' field, it will be added. * * @param documents a list of {@code DBObject}'s to be inserted * @param aWriteConcern {@code WriteConcern} to be used during operation * @param dbEncoder {@code DBEncoder} to be used * @return the result of the operation * @throws com.mongodb.DuplicateKeyException if the write failed to a duplicate unique key * @throws com.mongodb.WriteConcernException if the write failed due some other failure specific to the insert command * @throws MongoException if the operation failed for some other reason * @mongodb.driver.manual tutorial/insert-documents/ Insert Documents */ public WriteResult insert(final List<? extends DBObject> documents, final WriteConcern aWriteConcern, @Nullable final DBEncoder dbEncoder) { return insert(documents, new InsertOptions().writeConcern(aWriteConcern).dbEncoder(dbEncoder)); }
private WriteResult saveDocument(final DBCollection dbColl, final DBObject document, final InsertOptions options) { if (document.get(ID_FIELD_NAME) == null) { return dbColl.insert(singletonList(document), options.getOptions()); } else { return dbColl.update(new BasicDBObject(ID_FIELD_NAME, document.get(ID_FIELD_NAME)), document, new DBCollectionUpdateOptions() .bypassDocumentValidation(options.getBypassDocumentValidation()) .writeConcern(options.getWriteConcern()) .upsert(true)); } }
wr = dbColl.insert(singletonList(dbObj), options.getOptions()); } catch (DuplicateKeyException e) { throw new ConcurrentModificationException(format("Entity of class %s (id='%s') was concurrently saved.",
/** * <p>Insert documents into a collection. If the collection does not exists on the server, then it will be created. If the new document * does not contain an '_id' field, it will be added.</p> * * <p>If the value of the continueOnError property of the given {@code InsertOptions} is true, * that value will override the value of the continueOnError property of the given {@code WriteConcern}. Otherwise, * the value of the continueOnError property of the given {@code WriteConcern} will take effect. </p> * * @param documents a list of {@code DBObject}'s to be inserted * @param insertOptions the options to use for the insert * @return the result of the operation * @throws com.mongodb.DuplicateKeyException if the write failed to a duplicate unique key * @throws com.mongodb.WriteConcernException if the write failed due some other failure specific to the insert command * @throws MongoException if the operation failed for some other reason * @mongodb.driver.manual tutorial/insert-documents/ Insert Documents */ public WriteResult insert(final List<? extends DBObject> documents, final InsertOptions insertOptions) { WriteConcern writeConcern = insertOptions.getWriteConcern() != null ? insertOptions.getWriteConcern() : getWriteConcern(); Encoder<DBObject> encoder = toEncoder(insertOptions.getDbEncoder()); List<InsertRequest> insertRequestList = new ArrayList<InsertRequest>(documents.size()); for (DBObject cur : documents) { if (cur.get(ID_FIELD_NAME) == null) { cur.put(ID_FIELD_NAME, new ObjectId()); } insertRequestList.add(new InsertRequest(new BsonDocumentWrapper<DBObject>(cur, encoder))); } return insert(insertRequestList, writeConcern, insertOptions.isContinueOnError(), insertOptions.getBypassDocumentValidation()); }
private Characters testMapping(final String field, final String value) { getMorphia().map(Characters.class); final DBCollection collection = getDs().getCollection(Characters.class); collection.insert(new BasicDBObject(field, value)); return getDs().find(Characters.class).find(new FindOptions().limit(1)).tryNext(); }
MongoServer server = new MongoServer(new MemoryBackend()); // bind on a random local port InetSocketAddress serverAddress = server.bind(); MongoClient client = new MongoClient(new ServerAddress(serverAddress)); DBCollection coll = client.getDB("testdb").getCollection("testcoll"); // creates the database and collection in memory and inserts the object coll.insert(new BasicDBObject("key", "value")); assertEquals(1, collection.count()); assertEquals("value", collection.findOne().get("key")); client.close(); server.shutdownNow();
/** * On collections flush. * * @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); } } }
public void put(final String url, final String accessToken, final String user) throws IOException { final DBObject token = getToken(url); final String encryptedToken = getEncryptedValue(accessToken); if (token != null) { getCollection().remove(token); } final BasicDBObject doc = new BasicDBObject("user", user).append("access_token", encryptedToken).append("repo_url", url); getCollection().insert(doc); }
@Override void insertJobInstance(final JobInstanceImpl jobInstance) { final Long nextId = incrementAndGetSequence(TableColumns.JOBINSTANCEID); jobInstance.setId(nextId); final DBObject dbObject = new BasicDBObject(TableColumns.JOBINSTANCEID, nextId); dbObject.put(TableColumns.JOBNAME, jobInstance.getJobName()); dbObject.put(TableColumns.APPLICATIONNAME, jobInstance.getApplicationName()); db.getCollection(TableColumns.JOB_INSTANCE).insert(dbObject); }