SQL Server -> MongoDB - Foreign Keys

MongoDB, and for that matter, other nosql databases, is a new beast for many developers. Foreign Keys is a concept that may need to be implemented in Mongo, although, before you explore this route, I highly encourage you to try to embed sub-information in your document rather than try to relate it. NoSQL engines do a beautiful job of persisting whole objects.

Regardless, you'll at some point, run into a scenario where it doesn't make sense to keep embedding documents into other documents. And a DBRef probably makes sense. Read more about 'em here. The folks at mongodb.org recommend that, when you can, you use manual references ("Unless you have a compelling reason for using a DBRef use manual references."). This is an example of a manual reference:

original_id = ObjectId()

    "_id": original_id
    "name": "Broadway Center"
    "url": "bc.example.net"

    "name": "Erin"
    "places_id": original_id
    "url":  "bc.example.net/Erin"

But you can also do a DBRef, doing embedding something like this in your document:

{ $ref : <value>, $id : <value>, $db : <value> }

And apparently the order of the fields matter.

Also, apparently the C# Driver offers support for DBRefs--which may be nice than manual refs. Helper functions and what not.

That's all there is. Zoidberg away!

No comments: