Search code examples
mongodbschemasubdocument

Mongodb subdocument structure best practices and queries


I've seen 2 main types of schema for subdocuments:

{
    "cbill@boogiemail:com": {
        "outbound": [
            {
                "name": "First",
                "state": {
                    "saved": "[email protected]",
                    "edited": "[email protected]",
                    "status": "active"
                },
                "data": {
                }
            },
            {
                "name": "Second",
                "state": {
                    "saved": "[email protected]",
                    "edited": "[email protected]",
                    "status": "draft"
                },
                "data": {
                }
            }
        ],
        "inbound" : [
            {
                "name": "First",
                "state": {
                    "saved": "[email protected]",
                    "edited": "[email protected]",
                    "status": "active"
                },
                "data": {
                }
            },
            {
                "name": "Second",
                "state": {
                    "saved": "[email protected]",
                    "edited": "[email protected]",
                    "status": "draft"
                },
                "data": {
                }
            }
        ]
    }
}

The alternative structure is:

{
    "cbill@boogiemail:com": {
        "outbound": {
            "First": {
                "state": {
                    "saved": "[email protected]",
                    "edited": "[email protected]",
                    "status": "active"
                },
                "data": {
                }
            },
            "Second": {
                "state": {
                    "saved": "[email protected]",
                    "edited": "[email protected]",
                    "status": "draft"
                },
                "data": {
                }
            }
        },
        "inbound" : {
            "First": {
                "state": {
                    "saved": "[email protected]",
                    "edited": "[email protected]",
                    "status": "active"
                },
                "data": {
                }
            },
            "Second": {
                "state": {
                    "saved": "[email protected]",
                    "edited": "[email protected]",
                    "status": "draft"
                },
                "data": {
                }
            }
        }
    }
}

The main difference between the two is the structure of the inbound/outbound subdocuments.

What is the best practice for Mongo DB subdocument structures? And in each case, what query would get me the subdocument pointed to by:

cbill@boogiemail:com.inbound.Second ?

To add a bit more information:

The collection will have many different documents starting with different email addresses, but each document in the collection will only have a few subdocuments under the inbound/outbound keys.


Solution

  • Found the answer from here (https://www.tutorialspoint.com/how-to-select-a-specific-subdocument-in-mongodb) after some slight modifications to that.

    The query for the second example (which was the one that I was most interested in) was:

    find({ "cbill@boogiemail:com.inbound": {$exists: true}},{"cbill@boogiemail:com.inbound.Second":1}).pretty()

    This results in:

    {
        "_id" : ObjectId("6216a9940b84b1a642cb925e"),
        "cbill@boogiemail:com" : {
            "inbound" : {
                "Second" : {
                    "state" : {
                        "saved" : "[email protected]",
                        "edited" : "[email protected]",
                        "status" : "draft"
                    },
                    "data" : {
                        
                    }
                }
            }
        }
    }
    

    Whether this is the most efficient query I'm not sure - feel free to post any better alternatives.