Root
Root

Reputation: 147

MongoDb GridFS with the existing specific collection in a database

Consider the below "restaurants" collection documents in the "restaurant" mongodb database.

{
"_id" : ObjectId("55f6564073fc0a09338cf434"),
"address" : {
    "building" : "97-22",
    "coord" : [ 
        -73.8601152, 
        40.7311739
    ],
    "street" : "63 Road",
    "zipcode" : "11374"
},
"borough" : "Queens",
"cuisine" : "Jewish/Kosher",
"grades" : [ 
    {
        "date" : ISODate("2014-11-23T17:30:00.000-06:30"),
        "grade" : "Z",
        "score" : 20
    }, 
    {
        "date" : ISODate("2013-01-16T17:30:00.000-06:30"),
        "grade" : "A",
        "score" : 13
    }, 
    {
        "date" : ISODate("2012-08-01T17:30:00.000-06:30"),
        "grade" : "A",
        "score" : 13
    }, 
    {
        "date" : ISODate("2011-12-14T17:30:00.000-06:30"),
        "grade" : "B",
        "score" : 25
    },

   ........
   .......
   .......
     {
        "date" : ISODate("2011-12-14T17:30:00.000-06:30"),
        "grade" : "AZZ",
        "score" : 25
    },   -- It reaches 15 MB
],
"name" : "Tov Kosher Kitchen",
"restaurant_id" : "40356068"

}

In this document "grades" filed is the embedded document array. This array is going to reach mongodb maximum document size 16Mb. So now we are in the situation to alter the data model for this collection. I came to know that in the mongoDb we can store the documents which exceed the default limit of 16mb by using "gridfs" . I'm not able to find the links to demonstrate this. I need to store gridfs documents along with existing fields in the collection.

Upvotes: 3

Views: 912

Answers (1)

Markus W Mahlberg
Markus W Mahlberg

Reputation: 20703

Just because you can embed documents, it doesn't mean that it's always a good idea. More often than not, it isn't.

Markus W Mahlberg

It basically only works in a "One-to-(Very-)Few" relationship. In your case, it's different.

Ask the right question. What do you really want to know? My guess is

What are the grades for a given restaurant?

So, modeling becomes easy. First, a restaurants collection

{
  _id: someObjectId,
  name: "The Foo Bar",
  cuisine: "Foo and Baz",
  ...
}

and a grades collection:

{
  _id: new ObjectId(),
  restaurant: someObjectId,
  grade: "A",
  date: someISODate
}

Answering the question is nothing more than:

db.grades.find(
  { restaurant: givenRestaurantsObjectId }
)

Upvotes: 2

Related Questions