简体   繁体   中英

how can insert data into mongodb from android app?

I want to insert my json data into mongo database, i import mongo Driver v 3.2.2 and create a database , collection in mlab.com but i cant send data in database such an document.

this is my code :

try {
    MongoClientURI uri  = new MongoClientURI("mongodb://user:pass@ds041939.mlab.com:41939/vado");
    MongoClient client = new MongoClient(uri);
    DB db = client.getDB(uri.getDatabase());
    DBCollection collection = db.getCollection("salam");

    DBObject dbObject = (DBObject) JSON.parse("{'name':'mkyong', 'age':30}");


    collection.insert(dbObject);

    DBCursor cursorDoc = collection.find();
    while (cursorDoc.hasNext()) {
    System.out.println(cursorDoc.next());
    }
} catch (Exception e) {
    e.printStackTrace();
}

Your code was a little bit deprecated. I have adjusted it (used mongo-driver 3.2.2) and tested against my local mongo and it works fine:

try {
    MongoClientURI uri  = new MongoClientURI("mongodb://localhost:27017/test");
    MongoClient client = new MongoClient(uri);

    MongoDatabase db = client.getDatabase(uri.getDatabase());
    MongoCollection<BasicDBObject> collection = db.getCollection("salam", BasicDBObject.class);

    BasicDBObject document = new BasicDBObject();
    document.put("name", "mkyong");
    document.put("age", 30);
    collection.insertOne(document);

    MongoCursor iterator = collection.find().iterator();

    while (iterator.hasNext()) {
        System.out.println(iterator.next());
    }
} catch (Exception e) {
    e.printStackTrace();
}

Ensure that you install mongo driver via maven or gradle so you have all transient dependencies as well because mongo driver requires bson and mongodb-driver-core .

Thank you, sir. I'm using the mlab.com and get connection string... import mongo-java-driver-3.4.0-SNAPSHOT and bson 3.3.0. enter image description here the picture show connection state is connected. but dont insert in database edited code :

 try {
        MongoClientURI uri  = new MongoClientURI("mongodb://va***:******@ds049446.mlab.com:49446/sensingt");
        MongoClient client = new MongoClient(uri);

        MongoDatabase db = client.getDatabase(uri.getDatabase());
        MongoCollection<BasicDBObject> collection = db.getCollection("salam", BasicDBObject.class);

        BasicDBObject document = new BasicDBObject();
        document.put("name", "mkyong");
        document.put("age", 30);
        collection.insertOne(document);

        MongoCursor iterator = collection.find().iterator();
        //System.out.println("Insert successfully");
        while (iterator.hasNext()) {
            System.out.println(iterator.next());
        }
    } catch (Exception e) {
        e.printStackTrace();
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM