MongoDB Learning Notes ~ Operations on collection Properties

Source: Internet
Author: User

Back to Catalog

$unset Clear Elements

Note that the result of using $unset on a single array element may not be the same as you would imagine. The result is simply setting the value of the element to null, rather than deleting the entire element. To completely delete an array element, you can use the $pull and $pop operators.

The difference between $addToSet and $push

The function of both is to add a value to the array. However, there is a difference between the two, $addToSet the value to add if it does not exist for the add operation, but the push adds only one value; for example:

tags = ["Zzl", "Dudu"]

If you execute db.collection.update ({},{$push: {tag: "Laozhao"}}) The result is ["Laozhao", "Zzl", "Dudu"]

If you execute db.collection.update ({},{$addToSet: {tag: "Zzl"}}) The result is the same

About Update.combine Collection update attention points

For Update.combine We can merge the fields that need to be updated into the list list<updatedefinition<t>> () and finally update them together, and for the collection properties, we need to be aware of the The collection property element is added using Pusheachinstead of push, because using push overwrites the previous element, leaving only the last element (in the collection), so you need to use Pusheach instead of it, and the code is as follows:

[TestMethod] Public voidPush () {varFilter = Builders<dog>. Filter.eq (i = i.id,"5850b0bdebb91a3184f90d3d"); //update the required fields            varUpdatelist =NewList<updatedefinition<dog>>(); //update fields that require collection types            varDoghistorylist =NewList<doghistory>(); //add element to collection propertyDoghistorylist.add (Newdoghistory {historyname="four No 3", Ishealth=false, Adderss=NewAdderss ("Guangdong","Shenzhen","Coastal")                }); Doghistorylist.add (Newdoghistory {historyname="Four No 4", Ishealth=false, Adderss=NewAdderss ("Guangdong","Shenzhen","Coastal")            }); //add objects that need to be updated to UpdatelistUpdatelist.add (BUILDERS&LT;DOG&GT;. Update.pusheach (i =i.doghistory, doghistorylist)); Mongodbclient.mongomanager<Dog>. Instance.updateone (filter, Builders<Dog>. Update.combine (updatelist)); }
Optimization of update warehousing

Uncle for this, also put it into the Lind.DDD.Repositories.Mongo storage, improve the update operation, modified the previous recursive processing logic, the code is more concise, the principle is to use $set directly to the original data coverage can be.

      Private voidGenerateRecursion2 (List<UpdateDefinition<TEntity>>FieldList, PropertyInfo property,ObjectPropertyValue, TEntity item,stringfather) {            //Complex Types            if(property. Propertytype.isclass && property. PropertyType! =typeof(string) && PropertyValue! =NULL)            {                //Collection                if(typeof(IList). IsAssignableFrom (Propertyvalue.gettype ())) {vararr = PropertyValue asIList; if(Arr! =NULL&& arr. Count >0)                    {                   Fieldlist.add (builders<tentity>. Update.set (property.           Name, arr));           }                }                //Entity                Else                {                    foreach(varSubinchProperty. Propertytype.getproperties (BindingFlags.Instance |bindingflags.public)) {if(string. Isnullorwhitespace (father)) Generaterecursion (FieldList, sub, sub. GetValue (PropertyValue), item, property.                        Name); Elsegeneraterecursion (FieldList, sub, sub.) GetValue (PropertyValue), item, Father+"."+Property .                    Name); }                }            }            //Simple Type            Else            {                if(property. Name! = EntityKey)//The update set cannot have entity keys _id                {                    if(string. Isnullorwhitespace (father)) Fieldlist.add (Builders<TEntity>. Update.set (property.                    Name, PropertyValue)); ElseFieldlist.add (Builders<tentity>. Update.set (Father +"."+Property .                Name, PropertyValue)); }            }        }

For the resulting result is acceptable to us, the collection properties can be easily updated.

The resulting results are as follows

You are welcome to continue to pay attention to MONGODB technology!

Continue to pay attention to Uncle Blog!

Back to Catalog

MongoDB Learning Notes ~ Operations on collection Properties

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.