Object Update in C # MONGO array

Source: Internet
Author: User

var querydetail = new Bsondocument ("CNo", DOC.CNO);
Querydetail.addrange (New Bsondocument ("Details.ddate", doc.ordate));

                    //Data with the same report date in the Daily table array
                     bool exist = Tbldays.findsync (Querydetail). Any ();
                     if (exist)
                     {
                         #region data for the same report date in the daily table array, the

is updated by field.

list<writemodel<bsondocument>> requests = new list<writemodel<bsondocument>> ();
Requests. ADD (New updateonemodel<bsondocument> (_query, update) {Isupsert = true});

var updatedetail = new Bsondocument ();
foreach (Var propert in properties)
{
var vals = Propert. GetValue (detail);
if (Propert. PropertyType.BaseType.Name.Equals ("Enum"))
{
Vals = Convert.toint64 (Vals);
}

var tmpupdate = new Bsondocument () {{"$set", new Bsondocument () {string. Format ("details.$.{ 0} ", Propert. Name), Bsondocumentwrapper.create (Vals)}}};

Requests. ADD (New updateonemodel<bsondocument> (Querydetail, tmpupdate) {Isupsert = true});
}
if (requests. Count > 0)
{
Tbldays.bulkwrite (requests);
}

#endregion
}

else {
#region data for the same report date does not exist in the day table array, it is added directly to the array

var updateset = new Bsondocument () {{"$addToSet", new Bsondocument () {{"Details", bsondocumentwrapper.create (detail)} } } };
Update. AddRange (Updateset);

Tbldays.updateone (_query, update, new UpdateOptions {Isupsert = true});

#endregion
}

C # MONGO object update in array

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.