如何使用C#和.NET从mongodb中检索json数据 [英] How to retrieve json data from mongodb using C# and .NET

查看:86
本文介绍了如何使用C#和.NET从mongodb中检索json数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是MongoDb的新手。我有一个数据库,在集合中我找到了一个json标签,并且在该列表中的详细信息列在一个键值对中。如何使用c#.Net访问json数组元素?



json结构类似于这样

{

_id:AA104eM_8_en-ca,

documentId:AA104eM,

版本:8,

locale:en-ca,

json:{

type:image,

_id :AA104eM,

_links:{

parent:[

{

href:cms / api / amp / section / AAimw

}

],

feed:[

{

href:cms / api / amp / feed / AA28Wv

}

],

提供者:[

{

href:cms / api / amp / provider / AA1VFq

}

],

自我:[

{

href:cms / api / amp / image / AA104eM

}

]

},

_ name:Conservatve MP Eve Adams于2013年11月28日在渥太华拍摄。加拿大新闻社/ Adrian Wyld,

_lastEditedDateTime:2015-07-22T10:03:26Z,

_lastPublishedDateTime:2015-07-22T10 :03:27.179Z,

_locale:en-ca,

关键词:[],

facets :[

{

key:jobEnvironment,

值:[

SDP-Prod-Co3

]

},

{

key:jobInstanceName,

值:[

2014-05-24_03_19_29.059-AMP-Generic_News-

]

},

{

key:feedUrl,

值:[

example.xml

]

}

],

_expirationDateTime:2114-06-24T00:00:00Z,

title:保守党议员Eve Eve Adams于2013年11月28日在渥太华拍摄。加拿大新闻社/ Adrian Wyld,

altText:Conservatve于2013年11月28日在渥太华拍摄。加拿大新闻社/ Adrian Wyld,

标题:Conservatve于2013年11月28日在渥太华拍摄。加拿大新闻/ Adrian Wyld,

归属地:由出版社提供,

宽度:650,

身高:482,

focalRegion:{

x1:268,

y1:114,

x2:398,

y2:244

},

href:http:// com .akamaized.net / tenant / amp / entityid / AA104eM.img,

crop:[],

fileSize:0,

subscriptionType:免费,

copyright:由媒体提供,

_processingStatus:{

质量:完成,

优化:完成,

face2d:完成,

消息:[]

},

sourceHref:http:// cnet:88 / cosmos / AMP.prod / local / Working / SDP-Prod-Ch1 /AMP/2014-05-24_03_19_29.059-AMP-Generic_Ne ws- / RawImages.dat #separator_#ftp://ftp2.cp.org/downloads/online/MicrosoftMSN5/National/CPW1203189029_high.jpg,

excludedExperiences:[],
excludedVerticals:[],

_systemTags:[],

_lastPublishedSequence:7

} < br $>
}



我尝试过:



我已安装C#.Net驱动程序并能够以编程方式连接MongoDb。





用于从c#连接到db的代码应用



var conString =mongodb:// localhost:27017;

var Client = new MongoClient(conString);

var DB = Client.GetDatabase(test);

var collection = DB.GetCollection< bsondocument>(test);



如何获取详细信息,例如href键中的详细信息?我是否需要安装任何其他dll来访问json标签中的详细信息(参见上面提到的示例)?对于这种情况的任何编码示例都会有很大的帮助!!

解决方案

这对您来说是一个合理的起点:

