Hi, im trying to create a db but im not sure how to add a data field that could be populated later. Ie. With creating a database to hold student information. I need the teacher to be able to manually add in a grade later in the GUI. So in the students collection i thought it may be correct to embed a document called grade and add the ability to add a grade at a later date. Can anyone help me with a solution.
data in MongoDB is flexible, meaning you may even start with an empty object and fill in details later. The thing here is to write your app so as to deal later with this missing information.
what you need to know in this case is that every document you insert will get an
_id field automatically (unless you create it yourself). you need to keep track of a document with this id field and then update it again with this id.
Or you may take your time, give some thought to how your data should look, define all fields in a schema, and write your app to conform to the schema. Arrays are not needed to be filled at the beginning which suits your situation to keep grades. or make each grade a separate field with null values, or assign zeros.
Yet, even with a well-thought schema, you need to keep track of
_id field of a document you want to update. so you also need to get used to working with the id field.