C # Simple Operation MongoDB

Source: Internet
Author: User

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 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
 varDocument =Newbsondocument {{"name","test Data 1" },                { "type","Big class" },                { " Number",5 },                { "Info",Newbsondocument {{"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.

 varresult =collection. Find (Builders<bsondocument>. FILTER.LT (" Number",999) & Builders<bsondocument>. FILTER.GT (" Number", the)) & Builders<bsondocument>. Filter.eq ("name","test Data 1"))
. ORDER BY (x=>x["number"])//Sort. Skip (Ten)//Skip. Limit (Ten)//Limit
. As<bar>()//m=>o. ToList ();//Like a great LINQ?

What if it's not?

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

The custom class used is about the length of this:

     Public classBar { PublicObjectId _id {Get;Set; }  Public stringName {Get;Set; }  Public stringType {Get;Set; }  Public intNumber {Get;Set; }  Public intnumber2 {Get;Set; }  PublicBarinfo Info {Get;Set; }  Public classBarinfo { Public intx {Get;Set; }  Public intY {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.

Tags Index: