Back to Catalog
Today we are going to talk about technical points, when we design the MONGODB data model, if the property is an array or a collection type, we need to initialize it for the model when it is initialized, otherwise it will be stored as NULL in the database, and when it is stored as NULL, We will not be able to perform operations such as push,pull of components.
This is not noticed in the design of the model, and the problem is that it is not assigned to the collection and will be stored as null in MongoDB.
When the array element is push, of course, the following exception occurs, because MongoDB does not know that it is an array object, hehe
When we initialize this for a collection property of a module, the stored data structure is an array of empty elements,
When we use the Push command, we are ready to add elements.
Let's make an entire rule: For example, the following dog model, which initializes its collection properties
Public classdog:nosqlentity { PublicDog () { This. Addresshistory =NewList<adderss>(); This. Doghistory =NewList<doghistory>(); This. Foods =New string[] { }; } PublicDes des {Get;Set; } Public stringTitle {Get;Set; } Public stringType {Get;Set; } Public string[] Foods {Get;Set; } PublicList<doghistory> Doghistory {Get;Set; } PublicList<adderss> Addresshistory {Get;Set; } }
In the add operation, you do not know that the display is assigned to them, no assignment in MongoDB will also be an empty element of the array, which is in line with our design, see this data model, is a little ef in the meaning of POCO entities, hehe!
Back to Catalog
MongoDB Learning Note ~ The data model attribute should be initialized for a collection when it is set