Package com.mongodb

Examples of com.mongodb.Mongo


        }));
    }

    public static boolean isAvailable() {
        try {
            Mongo mongo = new Mongo(HOST, PORT);
            try {
                mongo.getDatabaseNames();
                return true;
            } finally {
                mongo.close();
            }
        } catch (Exception e) {
            return false;
        }
    }
View Full Code Here


    private DBCollection nodesStore;
    private DBCollection settingsStore;

    NodeMapInMongoDb(String dir) {
        try {
            con = new Mongo();
            db = con.getDB(DB);
            db.setWriteConcern(WriteConcern.SAFE);
            nodesStore = db.getCollection(NODES_COLLECTION);
            nodesStore.ensureIndex(
                    new BasicDBObject(KEY_FIELD, 1),
View Full Code Here

public class MongoApp2 {
  private static final Log log = LogFactory.getLog(MongoApp.class);

  public static void main(String[] args) throws Exception {
    MongoOperations mongoOps = new MongoTemplate(new SimpleMongoDbFactory(
        new Mongo(), "database"));
    Person p = new Person("Joe", 34);
    // Insert is used to initially store the object into the database.
    mongoOps.insert(p);
    log.info("Insert: " + p); // Find
    p = mongoOps.findById(p.getId(), Person.class);
View Full Code Here

public class MongoApp {
  private static final Log log = LogFactory.getLog(MongoApp.class);

  public static void main(String[] args) throws Exception {
    MongoOperations mongoOps = new MongoTemplate(new Mongo(), "database");
    mongoOps.insert(new Person("Joe", 34));
    log.info(mongoOps.findOne(new Query(where("name").is("Joe")),
        Person.class));
//    mongoOps.dropCollection("person");
  }
View Full Code Here

    private Mongo con;
    private DB db;
    private DBCollection dataStore;

    public MongoBlobStore() throws IOException {
        con = new Mongo();
        db = con.getDB(DB);
        db.setWriteConcern(WriteConcern.SAFE);
        dataStore = db.getCollection(DATASTORE_COLLECTION);
        dataStore.ensureIndex(
                new BasicDBObject(DIGEST_FIELD, 1),
View Full Code Here

    // TODO: make this configurable
    private IdFactory idFactory = IdFactory.getDigestFactory();
   
    public void initialize(File homeDir) throws Exception {
        con = new Mongo();
        //con = new Mongo("localhost", 27017);

        db = con.getDB("mk");
        db.setWriteConcern(WriteConcern.SAFE);
View Full Code Here

            String dbName = getProperty(properties, "dbName", "test");
            String username = getProperty(properties, "username", null);
            String password = getProperty(properties, "password", null);
           
            try {
                Mongo mongo;
                if (mongoURI != null) {
                    MongoURI uri = new MongoURI(mongoURI);
                    mongo = new Mongo(uri);
                }
                else {
                    MongoOptions mongoOptions = createMongoOptions(properties);
                   
                    mongo = createMongo(host, port, mongoOptions);
                }

                mongo.setWriteConcern(WriteConcern.valueOf(writeConcern));
                if (readPreference.equals("PrimaryReadPreference")) {
                    mongo.setReadPreference(ReadPreference.PRIMARY);
                }
                else if (readPreference.equals("PrimaryReadPreference")) {
                    mongo.setReadPreference(ReadPreference.SECONDARY);
                } else {
                    m_logService.log(LogService.LOG_ERROR, "ReadPreference '" + readPreference + "' is not supported and was ignored." );
                }
               
                if(username != null) {
                    mongo.getDB(dbName).authenticate(username, password.toCharArray());
                    m_logService.log(LogService.LOG_INFO, "Authenticated as '" + username + "'");
                }
               
                MongoDBServiceImpl instance = new MongoDBServiceImpl(mongo, mongo.getDB(dbName));
               
                Properties serviceProperties = new Properties();
                serviceProperties.put("dbName", dbName);
                Component component = m_dependencyManager.createComponent().setInterface(MongoDBService.class.getName(), serviceProperties).setImplementation(instance);
                m_dependencyManager.add(component);
View Full Code Here

            }
        }
    }

    private Mongo createMongo(String host, int port, MongoOptions mongoOptions) throws UnknownHostException {
        Mongo mongo;
        if(host.contains(",")) {
            String[] hosts = host.split(",");
            List<ServerAddress> addresses = new ArrayList<ServerAddress>();
            for (String hostUrl : hosts) {
                ServerAddress serverAddress;
                if(hostUrl.contains(":")) {
                    String[] hostUrlParts = hostUrl.split(":");
                    port = Integer.parseInt(hostUrlParts[1]);
                    serverAddress = new ServerAddress(hostUrlParts[0], port);
                } else {
                    serverAddress = new ServerAddress(hostUrl);
                }
               
                addresses.add(serverAddress);
            }
           
            mongo = new Mongo(addresses, mongoOptions);
        } else {
            mongo = new Mongo(new ServerAddress(host, port), mongoOptions);
        }
        return mongo;
    }
View Full Code Here

  private final static PropertyRecordDaoParams DEFAULT_PARAMS = new PropertyRecordDaoParams();

  public static class Factory {
    public static MongoDao newTestMongoDao() throws UnknownHostException,
        MongoException {
      Mongo mongo = new Mongo("localhost");
      DB database = mongo.getDB("test");
      MongoDao mongoDao = new MongoDao();
      mongoDao.setDbCollection(database.getCollection("entities"));
      mongoDao.setDbCollection_History(database
          .getCollection("entities_history"));
      mongoDao.setSourceCollection(database.getCollection("sources"));
View Full Code Here

  private void buildModel() throws UnknownHostException, MongoException {
    userIsObject = false;
    itemIsObject = false;
    idCounter = 0;
    preferenceIsString = true;
    Mongo mongoDDBB = new Mongo(mongoHost, mongoPort);
    DB db = mongoDDBB.getDB(mongoDB);
    mongoTimestamp = new Date(0);
    FastByIDMap<Collection<Preference>> userIDPrefMap = new FastByIDMap<Collection<Preference>>();
    if (!mongoAuth || (mongoAuth && db.authenticate(mongoUsername, mongoPassword.toCharArray()))) {
      collection = db.getCollection(mongoCollection);
      collectionMap = db.getCollection(MONGO_MAP_COLLECTION);
View Full Code Here

TOP

Related Classes of com.mongodb.Mongo

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.