Click here to Skip to main content
15,887,214 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
"cmd" : [
{
"a" : 1
},
{
"b" : 2
},
{
"c" : 3
}

]
}
i need to update object inside a array{"d":4}..


What I have tried:

<pre>package hellomongo;


import javax.lang.model.element.Element;

import org.bson.Document;

import com.mongodb.BasicDBObject;
import com.mongodb.DBCollection;
import com.mongodb.MongoClient;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
 

public class Mongoh{
public static void main(String args[]) {
		
			MongoClient mongoClient = new MongoClient("localhost", 27017);
			 
			MongoDatabase db = mongoClient.getDatabase("admin");
			MongoCollection<Document> coll = db.getCollection("command");

			BasicDBObject lurl = new BasicDBObject();
		    BasicDBObject lurl2 = new BasicDBObject();
		    BasicDBObject lkey = new BasicDBObject();
		    for(Element e : links){
		        DBCollection colls = db.getCollection(e.text());
		        lurl.put("d", e.attr("4"));
		        lurl2.put("$addToSet", new BasicDBObject(lurl));
		        lkey.put("a", e.text());
		        colls.update(lkey, lurl2);
	}
}
	
}
Posted
Updated 20-May-18 22:46pm
v3
Comments
Patrice T 16-May-18 21:45pm    
What is the problem ?
Member 13809409 17-May-18 1:07am    
iam not updating a value into mongodb .i get into programme output :
[{ "a" : 1.0 }, { "b" : 2.0 }, { "c" : 3.0 }, { "d" : 4.0 }]
mongodb:
{
"_id" : ObjectId("5afc4091884ee5cb97cd53eb"),
"cmd" : [
{
"a" : 1
},
{
"b" : 2
},
{
"c" : 3
}
]
}
Patrice T 17-May-18 2:31am    
Use Improve question to update your question.
So that everyone can pay attention to this information.
Richard MacCutchan 21-May-18 4:53am    
What is the problem - again? There is nothing in your question or your code which explains what is not working.
Member 13809409 21-May-18 5:01am    
iam not updating object inside array ..that is my problem help me

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900