Questions about MongoDB Document Structure

I want to build a website where people can register, they create a profile and they share text and pixtures and other can like them.

I want to save all users in a collection, my idea is to save them like i would do with a JSON File.

I dont know if MongoDB have limits or how good it would perform, i have here a example how my idea looks in Json format:

users:

{
  "Kai"
  {
    "Texts"
    {
      "text": "my text"
      {
        {"lik": "lisa", "date": "124561"},
        {"lik": "lisa", "date": "124561"}
      }
    },
   "Pictures"
    {
        {"url": "vffdvgf", "date": "124561"},
        {"url": "bfgfgbfg", "date": "124561"}
     }
    }
   }
  }
}

i hope that example helps to understand my idea, so into a Mongo Collection (Tabel) i would save all users and also the pictures and text from the users and if somebody like somethink i put it also into there. Then if somebody open the user profil i would only need to read everythink which is saved into the Mongo Entry for that username, what do you think will this work good?

And is there some limits how many users i can save into one collection (table)?

Hello @Florian_Silbereisen, welcome to the MongoDB Community forum!

MongoDB data is stored as documents in collections. Each document has fields and their values - it is JSON like structure. The actual data is stored in the database as BSON types.

Some comments about your document structure. A document has fields and values - and your structure has some things missing. For example, the following do not make a JSON (you are intending to create a JSON structure, and a JSON must have a field and value). See JSON.

{
  "Kai"
  {
    "Texts"

And, this "lik": "lisa" - it doesn’t comprehend very well. What is "lik"? If it is a field name it needs to be meaningful to everyone who work with the structure.

There are number of field types a document can contain - string, number, date, array and object are some of them. You can take advantage of these in building your document structure. And a document can store upto 16 MB (mega bytes) of data.

Let us take the "Pictures" from your structure. The pictures can be stored as an array of picture information. For example:

"pictures": [
        { "url": "https://picsum.photos/200/300", "date": ISODate("2021-03-30T05:04:55.041Z") },
        { "url": "https://picsum.photos/200", "date": ISODate("2021-03-28T00:00:00Z") }
]

The pictures field is an array, i.e., its type is array. Each element in the array is a sub-document (a.k.a. embedded document) and represents a picture’s attributes, the url and the date. The field type of the url is a string and that of the date is of type date. This structure allows that you can store dozens or hundreds or thousands of picture information within a document. Also, the MongoDB query language (MQL) allows querying these picture information efficiently.

I suggest you make your present structure a proper JSON. The usage is an aspect of how you structure the document. How are the documents structured? It is another subject, generally known as data modeling or database design (see Data modeling Introduction).


And is there some limits how many users i can save into one collection (table)?

The first question is: “How many users are you planning to store in your users collection?”. Based on that information you can plan the storage requirements for your database. Also, see MongoDB Limits and Thresholds.

1 Like

Great answer, Prasad!

As you mentioned, the way that you’ll query the data is super important in determining how you should store your data. The rule of thumb when modeling data in MongoDB is: data that is accessed together should be stored together.

1 Like

i have a new question, currently i dont understand why the mongo database show me the info that i have 1 document, please look to my picture:

i see that i have 3 entrys in the collection, in mysql this would mean i table with 3 entrys, why here in mongo it show me 1 collection and 1 documents? and how much Megabyte can i save into such a collection or document?

Hi Florian,

I’m not sure why you’re seeing inconsistent information about how many documents are in your collection. I’m hopeful that if you refresh the page, you’ll see 3 documents in your collection.

A document is roughly equivalent to a row in MySQL. A collection is roughly equivalent to a table in MySQL. For more information on how terms map between relational databases and MongoDB, check out my blog post on the topic:
https://www.mongodb.com/article/map-terms-concepts-sql-mongodb/

The inner screenshot above shows you have 3 documents in the the users collection.

Documents have a 16mb size limit. See https://docs.mongodb.com/manual/reference/limits/#:~:text=The%20maximum%20BSON%20document%20size,MongoDB%20provides%20the%20GridFS%20API for more information.

You are right when i load the website new it does show me now 3 documents. I did not know that you need to reload the site.

thank you also for the other infos.

I’m happy to hear the reload fixed the problem!

Hello, i have a question about the following, in my database i have save a user with his login infos as a document into the “users” collection. I want to add into this user a couple of “user questions”, the questions i want to write as a object which contains all questions. I see now during testing that my querys does update all user questions, what i want to have is that a user question gets only updated if already exist or it should be add if not exist. What i understand currently is that the databse does see my query for insert and update like a query which should update the complete questions object, but i want the query to only update the questions which already exist or that it adds the questions as a new object entry if the questions does not already exist and every questions also have one number, so maybe it could be possible to archive this, but i dont know how to write the query correctly, please take a look at my current database entry and my query:

@Florian_Silbereisen This looks like a new question. Can you open a new topic for it? This allows us to ensure questions don’t get buried and allows for others in the future to quickly find the answer that has been marked as correct.

1 Like

ok here is the new topic: Insert or update into a object with one query

1 Like