List of usage examples for com.mongodb DBCollection insert
public WriteResult insert(final List<? extends DBObject> documents, final InsertOptions insertOptions)
Insert documents into a collection.
From source file:com.ibm.sae.LoadDreamHomeDB.java
/***************************************************************/ private static void loadOffice(DB db, DBCollection coll) { System.out.println("LoadDreamHomeDB:loadOffice begins"); coll.drop();/*from w ww. java2 s. c om*/ DBObject officeDoc = new BasicDBObject("officeId", 2999).append("officeName", "Valley North") .append("officeManager", "Erlich Bachman") .append("officeAddr", new BasicDBObject("address", "223 Mountain Drive") .append("city", "Buena Vista").append("state", "California").append("zip", "65746")) .append("numProperties", 0); WriteResult wr = coll.insert(officeDoc, WriteConcern.ACKNOWLEDGED); System.out.println("LoadDreamHomeDB:loadOffice ends"); }
From source file:com.ibm.sae.LoadDreamHomeDB.java
/***************************************************************/ private static void loadProperty(DB db, DBCollection coll) { System.out.println("LoadDreamHomeDB:loadOffice begins"); coll.drop();/*w w w . java2 s . co m*/ DBObject propertyDoc = new BasicDBObject("propertyId", 2000) .append("location", new BasicDBObject("address", "1024 College").append("city", "Wheaton") .append("state", "California").append("zip", "76857") .append("longitude", "35.601623").append("latitude", "-78.245908")) .append("sqFeet", 2895).append("numBeds", 4).append("numBaths", 3) .append("description", "Two blocks from university").append("askingPrice", 789900.00); WriteResult wr = coll.insert(propertyDoc, WriteConcern.ACKNOWLEDGED); DBObject propertyDoc2 = new BasicDBObject("propertyId", 2001) .append("location", new BasicDBObject("address", "435 Weston").append("city", "Springfield") .append("state", "California").append("zip", "76857") .append("longitude", "36.507623").append("latitude", "-79.145509")) .append("sqFeet", 3200).append("numBeds", 5).append("numBaths", 3) .append("description", "Nice cottage by lake").append("askingPrice", 569900.00); WriteResult wr2 = coll.insert(propertyDoc2, WriteConcern.ACKNOWLEDGED); DBObject propertyDoc3 = new BasicDBObject("propertyId", 2002) .append("location", new BasicDBObject("address", "2240 Berlin").append("city", "Florence") .append("state", "California").append("zip", "76857") .append("longitude", "31.086579").append("latitude", "-72.357987")) .append("sqFeet", 3950).append("numBeds", 5).append("numBaths", 5) .append("description", "Mansion in the city").append("askingPrice", 645800.00); WriteResult wr3 = coll.insert(propertyDoc3, WriteConcern.ACKNOWLEDGED); System.out.println("LoadDreamHomeDB:loadProperty ends"); }
From source file:com.ikanow.infinit.e.data_model.utils.MongoTransactionLock.java
License:Apache License
protected synchronized boolean getToken() { DBCollection cachedCollection = _collections.get(); boolean bDefinitelyHaveControl = false; String hostname = null;/* w ww . j a v a 2 s . c o m*/ String oneUp = null; // 1] Get Lock Object (create one an assert control if it doesn't exist) BasicDBObject lockObj = (BasicDBObject) cachedCollection.findOne(); if (null == lockObj) { // Currently the DB is unlocked hostname = getHostname(); oneUp = Long.toString(1000000L * (new Date().getTime() % 10000)); // (ie a randomish start number) lockObj = new BasicDBObject(id_, _lockId); lockObj.put(hostname_, hostname); lockObj.put(oneUp_, oneUp); lockObj.put(lastUpdated_, new Date()); //logger.debug("Creating a new aggregation lock object: " + lockObj.toString()); try { cachedCollection.insert(lockObj, WriteConcern.SAFE); // (will fail if another harvester gets there first) bDefinitelyHaveControl = true; } catch (Exception e) { // Someone else has created it in the meantime lockObj = (BasicDBObject) cachedCollection.findOne(); } } //TESTED // (So by here lockObj is always non-null) // 2] Do I have control? if (bDefinitelyHaveControl) { _bHaveControl = true; _nLastCheck = 0; } else { hostname = lockObj.getString(hostname_); oneUp = lockObj.getString(oneUp_); _bHaveControl = getHostname().equals(hostname); } // 3] If not, has the lock object been static for >= 1 minute if (!_bHaveControl) { // Don't currently have control long nNow = new Date().getTime(); if (0 == _nLastCheck) { _nLastCheck = nNow; } if ((nNow - _nLastCheck) > 60000) { // re-check every minute if (_savedHostname.equals(hostname) && _savedOneUp.equals(oneUp)) { // Now I have control... //logger.debug("I am taking control from: " + hostname + ", " + oneUp); if (updateToken(true)) { // Try to grab control: _bHaveControl = true; } else { // (else someone else snagged control just carry on) _nLastCheck = 0; // (reset clock again anyway) } } //(if lock has remained static) } //(end if >=1 minutes has passed) } //(end if have don't have control) // 4] Update saved state _savedHostname = hostname; _savedOneUp = oneUp; return _bHaveControl; }
From source file:com.nesscomputing.mongo.MongoWriter.java
License:Apache License
protected void flushToMongo(final List<Callable<DBObject>> dbObjects) { LOG.trace("Starting write of %d elements...", dbObjects.size()); final DBCollection collection = dbCollection.get(); if (collection != null) { final WriteResult writeResult = collection.insert(Lists.transform(dbObjects, CALLABLE_FUNCTION), WriteConcern.NORMAL);//from w w w . j a v a 2s .co m final CommandResult cmdResult = writeResult.getLastError(); if (cmdResult.ok()) { opsSent.addAndGet(dbObjects.size()); } else { LOG.warn("Command returned %s", cmdResult.getErrorMessage()); opsLost.addAndGet(dbObjects.size()); } LOG.trace("Wrote %d put ops to Mongo dbCollection %s.", dbObjects.size(), collectionName); } else { LOG.warn("dbCollection is null, probably shutting down!"); } }
From source file:com.staticvillage.recommender.indexer.MongoDBIndexer.java
License:Apache License
@Override public void addBean(Place bean) throws IndexerException { DBCollection dbCollection = instanceDB.getCollection(collection); dbCollection.insert(toDBObject(bean), WriteConcern.NORMAL); }
From source file:de.belaso.mongolyn.ui.TaskDataHandler.java
License:Open Source License
@Override public RepositoryResponse postTaskData(TaskRepository repository, TaskData taskData, Set<TaskAttribute> oldAttributes, IProgressMonitor monitor) throws CoreException { DBCollection dbCollection = MongolynUtils.getDBCollection(repository); BasicDBObjectBuilder bob = BasicDBObjectBuilder.start(); for (Map.Entry<String, TaskAttribute> entry : taskData.getRoot().getAttributes().entrySet()) { String key = entry.getKey(); TaskAttribute attribute = entry.getValue(); String attributeValue = attribute.getValue(); if (attributeValue != null) { bob.add(key.replace('.', '_'), attributeValue); }/*from w ww . j a v a2s .c o m*/ } DBObject dbObject = bob.get(); try { if (taskData.isNew()) { dbCollection.insert(dbObject, WriteConcern.SAFE); return new RepositoryResponse(ResponseKind.TASK_CREATED, dbObject.get("_id").toString()); } else { dbCollection.findAndModify(new BasicDBObject("_id", new ObjectId(taskData.getTaskId())), dbObject); return new RepositoryResponse(ResponseKind.TASK_UPDATED, taskData.getTaskId()); } } catch (MongoException mongoException) { throw new CoreException(Activator.INSTANCE.getErrorStatus(mongoException)); } }
From source file:in.mtap.iincube.mongoapi.MongoWriter.java
License:Apache License
public WriteResult execute(WriteConcern writeConcern) { assertArguments();/*from w ww . j a v a 2 s. c om*/ assertNotNull(writeConcern, "WriteConcern == null"); DBCollection collection = collectionFactory.get(); return collection.insert(dbObjects, writeConcern); }
From source file:it.wami.map.mongodeploy.OsmSaxHandler.java
License:Apache License
private void saveTag(String key, String value) { if (Arrays.asList(Tag.TYPES).contains(key)) { Tag tag = new Tag(); tag.setKeyValue(key, value);/*from w w w . ja v a2s. co m*/ Map<String, String> pair = new HashMap<String, String>(); pair.put(key, value); if (pairList.contains(pair)) return; pairList.add(pair); DBCollection coll = db.getCollection(COLL_TAGS); //if(coll.find(new BasicDBObject(key, value)).count() > 0) //return; try { // this speed up the proccess coll.insert(tag, WriteConcern.UNACKNOWLEDGED); } catch (IllegalArgumentException e) { e.printStackTrace(); } } }
From source file:it.wami.map.mongodeploy.OsmSaxHandler.java
License:Apache License
/** * Save the array in mongoDB./*w w w . j a v a 2s . co m*/ * @param obj the entry * @param collection */ private void saveEntry(List<DBObject> obj, String collection) { DBCollection coll = db.getCollection(collection); try { // this speed up the proccess synchronized (obj) { try { coll.insert(obj, WriteConcern.UNACKNOWLEDGED); } catch (MongoInternalException e) { recover(coll, obj); } nodesQueue.clear(); waysQueue.clear(); relationsQueue.clear(); tagsQueue.clear(); } } catch (IllegalArgumentException e) { e.printStackTrace(); } }
From source file:it.wami.map.mongodeploy.OsmSaxHandler.java
License:Apache License
/** * Try to recover the objects that can be saved inside the DB. * /*from w w w . jav a 2 s . co m*/ * @param coll {@link DBCollection} the target collection. * @param list {@link List}<{@link DBObject}> the list of objs to save inside the DB. */ private void recover(DBCollection coll, List<DBObject> list) { for (DBObject o : list) { try { coll.insert(o, WriteConcern.UNACKNOWLEDGED); } catch (MongoInternalException e) { System.err.println(o.get("_id")); o.removeField("loc"); coll.insert(o, WriteConcern.UNACKNOWLEDGED); } } }