MongoDb GridFS 与数据库中现有的特定集合

MongoDb GridFS with the existing specific collection in a database

考虑 "restaurant" mongodb 数据库中的以下 "restaurants" 集合文档。

{
"_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"

}

在此文档中"grades" filed 是嵌入的文档数组。该数组将达到 mongodb 最大文档大小 16Mb。所以现在我们要改变这个集合的数据模型。我开始知道在 mongoDb 中,我们可以使用 "gridfs" 存储超过默认限制 16mb 的文档。我无法找到证明这一点的链接。我需要将 gridfs 文档与集合中的现有字段一起存储。

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

基本上只在"One-to-(Very-)Few"关系中有效。在你的情况下,它是不同的。

提出正确的问题。你真正想知道什么?我的猜测是

What are the grades for a given restaurant?

因此,建模变得容易。首先是restaurants合集

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

和一个 grades 集合:

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

回答问题无非就是:

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