jsonmongodbsizebsonexceed

BSON document size exceeds 16 MB in mongo document


I would like to store ticket details in an array in a mongo document. it works fine till the document size reaches 16MB, after that I get the exception (Resulting document after update is larger than 16777216) and program terminates abruptly. I cannot split this document coz it stores all the ticket details falls under that year 2016.

here goes my document structure.

{
   year:2016,
   purpose: ticketdetail,
   tickets:[
    {ticketid:001, desc:"xyz", created:20161231},
    {ticketid:002, desc:"xyz", created:20161231},
    {ticketid:003, desc:"xyz", created:20161231},
    .......
    .......
    {ticketid:00N, desc:"xyz", created:20161231},
   }]    
}

Solution

  • You will need to split your document into separate documents, perhaps in a different collection. I would not recommend GridFS, because you cannot query data within a GridFS blob.

    Here is a suggested document structure:

    {
      _id: ObjectId("85bf0ef0b9692c0010978359"),
      "ticketid" : "001",
      "desc" : "xyz",
      "created" : ISODate("2016-12-31T00:00:00.000Z")
    }
    ,
    {
      _id: ObjectId("85bed4257726f90010d4e21f"),
      "ticketid" : "002",
      "desc" : "xyz",
      "created" : ISODate("2016-12-31T00:00:00.000Z")
    }
    

    Notes on this structure:

    1. Each ticket is in a different document - this makes it scalable, because there is no limit on the number of documents in a collection.
    2. The "created" field is now in a proper date field. This gives you more accurate queryability.
    3. You said that your original document was needed to store all tickets for 2016. A suitable query on this new collection will return you all tickets for 2016, so you don't need to store them all in a single document:
    db.tickets.find({
        "created" : {
                $gte: ISODate("2016-01-01"),
                $lt: ISODate("2017-01-01")
            }
        }
    });