英文:
Get value from document in Mongo database
问题
我刚开始学习编码,想要从从Mongo数据库返回的文档中获取URL的值("url": "https://test.com")。请帮助我,谢谢。我附上了我编写的文档和代码。
{
    "_id": {
        "$oid": "test123"
    },
    "settings": {
        "disableHelp": false
    },
    "contact_code": "+1",
    "verification_string": "xxxxxxxxxxxxxxxxxxxxxxxxx",
    "verification_email_sent": "no",
   
    "username": "test2289223@gmail.com",
    "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", "test2289223@gmail.com")).first();
System.out.println("value is ----" + myDoc.get("verification_string").toString());
英文:
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": "test2289223@gmail.com",
"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", "test2289223@gmail.com")).first();
System.out.println("value is ----" + myDoc.get("verification_string").toString());
答案1
得分: 1
这是检索URI值的代码。
List<Document> arr = (List<Document>) myDoc.get("channels");
System.out.println(arr.get(0).get("url"));
英文:
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"));
答案2
得分: 0
你可以使用 JSON.parse(jsonString) 函数,将你的 JSON 字符串传递给它。
英文:
You can use JSON.parse(jsonString) function and pass your JSON string to it.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论