I am using nested collections in mongodb. I am fetching all the data from mongodb through angularjs.
my code is shown below
$http.get('/Unitget').then(function (response) {
UnitDetails = $scope.UnitDetails = response.data;
UnitDetails.forEach(function (element) {
var c = element.Beds_Details; // test purpose
type1_Bed += element.Beds_Details[0].type1_Bed ;
type2_Bed += element.Beds_Details[0].type2_Bed ;
type3_Bed += element.Beds_Details[0].type3_Bed ;
type4_Bed += element.Beds_Details[0].type4_Bed ;
type5_Bed += element.Beds_Details[0].type5_Bed ;
}
}
But here element.Beds_Details[0].type1_Bed is not getting any values if I removed that "[0]" then it is showing undefined. If I assign Beds_Details to another variable it is assigning correctly as a object.
My sample record of my collection is as below
"UnitId": "59225fd86ea6863028aba8b1", //unique id
"UnitName": "Hospital",
"UnitIcon": "./upload/file-1493206621849.png",
"FloorId": "592254b16ea6863028aba7e2", // unique id of another collection
"Material_Stream_Flow_Details": [
{
"Stream_Flow_DetailsID": "3",
"fa": "590042c06ea6863028aba777",
"fb": "590042c06ea6863028aba777",
"fc": "590042c06ea6863028aba777",
"fd": "590042c06ea6863028aba777",
"fe": "590042c06ea6863028aba777",
"ff": "590042c06ea6863028aba777",
"_id": ObjectId("59006a8d6ea6863028aba8cb")
}
],
"Destination_Details": [
{
"Elevator_ID": "2",
"Elevator": "590042c06ea6863028aba777",
"One_way": 42.6,
"Round_Trip": 21.3,
"_id": ObjectId("59006a8d6ea6863028aba8cc")
}
],
"Beds_Details": [
{
"Beds_DetailsID": "1",
"type1_Bed": 0,
"type2_Bed": 0,
"type3_Bed": 0,
"type4_Bed": 0,
"type5_Bed": 0,
"type6_Bed": 0,
"_id": ObjectId("59006a8d6ea6863028aba8cd")
}
]
How to fetch the values to in nested collection.