Kavya Durgam

Kavya Durgam

  • NA
  • 26
  • 40k

How to retrieve the data from mongodb in json format

Jan 31 2018 4:46 AM

I am retrieving the data from mongodb.it is coming as bson document.

when i am converting it to json it throws system out of memory exception because of bulk data(lakhs of data) is coming from mongodb.

Is it possible to retrieve the data from mongodb in json format directly? how to resolve this.Any Help.Thanks in Advance.

please see the image.
 
  1. public object getFrequencyQuestionData1(int id)  
  2.        {  
  3.            var StudyId = (from ST in _context.Study_Team  
  4.                           where ST.Emp_Id == id  
  5.                           select (ST.Study_Id).ToString()).ToList();  
  6.            var connectionString = "mongodb://localhost:27017";  
  7.            MongoClient client = new MongoClient(connectionString);  
  8.            MongoServer server = client.GetServer();  
  9.            MongoDatabase db = server.GetDatabase("FormDataBase");  
  10.            MongoCollection<BsonDocument> collection = db.GetCollection<BsonDocument>("FrequencyQuestionForm");  
  11.            var array = new BsonArray(StudyId.ToArray());  
  12.            var query = Query.In("data.formList.IdentificationDetails.Study_Id", array);                     
  13.            var myLists = collection.Find(query);             
  14.            myLists.SetFields(Fields.Include("_id","data.formList.IdentificationDetails.Study_Id""data.formList.IdentificationDetails.Study_Name""data.formList.IdentificationDetails.Volunteer_Id""data.formList.IdentificationDetails.Form_Id","data.formList.IdentificationDetails.Form_Name""data.formList.IdentificationDetails.Status""data.formList.IdentificationDetails.Group_Id""data.formList.IdentificationDetails.Visit_No","data.formList.IdentificationDetails.Created_Emp_Id""data.formList.IdentificationDetails.Updated_Emp_Id"));  
  15.            var items = myLists.ToList();  
  16.            string document = items.ToJson();  
  17.            JToken token = JToken.Parse(document);  
  18.            var jObject = JObject.FromObject(new { formdata = token });  
  19.            JArray sizes = (JArray)jObject["formdata"];  
  20.            ArrayList SplittedData = new ArrayList();  
  21.            for (int i = 0; i < sizes.Count(); i++)  
  22.            {  
  23.                JValue _id = (JValue)sizes[i]["_id"];  
  24.                JObject sampledata = (JObject)sizes[i]["data"];  
  25.                JArray sampledata2 = (JArray)sampledata["formList"];  
  26.                JArray sampledata3 = (JArray)sampledata2[0]["IdentificationDetails"];  
  27.                sampledata3[0]["id"] = _id;  
  28.                int? Visit_No = (int?)sampledata3[0]["Visit_No"];  
  29.                int? Group_Id = (int?)sampledata3[0]["Group_Id"];  
  30.                int? Study_Id = (int?)sampledata3[0]["Study_Id"];  
  31.                int? Form_Id = (int?)sampledata3[0]["Form_Id"];  
  32.                int CreatedEmp_Id =(int)sampledata3[0]["Created_Emp_Id"];  
  33.                var Visit_No1 = (from SS in _context.Study_Schedule  
  34.                                 join SV in _context.Study_Visits  
  35.                                 on new { SS.Study_Id, SS.Visit_No } equals  
  36.                                    new { SV.Study_Id, SV.Visit_No }  
  37.                                 //on SS.Study_Id equals SV.Study_Id && SS.Visit_No equals SV.Visit_No  
  38.                                 where SS.Study_Id == Study_Id && SS.Visit_No == Visit_No && SS.Form_Id == Form_Id  
  39.                                 select SV.Visit_Description).FirstOrDefault();  
  40.                var Created_Emp_Id = (from US in _context.User  
  41.                                      where US.Emp_Id == CreatedEmp_Id  
  42.                                      select US.Emp_Name).FirstOrDefault();  
  43.   
  44.                sampledata3[0]["Created_Emp_Id"] = Created_Emp_Id;  
  45.                if (Group_Id == null)  
  46.                {  
  47.                    sampledata3[0]["Diet_Group"] = "";  
  48.                }  
  49.                else  
  50.                {  
  51.                    var StudyGroup = (from SG in _context.Study_Group  
  52.                                      where SG.Group_Id == Group_Id  
  53.                                      select new  
  54.                                      {  
  55.                                          Group_Description = SG.Group_Description,  
  56.                                      }).FirstOrDefault();  
  57.                    if (StudyGroup == null)  
  58.                    {  
  59.                        sampledata3[0]["Diet_Group"] = "";  
  60.                    }  
  61.                    else  
  62.                    {  
  63.                        sampledata3[0]["Diet_Group"] = StudyGroup.Group_Description.ToString();  
  64.                    }  
  65.                }  
  66.                sampledata3[0]["Visit"] = Visit_No1;  
  67.                sampledata3[0]["Created_Emp_Id"] = Created_Emp_Id;  
  68.                var  sampledata4 =  (JObject)sampledata3[0];  
  69.                SplittedData.Add(sampledata4);  
  70.            }  
  71.            var jObject1 = JObject.FromObject(new { formdata = SplittedData});  
  72.            return jObject1;  
  73.        } 
 

Answers (2)