Rituparna Kashyap
Rituparna Kashyap

Reputation: 1507

How to simplify mongodb collection?

I have a collection called Org which contains resources (Resources schema) which is shared among teams (Teams schema)

{
   "_id": ObjectId("511cfbc9d593e5290c000005"),
   "name": "Some org name",
   "resources": [
       {
           "_id": ObjectId("511cfbc9d593e5290c000007"),
           "name": "Printer1",
           /* mongoose.Schema.Types.Mixed */
           "details": {
              "ip": "192.168.1.99"
           }
       }, {
           "_id": ObjectId("511cfbc9d593e5290c000008")
           "name": "Fax1",
           "details": {
               "number": "XXXXXXXXXXXX"
           }
       }
   ],
   "teams" : [
       {
            "_id": ObjectId("511cfbc9d593e5290c000012"),
            "name": "sales",
            /*"resources": {type: [mongoose.Schema.Types.ObjectId], ref: 'Resources'}*/
            "resources": [ObjectId("511cfbc9d593e5290c000007")]
       }, {
            "_id": ObjectId("511cfbc9d593e5290c000006"),
            "name": "developer",
            "resources": [ObjectId("511cfbc9d593e5290c000007"), ObjectId("511cfbc9d593e5290c000008")]
       }
   ]      
}

Also there is a People collection, which is part of a team.

{
    "name": "Peter",
    "designation": "senior s/w engg.",
    "contact": {}
    /*"teams": {type: [mongoose.Schema.Types.ObjectId], ref: 'Teams'}*/
    "teams": [ObjectId("511cfbc9d593e5290c000006")]
}

Now I want of skip multiple updates if a resource or team is changed, so I did not use nested documents. I am not able to ref Resources schema from Teams schema. As a result, to get following result I have to go through very complex aggregation function.

{   
    "name": "Peter",
    "designation": "senior s/w engg.",
    "contact": {}
    "teams": [{
                "_id": ObjectId("511cfbc9d593e5290c000006"),
                "name": "developer",
                "resources": [{
                    "_id": ObjectId("511cfbc9d593e5290c000007"),
                    "name": "Printer1",
                    "details": {
                        "ip": "192.168.1.99"
                    }
                 }, {
                    "_id": ObjectId("511cfbc9d593e5290c000008")
                     "name": "Fax1",
                     "details": {
                         "number": "XXXXXXXXXXXX"
                    }
                 }]              
     }]
}

Can some please suggest whether if I am doing something wrong in the db design. Is there any simpler way?

Upvotes: 1

Views: 129

Answers (2)

Zaid Masud
Zaid Masud

Reputation: 13463

Now I want of skip multiple updates if a resource or team is changed, so I did not used nested documents.

You will find this to be a common tradeoff to consider in MongoDB schema design. Really it will boil down to how important it is for you to skip multiple updates. Often multiple updates are not as challenging as you might think, and turn out to be the right decision especially when read simplicity and performance is desired.

Can some please suggest whether if I am doing something wrong in the db design. Is there any simpler way?

I don't think your approach is problematic given your stated intention of avoiding multiple updates. Personally in this situation though, I would go with the nested documents approach and try to work around the multiple updates. This will mean much simpler querying, with better performance as well (which often tends to be the most important consideration in app development).

Upvotes: 3

gustavohenke
gustavohenke

Reputation: 41440

In mongoose, you could simply do the following:

People.find({ name: "Peter" }).populate("teams").exec( callback );

If this don't bring the teams.resources populated (what I doubt, because mongoose is very flexible), try this instead:

People.find({ name: "Peter" })
    .populate("teams")
    .populate("teams.resources")
    .exec( callback );

Also, don't forget to add ref in the teams property of your People schema.

Upvotes: 0

Related Questions