且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何使用C#和.NET从mongodb中检索json数据

更新时间:2023-09-08 16:56:10

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

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



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


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



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[^]