MongoDB Learning Note ~ The data model attribute should be initialized for a collection when it is set.

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.