Jeremy Huang
Jeremy Huang

Reputation: 221

Is it possible to exceed Mongodb's document size limit by storing object Id in a single document?

Suppose we have such a mongodb schema (I used the format from mongoose just for illustration purposes):

var userSchema = new Schema({
    posts : [Schema.Types.ObjectId] // stores the _id field from post
});

var postSchema = new Schema({
    body : String
});

Since the maximum document size in mongodb is 16 MB, which is finite, wouldn't we run the risk of exceeding such a limit such the above schema (even if it is a every small risk ) ?

Thanks.

Upvotes: 0

Views: 152

Answers (1)

JohnnyHK
JohnnyHK

Reputation: 312045

Yes, that's possible for a very large number of posts. The traditional way to overcome the limitation is to invert the schema:

var userSchema = new Schema({
    name: String
});

var postSchema = new Schema({
    user: { type: Schema.Types.ObjectId },  // _id of the post's user
    body : String
});

Upvotes: 2

Related Questions