MongoDB Learning Note ~update method Update collection Properties after the strange problem

Source: Internet
Author: User

Back to Catalog

After the encapsulation of MongoDB, for the update object in the collection of the properties of a phenomenon, people feel very disgusting, it is a beautiful array before updating, but after updating the collection object into a key-value pair, the key is the type name of the collection, the value is the real array value, Haha, This problem at first troubled me for a long time, today finally enlightened, the original is the update method of the problem, hehe!

Look at the original value.

Look at the updated value of the metamorphic

And look at our Update method.

     PublicTask Updateasync (TEntity item) {varquery =NewQuerydocument ("_id",typeof(TEntity). GetProperty (EntityKey). GetValue (item).            ToString ()); varFieldList =NewList<updatedefinition<tentity>>(); foreach(varPropertyinch typeof(TEntity). GetProperties (BindingFlags.Instance |bindingflags.public)) {if(property. Name! = EntityKey)//The update set cannot have entity keys _id{fieldlist.add (Builders<TEntity>. Update.set (property. Name, property.                GetValue (item))); }            }            returnForwait (() = _table. Updateoneasync (Query, builders<tentity>. Update.combine (FieldList))); }

There's really nothing wrong with that, but in the end it generates code with _t and _v as the key value, which is because your code is not recognized by MONGO, just like we did with the decimal type of data for MONGO before, it would have the same situation.

Workaround

The complex type is unpacked and assembled, so that it is known by MONGO, so that the update operation can be done as we expected, it is worth noting that if your object has a complex type, such as the person class has an address type, then in the assignment we spell the following

address.city= "Beijing"

And if your object property is a collection type, it is more troublesome, in addition to doing the above unpacking, but also to pay attention to its index number, such as the Person class has addlist collection properties, then in the assignment we spell the following

addlist.0.city= "Beijing"

The following public uncle's update code
         PublicTask Updateasync (TEntity item) {varquery =NewQuerydocument ("_id",typeof(TEntity). GetProperty (EntityKey). GetValue (item).            ToString ()); varFieldList =NewList<updatedefinition<tentity>>(); foreach(varPropertyinch typeof(TEntity). GetProperties (BindingFlags.Instance |bindingflags.public)) {//non-empty complex types                if(property. Propertytype.isclass && property. PropertyType! =typeof(string) && property. GetValue (item)! =NULL)                {                    if(typeof(IList). IsAssignableFrom (property. PropertyType)) {#regionCollection typeforeach(varSubinchProperty. Propertytype.getproperties (BindingFlags.Instance |bindingflags.public)) {if(Sub. Propertytype.isclass && Sub. PropertyType! =typeof(string))                            {                                varArr = property. GetValue (item) asIList; if(Arr! =NULL&& arr. Count >0)                                {                                     for(ints =0; S < arr. Count; s++)                                    {                                        foreach(varSubinnerinchSub. Propertytype.getproperties (BindingFlags.Instance |bindingflags.public)) {//PropertyName.index.innerPropertyNameFieldlist.add (BUILDERS&LT;TENTITY&GT;. Update.set (property. Name +"."+ S +"."+Subinner.name, Subinner.getvalue (Arr[s]));                        }                                    }                                }                            } }                        #endregion                    }                    Else                    {                        #regionEntity type//complex types, navigation properties, class objects, and collection objects                        foreach(varSubinchProperty. Propertytype.getproperties (BindingFlags.Instance |bindingflags.public)) {Fieldlist.add (Builders<tentity>. Update.set (property. Name +"."+Sub. Name, Sub. GetValue (property.                        GetValue (item))); }                        #endregion                    }                }                Else //Simple Type                {                    if(property. Name! = EntityKey)//The update set cannot have entity keys _id{fieldlist.add (Builders<TEntity>. Update.set (property. Name, property.                    GetValue (item))); }                }            }            returnForwait (() = _table. Updateoneasync (Query, builders<tentity>. Update.combine (FieldList))); }

Hope this article is helpful for students using MongoDB!

Back to Catalog

MongoDB Learning Note ~update method Update collection Properties after the strange problem

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.