0

I have record in database as below (http://www.jsoneditoronline.org/?id=cb05d8495014d3581acaf3fa466d5e4c)

          [{
            "X" : "ValueofX",
            "Y" : "ValueofY",
            "_id" : ObjectId("XXX"),
            "controlprops" : {
                "height" : "200px",
                "width" : "300px",
                "position" : "absolute",
                "X" : "387px",
                "Y" : "228px",
                "sequenceId" : 1,
                "ParentId" : null,
                "Id" : "hKNBdf0"
            }
        }, 
        {
            "X" : "ValueofX",
            "Y" : "ValueofY",
            "_id" : ObjectId("XXX"),
            "controlprops" : {
                "height" : "110px",
                "width" : "244px",
                "position" : "absolute",
                "X" : "16px",
                "Y" : "40px",
                "sequenceId" : 1,
                "ParentId" : "hKNBdf0",
                "Id" : "8aqaU39"
            }
        }, 
        {
            "X" : "ValueofX",
            "Y" : "ValueofY",
            "_id" : ObjectId("XXX"),
            "controlprops" : {
                "height" : "110px",
                "width" : "244px",
                "position" : "absolute",
                "X" : "16px",
                "Y" : "40px",
                "sequenceId" : 1,
                "ParentId" : "hKNBdf0",
                "Id" : "8aqaU39"
            }
        }, 
        {
            "X" : "ValueofX",
            "Y" : "ValueofY",
            "_id" : ObjectId("571868926383b14c1a1fbb1c"),
            "controlprops" : {
                "height" : "38px",
                "width" : "100px",
                "position" : "absolute",
                "X" : "48px",
                "Y" : "37px",
                "background_color" : "green",
                "color" : "rgb(255, 255, 255)",
                "sequenceId" : 2,
                "ParentId" : null,
                "Id" : "eSJfjXp"
            }
        }]

Here based on "ParentId","Id" in "controlprops" I want object with parent child relationship.

Here output will be like (http://www.jsoneditoronline.org/?id=cb05d8495014d3581acaf3fa4617ea52):

 [{
            "X" : "ValueofX",
            "Y" : "ValueofY",
            "controlprops" : {
                "height" : "200px",
                "width" : "300px",
                "position" : "absolute",
                "X" : "387px",
                "Y" : "228px",
                "sequenceId" : 1,
                "ParentId" : null,
                "Id" : "hKNBdf0"
            },
            "child" : [
              {
             "X" : "ValueofX",
             "Y" : "ValueofY",
            "controlprops" : {
                "height" : "110px",
                "width" : "244px",
                "position" : "absolute",
                "X" : "16px",
                "Y" : "40px",
                "sequenceId" : 1,
                "ParentId" : "hKNBdf0",
                "Id" : "8aqaU39"
             }
            },{
             "X" : "ValueofX",
             "Y" : "ValueofY",
            "controlprops" : {
                "height" : "110px",
                "width" : "244px",
                "position" : "absolute",
                "X" : "16px",
                "Y" : "40px",
                "sequenceId" : 1,
                "ParentId" : "hKNBdf0",
                "Id" : "8aqaU39"
             }
            }
            ]
        }
        , 
        {
            "X" : "ValueofX",
            "Y" : "ValueofY",
            "controlprops" : {
                "height" : "38px",
                "width" : "100px",
                "position" : "absolute",
                "X" : "48px",
                "Y" : "37px",
                "background_color" : "green",
                "color" : "rgb(255, 255, 255)",
                "sequenceId" : 2,
                "ParentId" : null,
                "Id" : "eSJfjXp"
            }
        }]

How can I achive it using mongodb/mongoose? Any help will be appreciated.

maulik sakhare
  • 1,957
  • 1
  • 12
  • 19
  • Do you want to get rid of the `_id` ? If this is the case just project the output in mongodb `db.collection('col').find({}, {_id: 0})` and this will return all but `_id` – Alexandru Olaru Apr 21 '16 at 06:26
  • @AlexandruOlaru yes he does want to get rid of `-id` but he also wants to convert other appropiate documents into child arrays using `ParentId` field, and perhaps using `_id` too; @mauliksakhare is that what you desire? – riteshtch Apr 21 '16 at 06:41
  • _Id in the output dosen't matter. I need parent child relation in the output. Please view Input and output data. You can also visit link to view difference in output – maulik sakhare Apr 21 '16 at 09:40

0 Answers0