从Mongo数据库中获取文档的值

huangapple go评论85阅读模式
英文:

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

{
&quot;_id&quot;: {
    &quot;$oid&quot;: &quot;test123&quot;
},
&quot;settings&quot;: {
    &quot;disableHelp&quot;: false
},
&quot;contact_code&quot;: &quot;+1&quot;,
&quot;verification_string&quot;: &quot;xxxxxxxxxxxxxxxxxxxxxxxxx&quot;,
&quot;verification_email_sent&quot;: &quot;no&quot;,

&quot;username&quot;: &quot;test2289223@gmail.com&quot;,
&quot;lastname&quot;: &quot;Douglas&quot;,
&quot;channels&quot;: [
    {
        &quot;_id&quot;: {
            &quot;$oid&quot;: &quot;xxxxxxx&quot;
        },
        &quot;channelName&quot;: &quot;xxxxx&quot;,
        &quot;isChannelActive&quot;: true,
        &quot;id&quot;: 358,
		
        &quot;url&quot;: &quot;https://test.com&quot;,
		
        &quot;channelType&quot;: &quot;hello&quot;,
        &quot;authorizationDomain&quot;: &quot;ws.test.com&quot;
    }
],
&quot;__v&quot;: 0

}

MongoCollection&lt;Document&gt; collection;
MongoClientURI connectionString;
MongoClient mongoClient;

connectionString = new MongoClientURI(&quot;credentiala.com&quot;);
mongoClient = new MongoClient(connectionString);

database = mongoClient.getDatabase(&quot;database name&quot;);
collection = database.getCollection(&quot;collectionName&quot;);
Document myDoc = collection.find().first();
myDoc = collection.find(eq(&quot;username&quot;, &quot;test2289223@gmail.com&quot;)).first();
System.out.println(&quot;value is ----&quot; + myDoc.get(&quot;verification_string&quot;).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&lt;Document&gt; arr = (List&lt;Document&gt;) myDoc.get(&quot;channels&quot;);
System.out.println(arr.get(0).get(&quot;url&quot;));

答案2

得分: 0

你可以使用 JSON.parse(jsonString) 函数,将你的 JSON 字符串传递给它。

英文:

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

huangapple
  • 本文由 发表于 2020年10月11日 14:35:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/64301275.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定