I am trying to save a set of tags within a mongodb document e.g.
{
id:"104454",
tags:["tag1", "tag2"]
}
I am struggling to figure out how to do this with the Java Driver though. I thought I would use BasicDBList but this doesnt seem to be right.
Could someone help please?
Thanks in advance.
You can use simple arrays and then you can do something like:
doc.put("tags", array)
When saving arrays into MongoDB with Java, according to the online doc, you can use anything that extends List.
So, using your example, that would be something like the following:
ArrayList tags = new ArrayList();
tags.add("tag1");
tags.add("tag2");
BasicDBObject doc = new BasicDBObject(new ObjectId(), tags);
Related
I want to ask how to get the text from element where didnt have unic id/class, i tried using xpath(copied from web browser) but it's not working this is the picture.
try below code;
("//*[#class='col-sm-4']//*[#class='text-grey'][1]").getText();
This might help.
Document doc = Jsoup.parse(driver.getPageSource());
Elements content = doc.select("span[class^=text-grey]");
ArrayList<String> allTextIntextGreyClass = (ArrayList<String>) content.eachText();
Then you can work with ArrayList to get the text you want to work with, or you can work with "content". If this does not work, you can get the inner HTML and work with it to get the right context you want. You can get innerHTML as follows:
String inHTML = driver.findElement(By.className("text-grey")).getAttribute("innerHTML");
Hi I need to convert Mongo Document to DBObject (BasicDBObject).
I am uploading a file to mongo using GridFS and I want to set metadata, which I get in document. I know Document is pretty much the same as DBObject. I know I can do something like this:
Document doc = new Document();
BasicDBObject.parse(doc.toJson());
But isn't this needlessly performance heavy?
The gridFS method setMetaData() accepts only DBObject so i have to convert it.
Is there a better way of doing that rather then converting it to string and back ?
You are kind of micro-optimizing here.
However, since both classes are implementations of Map, you can just do:
Document document = new Document();
BasicDBObject basicDBObject = new BasicDBObject(document);
Internally this does a Map#putAll operation that puts all entries of the Document map into the BasicDbObject map.
I know this is an old question and there is an accepted answer however it is not correct.
The proposed answer only does a shallow conversion between Document and DBOject. If your Json object contains nested objects or lists they will not be converted properly.
I got around this problem by serialising to JSON string. It is not efficient at all but might be enough in most cases, and at least it is correct:
public final class BsonConverter {
public static Document toDocument(DBObject dbObject) {
return Document.parse(dbObject.toString());
}
public static DBObject toDBObject(Document document) {
return BasicDBObject.parse(document.toJson());
}
}
I want to parse a json object by xpath in java.I have tried in the following way:
JSONObject obj=new JSONObject("{\"firstName\":\"John\",\"lastName\":\"doe\",\"age\":26,\"address\":{\"streetAddress\":\"naiststreet\",\"city\":\"Nara\",\"postalCode\":\"630-0192\"},\"phoneNumbers\":[{\"type\":\"iPhone\",\"number\":\"0123-4567-8888\"},{\"type\":\"home\",\"number\":\"0123-4567-8910\"}]}");
JXPathContext context = JXPathContext .newContext(obj);
Iterator i=context.iterate("phoneNumbers[0]/type");
But i found out that the above way is not working as the Iterator doesnt contain anything.
Can anyone please let me know whether I am doing any mistake here?
Also,can anyone please let me know if there is any other better way of parsing a json object by xpath.
As highlighted in comment, the XPath is wrong due to indexes starting with 1.
The second trick is to call "toMap" on your JSONObject.
JXPath doesn't know how to deal with JSONObject, but can deal with maps.
JSONObject obj=new JSONObject("{\"firstName\":\"John\",\"lastName\":\"doe\",\"age\":26,\"address\":{\"streetAddress\":\"naiststreet\",\"city\":\"Nara\",\"postalCode\":\"630-0192\"},\"phoneNumbers\":[{\"type\":\"iPhone\",\"number\":\"0123-4567-8888\"},{\"type\":\"home\",\"number\":\"0123-4567-8910\"}]}");
JXPathContext context = JXPathContext .newContext(obj.toMap());
Iterator i=context.iterate("phoneNumbers[1]/type");
I am trying to sort a Google Spreadsheet with the Java API but unfortunately it doesn't seem to work. The code I am using is really simple as shown in the API reference.
URL listFeedUrl = new URI(worksheet.getListFeedUrl().toString() + "?orderby=columnname").toURL();
However, this does not work. The feed returned is not sorted at all. Am I missing something? FYI the column I am trying to sort contains email addresses.
EDIT: I just realized that the problem only happens with the old version of Google Spreadsheet.
maybe this happens. The query is performed on the spreadsheet xml and xml tags are in lower case, for example the title of my column in my spreadseet is "Nombre" and the xml <gsx:nombre>is not working so instead of using [?orderby=Nombre], use [?orderby=nombre] with a lowercase "n"
The correct query for this is.
URL listFeedUrl = new URI(worksheet.getListFeedUrl().toString() + "?orderby=nombre").toURL();
Thanks for previous replies,
I am new to parsing concept using java, can anyone guide me how to modify the xml using saxparser. i searched long time to delete the tag but i dont know how to delete. pls guide me
You can remove an element using this :
SAXReader reader = new SAXReader();
reader.setEncoding(CharEncoding.UTF_8);
Document customXmlDocument = reader.read(inputStream);
// Get the element you want to remove and then pass it to the remove method as so
customXmlDocument.remove(Element)