List of usage examples for com.mongodb DBCollection find
public DBCursor find()
From source file:com.ikanow.infinit.e.harvest.extraction.document.file.InternalInfiniteFile.java
License:Open Source License
public InternalInfiniteFile(String url, NtlmPasswordAuthentication auth) throws MalformedURLException { try {//from ww w . j av a 2s . com ObjectId locationId = null; ObjectId ownerId = null; String communityIdsStr = null; if (url.startsWith(INFINITE_SHARE_PREFIX)) { _isShare = true; locationId = new ObjectId(url.substring(INFINITE_SHARE_PREFIX_LEN).replaceFirst("/.*$", "")); // remove trailing /s, can be used for information //TESTED (2.1, 2.2.1, 2.3) BasicDBObject query = new BasicDBObject(SharePojo._id_, locationId); _resultObj = (BasicDBObject) MongoDbManager.getSocial().getShare().findOne(query); if (null == _resultObj) { throw new MalformedURLException("Not found (or not authorized): " + url); } //TESTED (7.1) String mediaType = (String) _resultObj.get(SharePojo.mediaType_); if ((null != mediaType) && (mediaType.equalsIgnoreCase("application/x-zip-compressed") || mediaType.equalsIgnoreCase("application/zip"))) { _isDirectory = true; ObjectId fileId = _resultObj.getObjectId(SharePojo.binaryId_); GridFSRandomAccessFile file = new GridFSRandomAccessFile( MongoDbManager.getSocial().getShareBinary(), fileId); _zipView = new GridFSZipFile(_resultObj.getString(SharePojo.title_), file); } //TESTED (3.1) else { // Single share if (_resultObj.containsField(SharePojo.documentLocation_)) { throw new MalformedURLException("Reference shares are not currently supported"); } //TESTED (0.1) _isDirectory = false; // (this will get traversed as the initial "directory", which doesn't check isDirectory... //... and will return itself as a single file in the "directory") } //TESTED (1.1, 2.1, 3.1) } //TESTED else if (url.startsWith(INFINITE_CUSTOM_PREFIX)) { _isCustom = true; _isDirectory = true; BasicDBObject query = null; String locationStr = url.substring(INFINITE_CUSTOM_PREFIX_LEN).replaceFirst("/.*$", ""); StringBuffer sb = new StringBuffer(INFINITE_CUSTOM_PREFIX); try { locationId = new ObjectId(locationStr); query = new BasicDBObject(CustomMapReduceJobPojo._id_, locationId); } //TESTED (4.1) catch (Exception e) { // for custom jobs can also specify the job name query = new BasicDBObject(CustomMapReduceJobPojo.jobtitle_, locationStr); } //TESTED (5.1, 6.1) _resultObj = (BasicDBObject) MongoDbManager.getCustom().getLookup().findOne(query); if (null == _resultObj) { throw new MalformedURLException("Not found (or not authorized): " + url); } //TESTED (7.2, 7.3) if (null != locationId) { sb.append(locationStr).append('/') .append(_resultObj.getString(CustomMapReduceJobPojo.jobtitle_)).append('/'); } //TESTED (5.1, 6.1) else { sb.append(_resultObj.getObjectId(CustomMapReduceJobPojo._id_).toString()).append('/') .append(locationStr).append('/'); } //TESTED (4.1) _originalUrl = sb.toString(); _isCustomAppend = _resultObj.getBoolean(CustomMapReduceJobPojo.appendResults_, false); String outputDatabase = _resultObj.getString(CustomMapReduceJobPojo.outputDatabase_); String outputCollection = _resultObj.getString(CustomMapReduceJobPojo.outputCollection_); if (null == outputDatabase) { outputDatabase = "custommr"; } DBCollection outColl = MongoDbManager.getCollection(outputDatabase, outputCollection); BasicDBObject sort = null; if (_isCustomAppend) { // Use time of _last_ record as file time sort = new BasicDBObject("_id", -1); } else { // Use time of _first_ record as file time sort = new BasicDBObject("_id", 1); } //TESTED DBCursor dbc = outColl.find().sort(sort).limit(1); List<DBObject> firstOrLastRecordList = dbc.toArray(); if (!firstOrLastRecordList.isEmpty()) { _overwriteTime = ((ObjectId) firstOrLastRecordList.iterator().next().get("_id")).getTime(); } else { // No records, use lastRunTime_ as backup _overwriteTime = _resultObj.getDate(CustomMapReduceJobPojo.lastRunTime_, new Date()).getTime(); } //TOTEST } //TESTED else { throw new MalformedURLException("Not recognized: " + url); } //TESTED (7.4) communityIdsStr = auth.getDomain(); ownerId = new ObjectId(auth.getUsername()); // Now do some authentication: // Check communities first since that involves no external DB queries: boolean isAuthorized = false; if (_isShare) { BasicDBList communities = (BasicDBList) _resultObj.get(SharePojo.communities_); for (Object communityObj : communities) { BasicDBObject communityDbo = (BasicDBObject) communityObj; ObjectId commId = communityDbo.getObjectId("_id"); if (communityIdsStr.contains(commId.toString())) { isAuthorized = true; break; } } } //TESTED (7.*) else { //_isCustom BasicDBList communities = (BasicDBList) _resultObj.get(CustomMapReduceJobPojo.communityIds_); for (Object communityObj : communities) { ObjectId commId = (ObjectId) communityObj; if (communityIdsStr.equals(commId)) { isAuthorized = true; break; } } } //TESTED (7.*) if (!isAuthorized) { // Still OK ... only if user is an admin isAuthorized = AuthUtils.isAdmin(ownerId); } //TESTED (1,2,3,4,5,6) if (!isAuthorized) { // Permission fail throw new MalformedURLException("Not found (or not authorized): " + url); } //TESTED (7.5) } catch (Exception e) { throw new MalformedURLException("Invalid authentication or location: " + e.getMessage()); } //(just passed exceptions on) // Save original URL if (_isShare) { // (custom handled above) if (!url.endsWith("/")) { _originalUrl = url + "/"; } else { _originalUrl = url; } } //(TESTED 1.3, 2.3, 3.3) }
From source file:com.ikanow.infinit.e.processing.generic.aggregation.AggregationManager.java
License:Open Source License
public static void updateDocEntitiesFromDeletedDocuments(String uuid) { String outCollection = new StringBuilder(uuid).append("_AggregationUtils").toString(); try {/*from w ww .j a v a 2s. c o m*/ PropertiesManager props = new PropertiesManager(); if (props.getAggregationDisabled()) { // (no need to do this) return; } DBCollection outColl = DbManager.getDB("doc_metadata").getCollection(outCollection); DBCursor dbc = outColl.find(); for (DBObject dbo : dbc) { BasicDBObject entityEl = (BasicDBObject) dbo; BasicDBObject entityVal = (BasicDBObject) entityEl.get("value"); long nDocDecrement = entityVal.getLong("dc"); long nFreqDecrement = entityVal.getLong("f"); long nCurrFreq = entityVal.getLong("tf"); long nCurrDocCount = entityVal.getLong("tdc"); // (These are by construction the lowest values so this will provide some defence against going -ve) if (nDocDecrement > nCurrDocCount) { nDocDecrement = nCurrDocCount; } if (nFreqDecrement > nCurrFreq) { nFreqDecrement = nCurrFreq; } BasicDBObject entityId = (BasicDBObject) entityEl.get("_id"); ObjectId commId = null; Object commObj = entityId.get("comm"); if (commObj instanceof ObjectId) { commId = entityId.getObjectId("comm"); } String index = (String) entityId.get("index"); if ((null == index) || (null == commId)) { continue; // random error } BasicDBObject updateQuery = new BasicDBObject(EntityFeaturePojo.index_, index); updateQuery.put(EntityFeaturePojo.communityId_, commId); BasicDBObject entityUpdate1 = new BasicDBObject(EntityFeaturePojo.doccount_, -nDocDecrement); entityUpdate1.put(EntityFeaturePojo.totalfreq_, -nFreqDecrement); BasicDBObject entityUpdate = new BasicDBObject(DbManager.inc_, entityUpdate1); if (_diagnosticMode) { if (_logInDiagnosticMode) System.out.println("UPDATE FEATURE DATABASE: " + updateQuery.toString() + "/" + entityUpdate.toString()); } else { DbManager.getFeature().getEntity().update(updateQuery, entityUpdate); // (can be a single query because the query is on index, the shard) } //TESTED if ((nDocDecrement < nCurrDocCount) && (nDocDecrement * 10 > nCurrDocCount)) { // ie there are some documents left // and the doc count has shifted by more than 10% BasicDBObject updateQuery2 = new BasicDBObject(EntityPojo.docQuery_index_, index); updateQuery2.put(DocumentPojo.communityId_, commId); BasicDBObject entityUpdate2_1 = new BasicDBObject(EntityPojo.docUpdate_doccount_, nCurrDocCount - nDocDecrement); entityUpdate2_1.put(EntityPojo.docUpdate_totalfrequency_, nCurrFreq - nFreqDecrement); BasicDBObject entityUpdate2 = new BasicDBObject(DbManager.set_, entityUpdate2_1); if (_diagnosticMode) { if (_logInDiagnosticMode) System.out.println("UPDATE DOC DATABASE: " + updateQuery2.toString() + "/" + entityUpdate2.toString()); } else { DbManager.getDocument().getMetadata().update(updateQuery2, entityUpdate2, false, true); } } } //TESTED (including when to update logic above) } catch (Exception e) { e.printStackTrace(); } // Tidy up DbManager.getDB("doc_metadata").getCollection(outCollection).drop(); }
From source file:com.images3.data.impl.ImagePlantAccessImplMongoDB.java
License:Apache License
private List<ImagePlantOS> getAllImagePlants(Page pageCursor) { DBCollection coll = getDatabase().getCollection("ImagePlant"); int skipRecords = (pageCursor.getStart() - 1) * pageCursor.getSize(); List<DBObject> objects = coll.find().skip(skipRecords).limit(pageCursor.getSize()).toArray(); List<ImagePlantOS> imagePlants = new ArrayList<ImagePlantOS>(objects.size()); for (DBObject obj : objects) { imagePlants.add(getObjectMapper().mapToImagePlantOS((BasicDBObject) obj)); }//from w ww. j a v a 2s .c o m return imagePlants; }
From source file:com.imaginea.mongodb.services.GridFSServiceImpl.java
License:Apache License
/** * Service implementation for getting the list of files stored in GridFS of specified database. * * @param dbName Name of Database/* ww w. j a v a 2s . co m*/ * @param bucketName Name of GridFS Bucket * @returns JSON representation of list of all files as a String. */ public ArrayList<DBObject> getFileList(String dbName, String bucketName) throws ValidationException, DatabaseException, CollectionException { mongoInstance = mongoInstanceProvider.getMongoInstance(); if (dbName == null) { throw new EmptyDatabaseNameException("Database Name Is Null"); } if (dbName.equals("")) { throw new EmptyDatabaseNameException("Database Name Empty"); } ArrayList<DBObject> fileList = new ArrayList<DBObject>(); try { if (!mongoInstance.getDatabaseNames().contains(dbName)) { throw new UndefinedDatabaseException( "Database with dbName [ " + dbName + "] does not exist"); } GridFS gridFS = new GridFS(mongoInstance.getDB(dbName), bucketName); Field field = GridFS.class.getDeclaredField("_filesCollection"); field.setAccessible(true); DBCollection filesCollection = (DBCollection) field.get(gridFS); DBCursor cursor = filesCollection.find().sort(new BasicDBObject("uploadDate", -1)); Iterator<DBObject> it = cursor.iterator(); while (it.hasNext()) { fileList.add(it.next()); } } catch (Exception m) { CollectionException e = new CollectionException(ErrorCodes.GET_COLLECTION_LIST_EXCEPTION, "GET_FILES_LIST_EXCEPTION", m.getCause()); throw e; } return fileList; }
From source file:com.jaspersoft.mongodb.importer.MongoDbImporter.java
License:Open Source License
public void validate(String tableName) { DBCollection collection = mongodbConnection.getMongoDatabase().getCollection(tableName); long size = collection.getCount(); if (size == 0) { logger.error("No data in Mongo database"); return;//from w ww .ja v a 2 s .c om } logger.info("Elements in collection: " + size); logger.info("Validating the first 5 entries"); DBCursor cursor = collection.find().limit(5); DBObject object; Object value; logger.info("---------------"); while (cursor.hasNext()) { object = cursor.next(); for (String id : object.keySet()) { value = object.get(id); logger.info(value + " -> " + value.getClass().getName()); } logger.info("---------------"); } }
From source file:com.javamongodb.application.RecordsActivity.java
private void getRecordsOnTable() { try {// w ww. j a va 2s .c o m logger.debug("Opening database connection"); DBCollection collection = DatabaseUtils.openDBConnection(); DBCursor cursor = collection.find(); while (cursor.hasNext()) { DBObject doc = cursor.next(); Object[] data = new Object[] { doc.get("FirstName"), doc.get("MiddleName"), doc.get("LastName"), doc.get("Gender"), doc.get("City"), doc.get("Street"), doc.get("BlockNumber"), doc.get("Country"), doc.get("EmailAddress"), doc.get("MobileNumber"), doc.get("HomeContact") }; tableModel.addRow(data); } jtable.getModel().addTableModelListener(this); jScrollPane = new JScrollPane(jtable); logger.debug("Displaying objects succeeded"); } catch (Exception exception) { logger.error("Displaying objects failed:\n" + exception.getMessage()); JOptionPane.showMessageDialog(null, "Database Error!"); } finally { logger.debug("Closing database connection"); DatabaseUtils.closeDBConnection(); } }
From source file:com.malsolo.mongodb.humongous.driver.Main.java
public static void main(String... args) throws UnknownHostException { MongoClient mongoClient = new MongoClient(); DB db = mongoClient.getDB("news"); Set<String> colls = db.getCollectionNames(); for (String s : colls) { System.out.println(s);/* www .j a va 2 s. c o m*/ } DBCollection coll = db.getCollection("article"); BasicDBObject doc = new BasicDBObject("authorId", UUID.randomUUID()).append("author", "Driver") .append("date", new Date()).append("title", "Title"); coll.insert(doc); DBObject myDoc = coll.findOne(); System.out.println(myDoc); System.out.println(coll.getCount()); try (DBCursor cursor = coll.find()) { while (cursor.hasNext()) { System.out.println(cursor.next()); } } }
From source file:com.mebigfatguy.mongobrowser.dialogs.MongoDataPanel.java
License:Apache License
/** * installs the event listeners for the components on this panel *//*from w w w. j a va 2 s . co m*/ private void initListeners() { final JPopupMenu menu = new JPopupMenu(); tree.addMouseListener(new MouseAdapter() { @Override public void mouseClicked(MouseEvent e) { showPopup(e); } @Override public void mousePressed(MouseEvent e) { showPopup(e); } @Override public void mouseReleased(MouseEvent e) { showPopup(e); } private void showPopup(MouseEvent e) { int x = e.getX(); int y = e.getY(); if (e.isPopupTrigger()) { menu.removeAll(); TreePath path = tree.getPathForLocation(x, y); if (path == null) { menu.add(newCollectionItem); menu.show(tree, x, y); } else { MongoTreeNode node = (MongoTreeNode) path.getLastPathComponent(); context.setSelectedNode(node); if (node.getType() == MongoTreeNode.Type.Collection) { if (!node.isReadOnly()) { menu.add(newObjectItem); menu.show(tree, x, y); } } else if (node.getType() == MongoTreeNode.Type.Object) { if (!node.isReadOnly()) { menu.add(newKeyValueItem); menu.addSeparator(); menu.add(deleteItem); menu.show(tree, x, y); } } else if (node.getType() == MongoTreeNode.Type.KeyValue) { if (!node.isReadOnly()) { MongoTreeNode.KV kv = (MongoTreeNode.KV) node.getUserObject(); Object value = kv.getValue(); boolean needsSeparator = false; if (value instanceof DBObject) { menu.add(newKeyValueItem); needsSeparator = true; } if (!kv.getKey().startsWith("_")) { if (needsSeparator) { menu.addSeparator(); } menu.add(deleteItem); menu.show(tree, x, y); } } } } } } }); tree.addTreeWillExpandListener(new TreeWillExpandListener() { @Override public void treeWillExpand(TreeExpansionEvent event) throws ExpandVetoException { MongoTreeNode node = (MongoTreeNode) event.getPath().getLastPathComponent(); MongoTreeNode slug = (MongoTreeNode) node.getFirstChild(); if (slug.getType() == MongoTreeNode.Type.ExpansionSlug) { node.removeAllChildren(); switch (node.getType()) { case Collection: { DBCollection collection = (DBCollection) node.getUserObject(); DBCursor cursor = collection.find(); while (cursor.hasNext()) { DBObject obj = cursor.next(); MongoTreeNode objNode = new MongoTreeNode(obj, node.isReadOnly()); node.add(objNode); slug = new MongoTreeNode(); objNode.add(slug); } } break; case Object: { DBObject object = (DBObject) node.getUserObject(); for (String key : object.keySet()) { Object value = object.get(key); MongoTreeNode kv = new MongoTreeNode(new MongoTreeNode.KV(key, value), node.isReadOnly()); node.add(kv); if (value instanceof DBObject) { slug = new MongoTreeNode(); kv.add(slug); } } } break; case KeyValue: { MongoTreeNode.KV topKV = (MongoTreeNode.KV) node.getUserObject(); DBObject object = (DBObject) topKV.getValue(); for (String key : object.keySet()) { Object value = object.get(key); MongoTreeNode kv = new MongoTreeNode(new MongoTreeNode.KV(key, value), node.isReadOnly()); node.add(kv); if (value instanceof DBObject) { slug = new MongoTreeNode(); kv.add(slug); } } } break; } } DefaultTreeModel model = (DefaultTreeModel) tree.getModel(); model.nodeStructureChanged(node); } @Override public void treeWillCollapse(TreeExpansionEvent event) throws ExpandVetoException { MongoTreeNode node = (MongoTreeNode) event.getPath().getLastPathComponent(); node.removeAllChildren(); MongoTreeNode slug = new MongoTreeNode(); node.add(slug); DefaultTreeModel model = (DefaultTreeModel) tree.getModel(); model.nodeStructureChanged(node); } }); tree.addTreeSelectionListener(new TreeSelectionListener() { @Override public void valueChanged(TreeSelectionEvent tse) { TreePath path = tse.getNewLeadSelectionPath(); if (path != null) { MongoTreeNode node = (MongoTreeNode) path.getLastPathComponent(); context.setSelectedNode(node); } else { context.setSelectedNode(null); } } }); }
From source file:com.miya.twit.mongodb.DBConnectSentiment.java
public static Polarity getPolarityWithWordType(String polarWord, int wordTypeValue) { try {// w w w .j a va2 s .com DBCollection collection = dbConnection(); if (collection != null) { DBCursor cursor = collection.find(); BasicDBObject query = new BasicDBObject(); query.put("text", polarWord); // query.put("rootType", wordTypeValue); //db.users.findOne({"username" : {$regex : ".*son.*"}}); cursor = collection.find(query); int polarValue; while (cursor.hasNext()) { polarValue = Integer.parseInt(cursor.next().get("polarity").toString()); // System.out.println(polarValue); return Polarity.getPolarity(polarValue); } } } catch (MongoException e) { System.out.println("dbConnectionForExpression hatas : " + e.getCause() + " -- " + e.getMessage()); e.printStackTrace(); return null; } return Polarity.getPolarity(0); }
From source file:com.miya.twit.mongodb.DBConnectSentiment.java
public static List<Expressions> getPolarityOfExpression(String polarWord) { DBCollection collection = dbConnectionForExpression(); if (collection != null) { DBCursor cursor = collection.find(); BasicDBObject query = new BasicDBObject(); query.put("text", polarWord); // query.put("rootType", wordTypeValue); //db.users.findOne({"username" : {$regex : ".*son.*"}}); cursor = collection.find(query); int polarValue; List<Expressions> listExpression = new ArrayList<Expressions>(); Expressions exp = null;/* w w w .jav a 2 s .co m*/ while (cursor.hasNext()) { exp = new Expressions(); exp.setExpression(cursor.next().get("text").toString()); exp.setPolarity(Polarity.getPolarity(Integer.parseInt(cursor.next().get("polarity").toString()))); listExpression.add(exp); } return listExpression; } return null; }