0

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.

1 Answer 1

0

You can iterate the Beds_Details array and push the key and values into another array.Please modify the array as per your requirement. I'm using the below schema to create new object:

{
    type: key,
    value: value
}

I'm using JQuery $.each method to iterate. Angular has similar method:

var arr = [];
angular.forEach(json, function(value, key) {
    arr.push({
        type: key,
        value: value
    });
});

Demo https://jsfiddle.net/sumitridhal/j3m2h4r0/6/

JS

var jsonObj ={ "UnitId": "59225fd86ea6863028aba8b1",    
    "UnitName": "Hospital",
    "UnitIcon": "./upload/file-1493206621849.png",
    "FloorId": "592254b16ea6863028aba7e2",   
    "Material_Stream_Flow_Details": [
        {
            "Stream_Flow_DetailsID": "3",
            "fa": "590042c06ea6863028aba777", 
            "fb": "590042c06ea6863028aba777",
            "fc": "590042c06ea6863028aba777",
            "fd": "590042c06ea6863028aba777",
            "fe": "590042c06ea6863028aba777",
            "ff": "590042c06ea6863028aba777"
        }
    ],
    "Destination_Details": [
        {
            "Elevator_ID": "2",
            "Elevator": "590042c06ea6863028aba777",
            "One_way": 42.6,
            "Round_Trip": 21.3
        }
    ],
    "Beds_Details": [
        {
            "Beds_DetailsID": "1",
            "type1_Bed": 0,
            "type2_Bed": 0,
            "type3_Bed": 0,
            "type4_Bed": 0,
            "type5_Bed": 0,
            "type6_Bed": 0,
            "_id": "59006a8d6ea6863028aba8cd"
        }
    ]};

let json = jsonObj.Beds_Details[0];
delete json['Beds_DetailsID'];
delete json['_id']; // Removes Beds_DetailsID & _id

var arr = [];
$.each(json, function(key, value){
    arr.push({
        type: key,
        value: value
    });
});
Sign up to request clarification or add additional context in comments.

4 Comments

Here we are deleting _id's. Is a possible to get without those variables from server. I am using normal mongoose query as collection.find({},function (err, docs) { res.json(docs); }); .
You need to define schema for that. Keep _id : false var subSchema = mongoose.Schema({ //your subschema content },{ _id : false }); var schema = mongoose.Schema({ // schema content subSchemaCollection : [subSchema] });
I am asking while querying but not while creating.
He's adding Name : 'Sample' property from one object to another object.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.