MongoDB入门(C#版本) [ ^ ]



阅读文档,尝试示例,然后返回一个更具体的问题,在那里显示一些代码以及遇到问题的地方。


< blockquote>这两个也会对你有所帮助:



C#从mongodb读取文档到JSON并尝试获取值 - Stack Overflow [ ^ ]

c# - Mongo DB对象使用JSON序列化程序进行反序列化 - Stack Overflow [ ^

I am newbie to MongoDb.I am having a database and in the collection I am finding a json tag and under that list of details are listed in a key value pair. How to access the json array of elements using c# .Net ?

The json structure is similar like this
{
"_id" : "AA104eM_8_en-ca",
"documentId" : "AA104eM",
"version" : 8,
"locale" : "en-ca",
"json" : {
"type" : "image",
"_id" : "AA104eM",
"_links" : {
"parent" : [
{
"href" : "cms/api/amp/section/AAimw"
}
],
"feed" : [
{
"href" : "cms/api/amp/feed/AA28Wv"
}
],
"provider" : [
{
"href" : "cms/api/amp/provider/AA1VFq"
}
],
"self" : [
{
"href" : "cms/api/amp/image/AA104eM"
}
]
},
"_name" : "Conservatve MP Eve Adams is pictured November 28, 2013 in Ottawa. THE CANADIAN PRESS/Adrian Wyld",
"_lastEditedDateTime" : "2015-07-22T10:03:26Z",
"_lastPublishedDateTime" : "2015-07-22T10:03:27.179Z",
"_locale" : "en-ca",
"keywords" : [],
"facets" : [
{
"key" : "jobEnvironment",
"values" : [
"SDP-Prod-Co3"
]
},
{
"key" : "jobInstanceName",
"values" : [
"2014-05-24_03_19_29.059-AMP-Generic_News-"
]
},
{
"key" : "feedUrl",
"values" : [
"example.xml"
]
}
],
"_expirationDateTime" : "2114-06-24T00:00:00Z",
"title" : "Conservatve MP Eve Adams is pictured November 28, 2013 in Ottawa. THE CANADIAN PRESS/Adrian Wyld",
"altText" : "Conservatve is pictured November 28, 2013 in Ottawa. THE CANADIAN PRESS/Adrian Wyld",
"caption" : "Conservatve is pictured November 28, 2013 in Ottawa. THE CANADIAN PRESS/Adrian Wyld",
"attribution" : "Provided by Press",
"width" : 650,
"height" : 482,
"focalRegion" : {
"x1" : 268,
"y1" : 114,
"x2" : 398,
"y2" : 244
},
"href" : "http://com.akamaized.net/tenant/amp/entityid/AA104eM.img",
"crops" : [],
"fileSize" : 0,
"subscriptionType" : "Free",
"copyright" : "Provided by Press",
"_processingStatus" : {
"quality" : "Complete",
"optimization" : "Complete",
"face2d" : "Complete",
"messages" : []
},
"sourceHref" : "http://cnet:88/cosmos/AMP.prod/local/Working/SDP-Prod-Ch1/AMP/2014-05-24_03_19_29.059-AMP-Generic_News-/RawImages.dat#_separator_#ftp://ftp2.cp.org/downloads/online/MicrosoftMSN5/National/CPW1203189029_high.jpg",
"excludedExperiences" : [],
"excludedVerticals" : [],
"_systemTags" : [],
"_lastPublishedSequence" : 7
}
}

What I have tried:

I have installed C# .Net driver and able to connect with MongoDb programmatically.


The code used for connectivity to db from c# application

var conString = "mongodb://localhost:27017";
var Client = new MongoClient(conString);
var DB = Client.GetDatabase("test");
var collection = DB.GetCollection<bsondocument>("test");

How to fetch the details namely like the one in href key? Do i need to install any other dlls to access the details in the json tag(ref above mentioned example)?any coding example for this case would be of great help !!

解决方案

This looks like a reasonable starting point for you:
Getting Started with MongoDB (C# Edition)[^]

Read the documentation, try the examples and then come back with a more specific question where you show some code and where you have a problem.


These 2 will help you too:

C# reading document from mongodb into JSON and trying to get values - Stack Overflow[^]
c# - Mongo DB object Id deserializing using JSON serializer - Stack Overflow[^]


这篇关于如何使用C#和.NET从mongodb中检索json数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