Kavya Durgam

Kavya Durgam

  • NA
  • 26
  • 39.9k

Mongodb Command to skip the unwanted objects from an array

Feb 3 2018 12:44 AM
Below is my Json Object which contains nested array's
 
{
"formdata": [
{
"_id": "5a6026e29cbcdc48083dae1a",
"data": {
"formList": [
{
"IdentificationDetails": [
{
"Status": "Data Entry Completed",
"Form_Id": 1,
"Interviewer_Name": "NAGARAJ.P",
"Group_Id": 9,
"Study_Id": "1",
"Study_Name": "INDIAB",
"Created_Emp_Id": "1",
"Form_Name": "Food Frequency Questionnarie",
"Visit_No": "1",
"Volunteer_Id": "R270116",
"Interview_Date": "2009-10-10T00:00:00Z",
"Volunteer_Name": "RAJENDRA PAWAR"
}
]
},
{
"VFoodDetails": [
{
"Portion_Size": 168,
"Serving_Size": "0",
"Food_Id": 1078,
"Value": "0",
"Portion_Tool": 177,
"Volunteer_Id": "R270116",
"Meal_Session": 125,
"Frequency_Serving": "Never"
},
{
"Portion_Size": 15,
"Serving_Size": "0",
"Food_Id": 1080,
"Value": "0",
"Portion_Tool": 18,
"Volunteer_Id": "R270116",
"Meal_Session": 125,
"Frequency_Serving": "Never"
},
{
"Portion_Size": 168,
"Serving_Size": "1",
"Food_Id": 610,
"Value": "1",
"Portion_Tool": 19,
"Volunteer_Id": "R270116",
"Meal_Session": 156,
"Frequency_Serving": "Daily"
},
{
"Portion_Size": 15,
"Serving_Size": "1",
"Food_Id": 624,
"Value": "1",
"Portion_Tool": 24,
"Volunteer_Id": "R270116",
"Meal_Session": 156,
"Frequency_Serving": "Monthly"
}
]
}
]
}
}
]
}
 
But when i am retriveing this data from mongo db i need to skip the never objects from 
VFoodDetails array.
 
Below is the json which i require.
 
{
"formdata": [
{
"_id": "5a6026e29cbcdc48083dae1a",
"data": {
"formList": [
{
"IdentificationDetails": [
{
"Status": "Data Entry Completed",
"Form_Id": 1,
"Interviewer_Name": "NAGARAJ.P",
"Group_Id": 9,
"Study_Id": "1",
"Study_Name": "INDIAB",
"Created_Emp_Id": "1",
"Form_Name": "Food Frequency Questionnarie",
"Visit_No": "1",
"Volunteer_Id": "R270116",
"Interview_Date": "2009-10-10T00:00:00Z",
"Volunteer_Name": "RAJENDRA PAWAR"
}
]
},
{
"VFoodDetails": [
{
"Portion_Size": 168,
"Serving_Size": "1",
"Food_Id": 610,
"Value": "1",
"Portion_Tool": 19,
"Volunteer_Id": "R270116",
"Meal_Session": 156,
"Frequency_Serving": "Daily"
},
{
"Portion_Size": 15,
"Serving_Size": "1",
"Food_Id": 624,
"Value": "1",
"Portion_Tool": 24,
"Volunteer_Id": "R270116",
"Meal_Session": 156,
"Frequency_Serving": "Monthly"
}
]
}
]
}
}
]
}
 
My command is in mongo shell:
 
db.FrequencyQuestionForm.find({ $and: [  { "data.formList.IdentificationDetails.Study_Id": "1"} , { "data.formList.IdentificationDetails.Volunteer_Id": "R270116" } ] }) 
 
My Command in c# :
 
var query = Query.And(
Query.EQ("data.formList.IdentificationDetails.Study_Id", "1"),
Query.EQ("data.formList.IdentificationDetails.Volunteer_Id", "R270116")
);
db.FrequencyQuestionForm.Find(query);
 
How can i skip that never objects from this json object.but i dont want to remove this objects in database.

Answers (1)