Dotnet Core uses MongoDB for high-performance NoSQL database operations

Source: Internet
Author: User
Tags dotnet install mongodb mongoclient mongodb driver

Long time no blog, looking at the open source of the Java Community drool, the heart is full of taste. Finally, when the Dotnet core was released this June,

Look at the dotnet community also step by step to prosperity, a prosperous scene. Not bound to Windows, I believe that the spring of Dotneter will come

Needless to say, the dotnet core is eager, a new project on hand is planning to use dotnet core to develop. Just do your own research, and share with you a bit of experience using MongoDB.

First, install vs Code, and install the C # plug-in, the official website address, I put the link here https://code.visualstudio.com/, if not installed configuration, Sir do not send

In addition to the C # language plugin, we also need the NuGet plugin.

Extension management tool, the NuGet plugin I installed is called. NET Core project Manager

After installation, you will be asked to restart vs code.

There is a guide to creating a dotnet core project after downloading vs code on the VS code's official website. If you do it, use that item. If you don't do it, follow the steps below to create a project.

1. Create a project folder

2. In the command line, enter the file you just created and

3. Command-Line Input command: dotnet new

Fried Chicken Simple Isn't it

Next we want to install the MongoDB driver.

Open command Palette in VS code, you can use the shortcut key shift+ctrl+p, the input nuget will appear in the prompt,

Selecting the Add new package will appear with a new input box, enter MongoDB and return.

There are some options,

Choose the first one, Mongodb.driver is the official driver. Then will pop up a new dialog box to select the version, note that this to choose the right, to choose 2.3.0-beta1 2.3.0-beta1 2.3.0-beta1 (important things say three times), do not choose 2.3.0-rc1, It took a lot of time to start with the wrong version.

The installation will prompt you to restore, just do it.

Then install MongoDB, and start the MONGO service.

MongoDB's official website address: https://www.mongodb.com/, execute the command:

Mongod--dbpath C:\mongodata

All right, we're all set.

Add the following two namespaces to the code file:

Using Mongodb.bson;
Using Mongodb.driver;

To perform data operations, first instantiate a mongoclient

var client = new Mongoclient ("mongodb://127.0.0.1:27017");

Then create DATABASE and collection

var database = client. Getdatabase ("foo");
var collection = database. Getcollection<bsondocument> ("Bar");

Then we can try to insert a piece of data, the code is as follows

var document = new Bsondocument
{
{"Name", "SQL Server"},
{"Type", "Database"},
{"Count", 5},
{"Info", new bsondocument
{
{"X", 111},
{"Y", 222}
}}
};

Collection. Insertone (document);

After inserting the data we'll see if it's successful, count it, and see the total in collection.

var count = collection. Count (New Bsondocument ());
Console.WriteLine ("Collection contains {0} document.", count);

Try the query again

var result = collection. Find (New Bsondocument ()). FirstOrDefault ();
Console.WriteLine ("I got the query result:");
Console.WriteLine (result);

The code is simple, let's try it yourself, Mongodb website has a. NET driver guidance http://mongodb.github.io/mongo-csharp-driver/2.2/getting_started/quick_tour/

Forrestlyu Source: http://www.cnblogs.com/forrestlyu/This copyright belongs to the author and the blog Park is shared, welcome reprint, but without the author's consent must retain this paragraph, and in the article page obvious location to the original link, Otherwise, the right to pursue legal liability is retained. Dotnet Core Elite Exchange Group Welcome to join us: 476097512

Dotnet Core uses MongoDB for high-performance NoSQL database 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.