List of usage examples for com.mongodb BasicDBObject append
@Override public BasicDBObject append(final String key, final Object val)
From source file:com.tomtom.speedtools.mongodb.MongoDBUpdate.java
License:Apache License
@Nonnull public DBObject toDBObject() throws MapperException { final DBObject result = new BasicDBObject(); for (final Modifier modifier : modifiers) { BasicDBObject existing = (BasicDBObject) result.get(modifier.operator); if (existing == null) { existing = new BasicDBObject(); }// www. j av a2 s . c om result.put(modifier.operator, existing.append(modifier.fieldName, modifier.toDBValue())); } return result; }
From source file:com.trenako.mapping.LocalizedFieldWriteConverter.java
License:Apache License
@Override public DBObject convert(LocalizedField<T> source) { BasicDBObject dbo = new BasicDBObject(); for (Map.Entry<String, T> entry : source.entrySet()) { dbo.append(entry.getKey(), entry.getValue()); }/*from w ww . j av a2 s. co m*/ return dbo; }
From source file:com.university.mongodb.courses.m101j.blogapplication.BlogPostDAO.java
License:Apache License
public String addPost(String title, String body, List<String> tags, String username) { System.out.println("inserting blog entry " + title + " " + body); String permalink = title.replaceAll("\\s", "_"); // whitespace becomes _ permalink = permalink.replaceAll("\\W", ""); // get rid of non // alphanumeric permalink = permalink.toLowerCase(); BasicDBObject post = new BasicDBObject(); // XXX HW 3.2, Work Here // Remember that a valid post has the following keys: // author, body, permalink, tags, comments, date ///*from w w w .j a v a2s .c o m*/ // A few hints: // - Don't forget to create an empty list of comments // - for the value of the date key, today's datetime is fine. // - tags are already in list form that implements suitable interface. // - we created the permalink for you above. // Build the post object and insert it post.append(POST_TITLE, title).append(POST_AUTHOR, username).append(POST_BODY, body) .append(POST_PERMALINK, permalink).append(POST_TAGS, tags) .append(POST_COMMENTS, Collections.EMPTY_LIST).append(POST_DATE, new Date()); try { postsCollection.insert(post); } catch (Exception e) { System.err.println("Unable to insert post... " + e); } return permalink; }
From source file:com.university.mongodb.courses.m101j.blogapplication.BlogPostDAO.java
License:Apache License
public void addPostComment(final String name, final String email, final String body, final String permalink) { // XXX HW 3.3, Work Here // Hints:/*from w w w .j av a 2 s. co m*/ // - email is optional and may come in NULL. Check for that. // - best solution uses an update command to the database and a suitable // operator to append the comment on to any existing list of comments DBObject post = findByPermalink(permalink); BasicDBObject comment = new BasicDBObject(); if (email != null && !email.equals("")) { comment.append(COMMENT_EMAIL, email); } comment.append(COMMENT_AUTHOR, name).append(COMMENT_BODY, body); // add comment BasicDBObject update = new BasicDBObject().append("$push", new BasicDBObject(POST_COMMENTS, comment)); try { postsCollection.update(post, update); } catch (Exception e) { e.printStackTrace(); } }
From source file:com.university.mongodb.courses.m101j.blogapplication.UserDAO.java
License:Apache License
public boolean addUser(String username, String password, String email) { String passwordHash = makePasswordHash(password, Integer.toString(random.nextInt())); // XXX WORK HERE // create an object suitable for insertion into the user collection // be sure to add username and hashed password to the document. problem // instructions // will tell you the schema that the documents must follow. BasicDBObject user = buildUserDocument("_id", username).append("password", passwordHash); if (email != null && !email.equals("")) { // XXX WORK HERE // if there is an email address specified, add it to the document // too./*from www.jav a 2 s.c om*/ user.append("email", email); } try { // XXX WORK HERE // insert the document into the user collection here usersCollection.insert(user); return true; } catch (MongoException.DuplicateKey e) { System.out.println("Username already in use: " + username); return false; } }
From source file:com.wincere.lamda.storm.bolt.CreateTable.java
License:Apache License
/** * Run this main method to see the output of this quick example. * * @param args takes no args/*from w w w . ja v a 2 s .c o m*/ * @throws UnknownHostException if it cannot connect to a MongoDB instance at localhost:27017 */ public void update(BasicDBObject doc, OutputCollector collector, Tuple input) throws UnknownHostException { // connect to the local database server MongoCredential credential = MongoCredential.createMongoCRCredential("superuser", "admin", "12345678".toCharArray()); try { MongoClient mongoClient = new MongoClient(new ServerAddress("172.16.1.171", 27017), Arrays.asList(credential)); // MongoClient mongoClient = new MongoClient("172.16.1.171",27017); /* // Authenticate - optional MongoCredential credential = MongoCredential.createMongoCRCredential(userName, database, password); MongoClient mongoClient = new MongoClient(new ServerAddress(), Arrays.asList(credential)); */ // get handle to "mydb" DB db = mongoClient.getDB("UCAPBatchTest"); // get a collection object to work with DBCollection coll = db.getCollection("Queries1"); // DBCollection status = db.getCollection("statustest1"); //DBCollection coll1 = db.getCollection("queryaudittest1"); // drop all the data in it //coll.drop(); //status.drop(); //coll1.drop(); /* status.insert(new BasicDBObject().append("queryStatus", "Open").append("QueryStatusID","1")); status.insert(new BasicDBObject().append("queryStatus", "Answered").append("QueryStatusID","2")); status.insert(new BasicDBObject().append("queryStatus", "Closed").append("QueryStatusID","3")); status.insert(new BasicDBObject().append("queryStatus", "Cancelled").append("QueryStatusID","4")); */ // make a document and insert it int count = 0; DateTimeFormatter formatter = DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ss"); try { //.equals("Open")?"1":(splitValue[5].equals("Answered")?"2":"3") BasicDBObject searchQuery = new BasicDBObject().append("queryRepeatKey", (String) doc.get("queryRepeatKey")); BasicDBObject newDocument = new BasicDBObject(); DBCursor cursor = coll.find(searchQuery); //DBObject result = cursor.next(); if (cursor.hasNext()) { DBObject result = cursor.next(); String queryValue = (String) result.get("queryValue"); String queryStatusID = (String) result.get("queryStatusID"); String queryResponse = (String) result.get("queryResponse"); String queryResolvedTimeStamp = (String) result.get("queryResolvedTimeStamp"); String queryAnsweredTimeStamp = (String) result.get("queryAnsweredTimeStamp"); String queryCreatedTimeStamp = (String) result.get("queryCreatedTimeStamp"); if (doc.get("queryValue").equals("\\N")) { doc.append("queryValue", queryValue); } if (doc.get("queryStatusID").equals("\\N")) { doc.append("queryStatusID", queryStatusID); } if (doc.get("queryResponse").equals("\\N")) { doc.append("queryResponse", queryResponse); } if (doc.get("queryResolvedTimeStamp").equals("\\N")) { doc.append("queryResolvedTimeStamp", queryResolvedTimeStamp); } if (doc.get("queryAnsweredTimeStamp").equals("\\N")) { doc.append("queryAnsweredTimeStamp", queryAnsweredTimeStamp); } doc.append("queryCreatedTimeStamp", queryCreatedTimeStamp); } if (doc.get("queryStatusID").equals("Open")) doc.append("queryCreatedTimeStamp", doc.get("queryCreatedTimeStamp")); //System.out.println(count); newDocument.append("$set", doc); try { coll.update(searchQuery, newDocument, true, true); } catch (MongoException me) { collector.fail(input); } // collector.ack(input); //coll.insert(doc); } catch (Exception e) { System.err.println("CSV file cannot be read : " + e); } //System.out.println(count); // lets get all the documents in the collection and print them out /*DBCursor cursor = coll1.find(); try { while (cursor.hasNext()) { System.out.println(cursor.next()); } } finally { cursor.close(); }*/ /* // now use a query to get 1 document out BasicDBObject query = new BasicDBObject("i", 71); cursor = coll.find(query); try { while (cursor.hasNext()) { System.out.println(cursor.next()); } } finally { cursor.close(); }*/ // release resources //db.dropDatabase(); mongoClient.close(); } catch (UnknownHostException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
From source file:com.zousu.mongopresser.Presser.java
License:Open Source License
public void press() { //get a list of all the tables and columns logger.info("Starting MySQL to Mongo Conversion..."); logger.info("Preparing Tables..."); mySQLHandler.initialiseDatabase();/*www. j ava 2 s . c om*/ List<Table> tables = mySQLHandler.getTableList(); for (int i = 0; i < tables.size(); i++) { Table table = tables.get(i); List<Column> columns = table.getColumns(); List<DBObject> dboList = new ArrayList<DBObject>(); SqlRowSet rs = mySQLHandler.selectAllFromTable(table.getTableName()); logger.info("Creating objects for " + table.getTableName() + "..."); while (rs.next()) { BasicDBObject dbo = new BasicDBObject(); for (int j = 0; j < columns.size(); j++) { Column col = columns.get(j); String colName = col.getColumnName(); switch (col.getType()) { case Types.INTEGER: case Types.BIGINT: dbo.append(colName, rs.getInt(colName)); break; case Types.DOUBLE: dbo.append(colName, rs.getDouble(colName)); break; case Types.DATE: dbo.append(colName, rs.getDate(colName)); break; default: dbo.append(colName, rs.getString(colName)); break; } } dboList.add(dbo); } //now insert it logger.info("Inserting " + dboList.size() + " mongo rows into " + table.getTableName() + "..."); table.setNumOfRows(dboList.size()); try { mongoHandler.createCollection(table.getTableName(), true); mongoHandler.batchInsert(dboList, table.getTableName()); assert (mongoHandler.getNumObjectsInCollection(table.getTableName()) == dboList.size()); logger.info(table.getTableName() + " DONE!"); } catch (CollectionExistException e) { e.printStackTrace(); } } logger.info(tables.size() + " collections added!"); //now check go get it from mongo and check if the data there is correct logger.info("Checking mongo consistency..."); for (int i = 0; i < tables.size(); i++) { Table table = tables.get(i); assert (mongoHandler.getNumObjectsInCollection(table.getTableName()) == table.getNumOfRows()); logger.info(table.getTableName() + " consistent!"); } logger.info("MySQL to Mongo Conversion Completed!!!!"); }
From source file:course.BlogPostDAO.java
License:Apache License
public String addPost(String title, String body, List tags, String username) { System.out.println("inserting blog entry " + title + " " + body); String permalink = title.replaceAll("\\s", "_"); // whitespace becomes _ permalink = permalink.replaceAll("\\W", ""); // get rid of non alphanumeric permalink = permalink.toLowerCase(); BasicDBObject post = new BasicDBObject(); // XXX HW 3.2, Work Here // Remember that a valid post has the following keys: // author, body, permalink, tags, comments, date ////w w w. j a v a2 s. c om // A few hints: // - Don't forget to create an empty list of comments // - for the value of the date key, today's datetime is fine. // - tags are already in list form that implements suitable interface. // - we created the permalink for you above. // - Build the post object and insert it post.append("title", title).append("body", body).append("author", username).append("tags", tags) .append("comments", new BasicDBList()).append("date", new Date()).append("permalink", permalink); postsCollection.insert(post); return permalink; }
From source file:course.BlogPostDAO.java
License:Apache License
public void addPostComment(final String name, final String email, final String body, final String permalink) { // XXX HW 3.3, Work Here // Hints:/*from w w w. java 2 s . com*/ // - email is optional and may come in NULL. Check for that. // - best solution uses an update command to the database and a suitable // operator to append the comment on to any existing list of comments BasicDBObject obj = new BasicDBObject().append("author", name).append("body", body).append("permalink", permalink); if (email != null && !email.equals("")) { obj.append("email", email); } postsCollection.update(new BasicDBObject("permalink", permalink), new BasicDBObject("$push", new BasicDBObject("comments", obj))); }
From source file:course.BlogPostDAO.java
License:Apache License
public String addPost(String title, String body, List tags, String username) { System.out.println("inserting blog entry " + title + " " + body); String permalink = title.replaceAll("\\s", "_"); // whitespace becomes _ permalink = permalink.replaceAll("\\W", ""); // get rid of non alphanumeric permalink = permalink.toLowerCase(); BasicDBObject post = new BasicDBObject(); // XXX HW 3.2, Work Here // Remember that a valid post has the following keys: // author, body, permalink, tags, comments, date ///*from w ww . ja v a 2 s. co m*/ // A few hints: // - Don't forget to create an empty list of comments // - for the value of the date key, today's datetime is fine. // - tags are already in list form that implements suitable interface. // - we created the permalink for you above. // Build the post object and insert it post.append("title", title).append("author", username).append("body", body).append("permalink", permalink) .append("tags", tags).append("comments", Collections.emptyList()).append("date", new Date()); postsCollection.insert(post); return permalink; }