C # Simple Operation MongoDB

Source: Internet
Author: User
Tags mongodb
One installation of MongoDB


Download, install and one step on-demand website.


Two vs Create a new project


Create a. Netcore console project, and then nuget to install the driver Mongodb.driver


Three Establish connections


Adding code in the Program.main function


 
 
var client = new MongoClient("mongodb://127.0.0.1:27017");
var database = client.GetDatabase("foo"); 
var collection = database.GetCollection<BsonDocument>("bar");


Three objects, the client is the clients that connect to the database, and collection is the object collection.



The action on the object is collection.


Four-action 1> insertion
Var document = new BsonDocument
             {
                 { "name", "test data 1" },
                 { "type", "large class" },
                 { "number", 5 },
                 { "info", new BsonDocument
                           {
                               { "x", 111 },
                               { "y", 222 }
                           }}
             };
collection.InsertOne(document);


As well as Insertmany (), given the high intelligence of VS superb tips, at a glance.


2> Find


After the previous step is inserted, find it out by find



Find () requires a filter parameter, which is queried according to the criteria


Collection. Find (BUILDERS<BSONDOCUMENT>. Filter.empty);


The above means unconditional query, matches everything.



If there is a condition, can be from Builders<bsondocument> filter, such as EQ is equal, lt is less than, GT is greater than ... Name implies. Powerful smart tips based on VS, very clear.



Example criteria query:


Collection. Find (BUILDERS<BSONDOCUMENT>. Filter.eq ("name"" test data 1") & Builders<bsondocument . filter.lt ("number"6));


Correlation or relationship between multiple conditions, corresponding use of & and | symbols


3> Update
Collection. Updatemany (BUILDERS<BSONDOCUMENT>. Filter.eq ("name"" test data 1"), Builders<bsondocument>. Update.set ("number", 6));


The update uses Updatemany (), which likewise supports conditions from BUILDERS<BSONDOCUMENT> Filter to get the.



Update support adds a new field, such as:


Collection. Updatemany (BUILDERS<BSONDOCUMENT>. Filter.eq ("name"" test data 1"), Builders<bsondocument>. Update.set ("number2"666));
4> Delete


Collection. Deletemany ()



Several other APIs are similar


Five Bsondocument to custom class object conversion


Do not introduce json.net (Newtonsoft.json)


1> Custom Types to Bsondocument


Extension functions:



Entity. Tobsondocument ()


2> bsondocument to custom types


Usually it's time to find it, ifindfluent.as<tentity> () to go.


Var result = collection
                 .Find((Builders<BsonDocument>.Filter.Lt("number",999) & Builders<BsonDocument>.Filter.Gt("number", 110)) & Builders<BsonDocument>.Filter.Eq("name", "Test Data 1"))
.OrderBy(x=>x["number"])//Sort
                 .Skip(10)//Skip
                 .Limit(10)//Restrictions
                 .As<Bar>()//m=>o
                 .ToList();//Like the Linq?


What if it's not?


var entity = bsonserializer.deserialize<bar> (Bson);


The custom class used is about the length of this:


public class Bar
    {
        public ObjectId _id { get; set; }
        public string name { get; set; }
        public string type { get; set; }
        public int number { get; set; }
        public int number2 { get; set; }
        public BarInfo info { get; set; }

        public class BarInfo
        {
            public int x { get; set; }
            public int y { get; set; }
        }
    }


Small Impressions:


MongoDB is very friendly to programmers and can dynamically change the structure, so that programmers are no longer afraid of frequent changes in demand.





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.