Reputation: 83
i am new to coding and to want to fetch value of URl ("url": "https://test.com",) from document returned from mongo database. kindly help me thanks. I attached the document and code which i write
{
"_id": {
"$oid": "test123"
},
"settings": {
"disableHelp": false
},
"contact_code": "+1",
"verification_string": "xxxxxxxxxxxxxxxxxxxxxxxxx",
"verification_email_sent": "no",
"username": "[email protected]",
"lastname": "Douglas",
"channels": [
{
"_id": {
"$oid": "xxxxxxx"
},
"channelName": "xxxxx",
"isChannelActive": true,
"id": 358,
"url": "https://test.com",
"channelType": "hello",
"authorizationDomain": "ws.test.com"
}
],
"__v": 0
}
MongoCollection<Document> collection;
MongoClientURI connectionString;
MongoClient mongoClient;
connectionString = new MongoClientURI("credentiala.com");
mongoClient = new MongoClient(connectionString);
database = mongoClient.getDatabase("database name");
collection = database.getCollection("collectionName");
Document myDoc = collection.find().first();
myDoc = collection.find(eq("username", "[email protected]")).first();
System.out.println("value is ----" + myDoc.get("verification_string").toString());
Upvotes: 0
Views: 590
Reputation: 2005
Here is the code to retrieve the value of URI.
List<Document> arr = (List<Document>) myDoc.get("channels");
System.out.println(arr.get(0).get("url"));
Upvotes: 1
Reputation: 744
You can use JSON.parse(jsonString)
function and pass your JSON string to it.
Upvotes: 0