List of usage examples for com.mongodb Mongo Mongo
@Deprecated
public Mongo()
From source file:examples.QuickTour.java
License:Apache License
public static void main(String[] args) throws Exception { // connect to the local database server Mongo m = new Mongo(); // get handle to "mydb" DB db = m.getDB("mydb"); // Authenticate - optional boolean auth = db.authenticate("foo", new char[] { 'b', 'a', 'r' }); // get a list of the collections in this database and print them out Set<String> colls = db.getCollectionNames(); for (String s : colls) { System.out.println(s);//from w w w.ja v a 2s. co m } // get a collection object to work with DBCollection coll = db.getCollection("testCollection"); // drop all the data in it coll.drop(); // make a document and insert it BasicDBObject doc = new BasicDBObject(); doc.put("name", "MongoDB"); doc.put("type", "database"); doc.put("count", 1); BasicDBObject info = new BasicDBObject(); info.put("x", 203); info.put("y", 102); doc.put("info", info); coll.insert(doc); // get it (since it's the only one in there since we dropped the rest earlier on) DBObject myDoc = coll.findOne(); System.out.println(myDoc); // now, lets add lots of little documents to the collection so we can explore queries and cursors for (int i = 0; i < 100; i++) { coll.insert(new BasicDBObject().append("i", i)); } System.out .println("total # of documents after inserting 100 small ones (should be 101) " + coll.getCount()); // lets get all the documents in the collection and print them out DBCursor cur = coll.find(); while (cur.hasNext()) { System.out.println(cur.next()); } // now use a query to get 1 document out BasicDBObject query = new BasicDBObject(); query.put("i", 71); cur = coll.find(query); while (cur.hasNext()) { System.out.println(cur.next()); } // now use a range query to get a larger subset query = new BasicDBObject(); query.put("i", new BasicDBObject("$gt", 50)); // i.e. find all where i > 50 cur = coll.find(query); while (cur.hasNext()) { System.out.println(cur.next()); } // range query with multiple contstraings query = new BasicDBObject(); query.put("i", new BasicDBObject("$gt", 20).append("$lte", 30)); // i.e. 20 < i <= 30 cur = coll.find(query); while (cur.hasNext()) { System.out.println(cur.next()); } // create an index on the "i" field coll.createIndex(new BasicDBObject("i", 1)); // create index on "i", ascending // list the indexes on the collection List<DBObject> list = coll.getIndexInfo(); for (DBObject o : list) { System.out.println(o); } // See if the last operation had an error System.out.println("Last error : " + db.getLastError()); // see if any previous operation had an error System.out.println("Previous error : " + db.getPreviousError()); // force an error db.forceError(); // See if the last operation had an error System.out.println("Last error : " + db.getLastError()); db.resetError(); }
From source file:examples.QuickTourAdmin.java
License:Apache License
public static void main(String[] args) throws Exception { // connect to the local database server Mongo m = new Mongo(); // Authenticate - optional // boolean auth = db.authenticate("foo", "bar"); // get db names for (String s : m.getDatabaseNames()) { System.out.println(s);/*from www .ja v a2s . c o m*/ } // get a db DB db = m.getDB("com_mongodb_MongoAdmin"); // do an insert so that the db will really be created. Calling getDB() doesn't really take any // action with the server db.getCollection("testcollection").insert(new BasicDBObject("i", 1)); for (String s : m.getDatabaseNames()) { System.out.println(s); } // drop a database m.dropDatabase("com_mongodb_MongoAdmin"); for (String s : m.getDatabaseNames()) { System.out.println(s); } }
From source file:it.sayservice.platform.smartplanner.utils.CacheGenerator.java
License:Apache License
public static void main(String args[]) throws Exception { // protected void setUp() throws Exception { // System.out.println(System.currentTimeMillis() - 1000 * 60 * 60 * 3); // System.out.println(System.currentTimeMillis() + 1000 * 60 * 60 * 3); // System.out.println(System.currentTimeMillis()); // System.out.println(System.currentTimeMillis() + RecurrentUtil.DAY - // 1000 * 60); // System.exit(0); // super.setUp(); handler = new OTPHandler(router, "http://127.0.0.1:7575"); MongoTemplate template = new MongoTemplate(new Mongo(), router); storage = new OTPStorage(template); mongoRouterMapper = new MongoRouterMapper(template, router); configurationManager = new ConfigurationManager(router); manager = new OTPManager(handler, storage, mongoRouterMapper, configurationManager); manager.preinit(true);/* w w w .j av a 2 s. c o m*/ planner = new PlannerCtrl(); manager.init(router); }
From source file:jahspotify.storage.statistics.MongoDBHistoricalStorage.java
License:Apache License
@PostConstruct public void initialize() { try {//from w w w . j a v a 2s.c om _mongoDBInstance = new Mongo(); _db = _mongoDBInstance.getDB(_dbName); } catch (Exception e) { } }
From source file:logica.MovieSessionBean.java
@PostConstruct private void initDB() { Mongo m;/* ww w .j a va2 s.c o m*/ try { m = new Mongo(); DB db = m.getDB("movieDB"); movieColl = db.getCollection("movies"); if (movieColl == null) { movieColl = db.createCollection("movies", null); } } catch (UnknownHostException e) { Logger.getLogger(MovieSessionBean.class.getName()).log(Level.SEVERE, null, e); } }
From source file:Main.Main.java
public static void main(String[] args) { GestionCommande gestionCommande = new GestionCommande(); int typeProduit = 1; int nombreProduits = 5; gestionCommande.creerCommande(typeProduit, nombreProduits); try {/*from w w w .j a va 2 s . c om*/ MongoOperations mongoOps = new MongoTemplate(new Mongo(), "gestionDeCommande"); mongoOps.insert(new Person("Joe", 36)); log.info(mongoOps.findOne(new Query(Criteria.where("name").is("Joe")), Person.class)); } catch (UnknownHostException ex) { log.error(ex.getMessage()); } }
From source file:mongo.Application.java
License:Open Source License
public static void main(String[] args) throws Exception { Mongo m = new Mongo(); MongoTemplate mt = new MongoTemplate(m, "helloKitty"); MongoConnector defaultConnector = new MongoConnector(mt); mt.dropCollection(Person.class); mt.dropCollection(Thing.class); mt.dropCollection(Address.class); mt.dropCollection(Role.class); ThingReaders tr = new ThingReaders(); tr.addReader("person/*", defaultConnector); tr.addReader("role/*", defaultConnector); tr.addReader("address/*", defaultConnector); ThingWriters tw = new ThingWriters(); tw.addWriter("person/*", defaultConnector); tw.addWriter("role/*", defaultConnector); tw.addWriter("address/*", defaultConnector); ValidatorFactory factory = Validation.buildDefaultValidatorFactory(); Validator validator = factory.getValidator(); ThingControl tc = new ThingControl(); tc.setThingReaders(tr);// w w w. ja va 2s . com tc.setThingWriters(tw); tc.setValidator(validator); Person user = new Person(); user.setFirstName("Person"); user.setLastName("Name"); Thing<Person> pt = tc.createThing("username", user); Address address = new Address(); address.setCity("Auckland"); address.setCountry("NZ"); address.setNr(1); address.setStreet("Fleet street"); Thing<Address> at = tc.createThing("home", address); tc.addChildThing(pt, at); Object id = pt.getId(); System.out.println("ID: " + id); Role role1 = new Role("role1"); Thing<Role> r1t = tc.createThing("group_1", role1); Role role2 = new Role("role2"); Thing<Role> r2t = tc.createThing("group_1", role2); Role role3 = new Role("role3"); Thing<Role> r3t = tc.createThing("group_2", role3); tc.addChildThing(pt, r1t); tc.addChildThing(pt, r2t); tc.addChildThing(pt, r3t); Observable<? extends Thing<?>> childs = tc.observeChildrenMatchingTypeAndKey(pt, "role", "*2*", true); childs.toBlockingObservable().forEach(t -> System.out.println(t)); Observable<? extends Thing<?>> childs2 = tc.observeChildrenMatchingTypeAndKey(pt, "address", "*", true); childs2.toBlockingObservable().forEach(t -> System.out.println(t)); }
From source file:mongodb.Movies_import.java
public static void main(String[] args) throws UnknownHostException, MongoException, FileNotFoundException, IOException { Mongo mongo = new Mongo(); //creating an instance of mongodb called mongo //using mongo object to get the database name List<String> databases = mongo.getDatabaseNames(); for (String string : databases) { System.out.println(string); }//from ww w .j av a 2 s .co m //assigning db variable to mongoDB directory 'db' created in the terminal DB db = mongo.getDB("db"); DBCollection Collection = db.getCollection("movies_import"); FileReader filereader = null; BufferedReader bufferreader = null; try { filereader = new FileReader("/Users/cheryldsouza/Documents/UPITT/Data Analytics/ml-10M100K/movies.dat"); bufferreader = new BufferedReader(filereader); System.out.println("Mongodb Files"); String read = bufferreader.readLine(); while (read != null) { System.out.println(read); String[] reads = read.split("::"); BasicDBObject object = new BasicDBObject(); object.append("MovieID", reads[0]); object.append("Title", reads[1]); object.append("Genres", reads[2]); Collection.insert(object); read = bufferreader.readLine(); } } catch (FileNotFoundException e) { System.out.println("Documents not found"); System.exit(-1); } catch (IOException e) { System.out.println("FAILED"); System.exit(-1); } }
From source file:mongodb.question2.java
public static void main(String[] args) throws UnknownHostException, MongoException { Mongo mongoclient = new Mongo(); DB db = mongoclient.getDB("db"); DBCollection Collection = db.getCollection("tags_import"); //Need to enter the userid System.out.println("Enter the User Id here"); Scanner userid = new Scanner(System.in); String UserId = userid.next(); BasicDBObject user = new BasicDBObject(); user.put("UserID", UserId); DBCursor usercursor = Collection.find(); usercursor = Collection.find(user); while (usercursor.hasNext()) { DBObject object = usercursor.next(); String MovieID = (String) object.get("MovieID"); System.out.println("MovieID is " + MovieID); //for each Movie Id find the target id BasicDBObject targetid = new BasicDBObject(); targetid.put("MovieID", MovieID); DBCursor moviecursor = Collection.find(); moviecursor = Collection.find(targetid); int count = moviecursor.count(); System.out.println("This movie has been rated " + count + " users"); System.out.println(count); while (moviecursor.hasNext()) { DBObject object2 = moviecursor.next(); String folluserID = (String) object2.get("UserID"); System.out.println("The following Users are familiar to the given user " + folluserID); }/*w w w.j a v a2 s. com*/ System.out.println(); } }
From source file:mongodb.tagsimport.java
public static void main(String[] args) throws UnknownHostException, MongoException { Mongo mongo = new Mongo(); List<String> databases = mongo.getDatabaseNames(); for (String str : databases) { System.out.println(str);/*from w w w . j a v a 2 s.c o m*/ } DB db = mongo.getDB("db"); DBCollection dbCollection = db.getCollection("tags_import"); FileReader filereader = null; BufferedReader bufferreader = null; try { filereader = new FileReader("/Users/cheryldsouza/Documents/UPITT/Data Analytics/ml-10M100K/tags.dat"); bufferreader = new BufferedReader(filereader); System.out.println("Mongodb Tags File"); String read = bufferreader.readLine(); while (read != null) { System.out.println(read); String[] reads = read.split("::"); //inserting keys BasicDBObject object = new BasicDBObject(); //UserID::MovieID::Tag::Timestamp object.append("UserID", reads[0]); object.append("MovieID", reads[1]); object.append("Tag", reads[2]); object.append("Timestamp", reads[3]); dbCollection.insert(object); read = bufferreader.readLine(); } } catch (FileNotFoundException e) { System.out.println("No documents found. Please try again"); System.exit(-1); } catch (IOException e) { System.out.println("FAILED"); System.exit(-1); } }