4

I'm newbie with the MongoDb, there are a lot of examples about updating a collection in 2.x versions but I couldn't find any source about 3.x versions.

JAVA CODE:

    MongoClient mongoClient = new MongoClient("localhost",27017);
    MongoDatabase database = mongoClient.getDatabase("dbTest");
    MongoCollection<Document> collection =    database.getCollection("colTest");
    Document updateQuery = new Document();
    updateQuery.append("$set",
    new Document().append("_id", "test"));
    Document searchQuery = new Document();
    searchQuery.append("likes", "125");
    collection.updateMulti(searchQuery, updateQuery); //.updateMulti gives an error.

There isn't any updateMulti in 3.x so how can I check the id of the database and change one of the datas?

Example MongoDB:

 { 
"_id" : "test",
   "status" : 2,
   "time" : null,
   "instagram" :{
         "description" : "database",
         "likes" : 100,
         "url" : "http://www.instagram.com/",
         "by", "users"
   },
   "batchid" : 15000234
}

Expected Output:

{ 
   "_id" : "test",
   "status" : 1,
   "time" : null,
   "instagram" :{
         "description" : "database",
         "likes" : 125,
         "url" : "http://www.instagram.com/",
         "by", "users"
   },
   "batchid" : 15000234
}

2 Answers 2

4

For Mongodb-java driver:

Use updateOne method To update single Document within the collection based on the filter,

         collection.updateOne(searchQuery, updateQuery );

Use updateMany method, To Update multiple Documents within the collection based on the filter ,

         collection.updateMany(searchQuery, updateQuery );

Example,

        MongoClient client = new MongoClient("localhost",27017);
        MongoDatabase db = client.getDatabase("TestDB");
        MongoCollection<Document> collection = db.getCollection("test");
        Document query = new Document();
        query.append("_id","test");
        Document setData = new Document();
        setData.append("status", 1).append("instagram.likes", 125);
        Document update = new Document();
        update.append("$set", setData);
        //To update single Document  
        collection.updateOne(query, update);
Sign up to request clarification or add additional context in comments.

Comments

0

so i wrote this function

 public void SetValueFromDB(String collectionName, String id, String id_value, String fieldOfChange, Object changToValue) {
        MongoCollection<Document> collection = database.getCollection(collectionName);
        BasicDBObject searchQuery = new BasicDBObject();
            searchQuery.append(id, id_value);
            Document setData = new Document();
            setData.append(fieldOfChange, changToValue);
            Document updateQuery = new Document();
            updateQuery.append("$set", setData);
            collection.updateOne(searchQuery, updateQuery);
    }

so for use it

db.SetValueFromDB("MyCollection","_id","test","likes",150);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.