[MongoDB] Array operations

Source: Internet
Author: User
Tags mongoclient

Summary

In the actual development encountered updating the value of an array in a document, here is a record.

The driver used here is

Using Mongodb.bson;
Using Mongodb.driver;

Related articles

Getting started with [MongoDB]

[MongoDB] additions and deletions change

[Mongodb]count,gourp,distinct

[Mongodb]mapreduce

[MongoDB] Profiling Performance Analysis

[MongoDB] Index

Mongodb C # Additions and deletions change

MongoDB Training

Demo
usingMongodb.bson;usingMongodb.driver;usingMongoDB.Driver.Builders;usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;namespacewolfy.mongodb{classProgram {Static voidMain (string[] args) {        }    }    classMongohelper {Private Static ReadOnly string_db =""; Private Static ReadOnly string_col =""; Private Static ReadOnly string_IP =""; Private Static ReadOnly string_user =""; Private Static ReadOnly string_pwd =""; Private Static ReadOnly string_connstr =string. Format ("Mongodb://{0}:{1}@{2}:27017/{3}", _user, _pwd, _ip, _db); /// <summary>        ///add friend to a UserID and add if it does not exist in the array/// </summary>        /// <param name= "UserId" ></param>        /// <param name= "friendID" ></param>        /// <returns></returns>         Public Static BOOLUpdateorinsert (stringUseridstringfriendID) {            varCol =GetCollection (); varquery =Newquerydocument {{"userId", userId},Newquerydocument {{"$nin",NewBsonarray ().            ADD (friendID)}}}; varUpdate = Update.push ("Friends", friendid). INC ("Count",1); returnCol. Update (query, update, Updateflags.none).        Ok; }        Private StaticMongocollection<bsondocument>GetCollection () {mongoclient client=Newmongoclient (_CONNSTR); Mongoserver Server=client.            Getserver (); vardb =server.            Getdatabase (_db); varCol =db.            GetCollection (_col); returnCol; }        /// <summary>        ///remove a friend from a property/// </summary>        /// <param name= "UserId" ></param>        /// <param name= "friendID" ></param>        /// <returns></returns>         Public Static BOOLRemovefriend (stringUseridstringfriendID) {            varCol =GetCollection (); varquery =Newquerydocument {{"userId", userId}}; MongoDB.Driver.Builders.UpdateBuilder Builder=NewMongoDB.Driver.Builders.UpdateBuilder (); Builder. Pull ("Friends", friendID); returnCol. Update (query, builder, Updateflags.none).        Ok; }        /// <summary>        ///Update Field values/// </summary>        /// <param name= "UserId" ></param>        /// <param name= "dic" ></param>        /// <returns></returns>         Public Static BOOLUpdatefields (stringUserId, dictionary<string,Object>dic) {varCol =GetCollection (); varquery =Newquerydocument {{"userId", userId}}; MongoDB.Driver.Builders.UpdateBuilder Builder=NewMongoDB.Driver.Builders.UpdateBuilder (); foreach(varIteminchDiC. Keys) {Builder.            Set (item, Bsonvalue.create (Dic[item])); } builder. Set ("DT", DateTime.Now); returnCol. Update (query, builder, Updateflags.upsert).        Ok; }    }}

Code

These are the actions in your friends list. If the buddy list already exists, it is no longer added or added. Omit to do business with C # in judgment. In fact, there are many, can be given to MongoDB to do, can be in the code a lot less if-else.

[MongoDB] Array operations

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.