List of usage examples for com.mongodb DBCollection insert
public WriteResult insert(final List<? extends DBObject> documents)
From source file:com.softlyinspired.jlw.script.validationScript.java
License:Open Source License
/** * write new script to mongodb given full details * @param scriptId/*from ww w . j a va 2 s. c o m*/ * @param scriptText * @param scriptCategory * @param scriptTitle * @param scriptConnection * @throws UnknownHostException */ private void writeNewScript(int scriptId, String scriptText, String scriptCategory, String scriptTitle, String scriptConnection) throws UnknownHostException { DBCollection coll = repoConnection.getScriptCollection(); BasicDBObject query = new BasicDBObject(); query.put("scriptId", scriptId); query.put("scriptText", scriptText); query.put("scriptTitle", scriptTitle); query.put("ScriptCategory", scriptCategory); query.put("scriptConnection", scriptConnection); try { coll.insert(query); } catch (Exception e) { JLWUtilities.scriptErrorMessage("Error creating script"); } }
From source file:com.spring.tutorial.dropbox.DropBoxAuth.java
public void fetchFilesAndFolders(String id, DbxClient client, String path) throws DbxException, UnknownHostException, Exception { MongoData mongoData = new MongoData(); DB db = mongoData.getDB();/* w ww. j a v a 2 s . c o m*/ DBCollection collection = db.getCollection(id + "_dropbox_files_meta"); DBCursor cursor = collection.find(); DbxEntry.WithChildren listing = client.getMetadataWithChildren(path); for (DbxEntry child : listing.children) { String fileInfo = child.toString(); DropboxEntity entity; if (child.isFile()) { String rev = fileInfo.substring(fileInfo.indexOf("rev=") + 5, fileInfo.indexOf(")") - 1); String fileSize = fileInfo.substring(fileInfo.indexOf("humanSize=") + 11, fileInfo.indexOf("\", lastModified")); String fileLastModified = fileInfo.substring(fileInfo.indexOf("lastModified=") + 14, fileInfo.indexOf("\", clientMtime")); entity = new DropboxEntity(child.name, "file", fileSize, fileLastModified, child.path, rev); DBObject document = new BasicDBObject(); document.put("rev", rev); document.put("fileSize", fileSize); document.put("lastModified", fileLastModified); document.put("name", child.name); document.put("path", child.path); document.put("type", "file"); cursor = collection.find(); boolean found = false; while (cursor.hasNext()) { DBObject doc = cursor.next(); String path1 = (String) doc.get("path"); if (path1.equals(child.path)) { found = true; } } if (found) { document.put("found", "true"); BasicDBObject newDocument = new BasicDBObject(); newDocument.append("$set", document); BasicDBObject searchQuery = new BasicDBObject().append("path", child.path); collection.update(searchQuery, newDocument); } else { document.put("tags", ""); document.put("description", ""); document.put("found", "false"); collection.insert(document); } } else { entity = new DropboxEntity(child.name, "folder", child.path); DBObject document = new BasicDBObject(); document.put("rev", ""); document.put("fileSize", ""); document.put("lastModified", ""); document.put("name", child.name); document.put("path", child.path); document.put("type", "folder"); cursor = collection.find(); boolean found = false; if (cursor.hasNext()) { while (cursor.hasNext()) { DBObject doc = cursor.next(); String path1 = (String) doc.get("path"); if (path1.equals(child.path)) { found = true; } } } if (found) { document.put("found", "true"); BasicDBObject newDocument = new BasicDBObject(); newDocument.append("$set", document); BasicDBObject searchQuery = new BasicDBObject().append("path", child.path); collection.update(searchQuery, newDocument); } else { document.put("tags", ""); document.put("description", ""); document.put("found", "false"); collection.insert(document); } fetchFilesAndFolders(id, client, child.path); } } }
From source file:com.spring.tutorial.entitites.DropboxUploader.java
public String upload() throws IOException, ServletException, FacebookException { OutputStream output = null;//from w w w .j av a2 s . co m InputStream fileContent = null; final Part filePart; final File file; try { filePart = request.getPart("file"); fileContent = filePart.getInputStream(); MongoClient mongoClient = new MongoClient(); mongoClient = new MongoClient(); DB db = mongoClient.getDB("fou"); char[] pass = "mongo".toCharArray(); boolean auth = db.authenticate("admin", pass); file = File.createTempFile("fileToStore", "tmp"); file.deleteOnExit(); FileOutputStream fout = new FileOutputStream(file); int read = 0; byte[] bytes = new byte[1024]; while ((read = fileContent.read(bytes)) != -1) { fout.write(bytes, 0, read); } FileInputStream inputStream = new FileInputStream(file); try { String fileName = filePart.getSubmittedFileName(); String path = request.getParameter("path"); if (path.equals("/home")) { path = ""; } DbxEntry.File uploadedFile = client.uploadFile(path + "/" + filePart.getSubmittedFileName(), DbxWriteMode.add(), file.length(), inputStream); DBCollection collection = db .getCollection(request.getSession().getAttribute("id") + "_dropbox_files_meta"); DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss"); Date date = new Date(); long fileSize = filePart.getSize(); BasicDBObject document = new BasicDBObject(); document.put("rev", ""); document.put("fileSize", Long.toString(fileSize)); document.put("lastModified", dateFormat.format(date)); document.put("name", filePart.getSubmittedFileName()); document.put("path", path + "/" + filePart.getSubmittedFileName()); document.put("type", "file"); document.put("tags", request.getParameter("tags")); document.put("description", request.getParameter("description")); document.put("found", "true"); collection.insert(document); } finally { inputStream.close(); } } catch (Exception e) { return "message:" + e.getMessage(); } finally { if (output != null) { output.close(); } if (fileContent != null) { fileContent.close(); } } return "success"; }
From source file:com.spring.tutorial.entitites.FileUploader.java
public String upload() throws IOException, ServletException, FacebookException { OutputStream output = null;//from www. j a v a 2 s. c o m InputStream fileContent = null; final Part filePart; final File file; try { filePart = request.getPart("file"); fileContent = filePart.getInputStream(); MongoClient mongoClient = new MongoClient(); mongoClient = new MongoClient(); DB db = mongoClient.getDB("fou"); char[] pass = "mongo".toCharArray(); boolean auth = db.authenticate("admin", pass); file = File.createTempFile("fileToStore", "tmp"); file.deleteOnExit(); FileOutputStream fout = new FileOutputStream(file); int read = 0; byte[] bytes = new byte[1024]; while ((read = fileContent.read(bytes)) != -1) { fout.write(bytes, 0, read); } GridFS gridFS = new GridFS(db, request.getSession().getAttribute("id") + "_files"); GridFSInputFile gfsInputFile = gridFS.createFile(file); gfsInputFile.setFilename(filePart.getSubmittedFileName()); gfsInputFile.save(); DBCollection collection = db.getCollection(request.getSession().getAttribute("id") + "_files_meta"); BasicDBObject metaDocument = new BasicDBObject(); metaDocument.append("name", filePart.getSubmittedFileName()); metaDocument.append("size", filePart.getSize()); metaDocument.append("content-type", filePart.getContentType()); metaDocument.append("file-id", gfsInputFile.getId()); metaDocument.append("tags", request.getParameter("tags")); metaDocument.append("description", request.getParameter("description")); DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss"); metaDocument.append("last_modified", dateFormat.format(new Date())); collection.insert(metaDocument); } catch (Exception e) { return "message:" + e.getMessage(); } finally { if (output != null) { output.close(); } if (fileContent != null) { fileContent.close(); } } return "success"; }
From source file:com.spring.tutorial.mongo.MongoDB.java
public boolean registerUser() { if (userExist()) { try {/*w w w. j a v a 2 s .c o m*/ BasicDBObject document = new BasicDBObject(); document.append("username", user.getUsername()); document.append("email", user.getEmail()); document.append("password", user.getPassword()); document.append("dropbox_token", user.getDropboxAccessToken()); document.append("dropbox_hash", ""); if (user.getId().equals("none")) { String id = Long.toString(System.currentTimeMillis()); document.append("id", id); user.setId(id); } else { document.append("id", user.getId()); } DBCollection collection = db.getCollection("users"); collection.insert(document); return true; } catch (Exception e) { return false; } } return true; }
From source file:com.springsource.html5expense.mongodb.services.DataInitalizer.java
License:Apache License
@PostConstruct private void init() { mongoTemplate.getCollection("ExpenseType").drop(); mongoTemplate.getCollection("Expense").drop(); DBCollection expenseTypeCollection = mongoTemplate.getCollection("ExpenseType"); BasicDBObject expenseTypeDoc = new BasicDBObject(); expenseTypeDoc.put("_id", new Long(1)); expenseTypeDoc.put("expenseTypeId", new Long(1)); expenseTypeDoc.put("name", "GYM"); expenseTypeCollection.insert(expenseTypeDoc); expenseTypeDoc.clear();//from w w w . j a va 2s. c om expenseTypeDoc.put("_id", new Long(2)); expenseTypeDoc.put("expenseTypeId", new Long(2)); expenseTypeDoc.put("name", "TELEPHONE"); expenseTypeCollection.insert(expenseTypeDoc); expenseTypeDoc.clear(); expenseTypeDoc.put("_id", new Long(3)); expenseTypeDoc.put("expenseTypeId", new Long(3)); expenseTypeDoc.put("name", "MEDICARE"); expenseTypeCollection.insert(expenseTypeDoc); expenseTypeDoc.clear(); expenseTypeDoc.put("_id", new Long(4)); expenseTypeDoc.put("expenseTypeId", new Long(4)); expenseTypeDoc.put("name", "TRAVEL"); expenseTypeCollection.insert(expenseTypeDoc); }
From source file:com.sqm.dashboard.dao.impl.UserDaoImpl.java
License:Apache License
public boolean addUser(String firstname) throws UnknownHostException { // String passwordHash = makePasswordHash(password, // Integer.toString(random.nextInt())); BasicDBObject user = new BasicDBObject(); System.out.println("In USerDAO: " + firstname); user.append("firstname", firstname); try {//from w ww .j a v a2s . c om final DBCollection usersCollection; final MongoClient mongoClient = new MongoClient(new MongoClientURI("mongodb://localhost")); final DB blogDatabase = mongoClient.getDB("blog"); usersCollection = blogDatabase.getCollection("user"); usersCollection.insert(user); return true; } catch (MongoException.DuplicateKey e) { System.out.println("Username already in use: " + firstname); return false; } }
From source file:com.stratio.tests.ATExampleMongoDB.java
License:Apache License
@BeforeClass public void setUp() throws UnknownHostException { MongoClient mongoClient = new MongoClient(mongoHost, mongoPort); mongoClient.dropDatabase(dataBase);/*from w w w .ja v a 2 s .c o m*/ DB db = mongoClient.getDB(dataBase); DBCollection tabletest = db.getCollection("tabletest"); // DBCollection tabletest = db.createCollection("tabletest"); SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy"); format.setTimeZone(TimeZone.getTimeZone("CET")); for (int i = 0; i < 10; i++) { Date parsedDate = null; String fecha = i + "/" + i + "/200" + i; try { parsedDate = format.parse(fecha); } catch (ParseException e) { e.printStackTrace(); } System.out.println(new java.sql.Date(parsedDate.getTime()).toString()); BasicDBObjectBuilder documentBuilder = BasicDBObjectBuilder.start().add("ident", i) .add("name", "name_" + i).add("money", 10.2 + i).add("new", true) .add("date", new java.sql.Date(parsedDate.getTime())); tabletest.insert(documentBuilder.get()); } // DBObject aux = tabletest.findOne(); // java.sql.Date aux1 = (java.sql.Date)aux.get("date"); mongoClient.close(); String connector = "Mongo"; //Preparamos las cositas para compartir ThreadProperty.set("Host", "127.0.0.1"); ThreadProperty.set("Connector", connector); }
From source file:com.streamreduce.core.dao.GenericCollectionDAO.java
License:Apache License
public BasicDBObject createCollectionEntry(DAODatasourceType datasourceType, String collectionName, BasicDBObject payloadObject) {/* ww w .j av a2s . c o m*/ DB db = getDatabase(datasourceType); DBCollection collection = db.getCollection(collectionName); collection.insert(payloadObject); return payloadObject; }
From source file:com.streamreduce.core.service.UserServiceImpl.java
License:Apache License
/** * SOBA-1617 -- bootstrap the Metric collections with proper indexes * * @param account - a valid account/*ww w . ja v a 2 s.c o m*/ */ private void createMetricInbox(Account account) { // create a bogus object DB db = genericCollectionDAO.getDatabase(DAODatasourceType.MESSAGE); DBCollection collection = db.getCollection(MessageUtils.getMetricInboxPath(account)); BasicDBObject dummyObj = new BasicDBObject(); collection.insert(dummyObj); // add indexes collection.ensureIndex("metricGranularity"); collection.ensureIndex("metricName"); // remove bogus object collection.remove(dummyObj); }