user3826914
user3826914

Reputation: 83

Get value from document in Mongo database

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

Answers (2)

RLD
RLD

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

MahanTp
MahanTp

Reputation: 744

You can use JSON.parse(jsonString) function and pass your JSON string to it.

Upvotes: 0

Related Questions