Dotnet core uses MongoDB for high-performance Nosql database operations. mongodbnosql

Source: Internet
Author: User
Tags install mongodb mongodb driver

Dotnet core uses MongoDB for high-performance Nosql database operations. mongodbnosql

I haven't written a Blog for a long time. I watched the open-source Java Community drool every day, and I was so confused that I finally waited for the official release of dotnet core in March,

Looking at the dotnet community, it is also a thriving phenomenon. I don't want to bind Windows. I believe that the spring of dotneter is coming soon. I have been eager to try dotnet core for a long time. A new project on hand is also planning to use dotnet core for development. I just did my own research and shared my experience with MongoDB.

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

In addition to the C # Language plug-in, we also need the nuget plug-in.

Extended management tool. The Nuget plug-in I installed is called. net core project manager.

After installation, restart VS code.

 

After downloading vs code on the official website of vs code, you will be instructed to create a dotnet core project. if you use that project, OK. if you do not do this, follow these steps to create a project.

1. Create a project folder

2. Enter the file you just created in the command line

3. Enter the command line: dotnet new

Is it easy to install the Mongodb driver.

Open command palette in vs code. You can press Shift + Ctrl + P to enter nuget. A prompt is displayed,

Select add new package. A new input box is displayed. Enter mongodb and press Enter.

Some options will appear,

Select the first one. MongoDB. Driver is the official Driver. A new dialog box will pop up to select the version. Note that you need to select the correct one.2.3.0-beta1 2.3.0-beta1 2.3.0-beta1 (three important things)Do not select 2.3.0-rc1. It takes a lot of time to start using the wrong version.

After the installation is complete, a restore prompt is displayed.

 

Install Mongodb and start the mongo service.

Mongodb official website address: https://www.mongodb.com/, execute the command:

mongod --dbpath c:\mongodata

Now, we are all ready for the environment.

Add the following two namespaces to the code file:

using MongoDB.Bson;
using MongoDB.Driver;

To perform data operations, first instance a Consumer Client

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

Then create the 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", "SqlServer" },
                { "type", "Database" },
                { "count", 5 },
                { "info", new BsonDocument
                    {
                        { "x", 111 },
                        { "y", 222 }
                    }}
            };

Collection. InsertOne (document );

 

After the data is inserted, check whether the data is successfully inserted. You can Count the total number in the Collection.

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

Try again

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

Code is very simple, you try it on your own, Mongodb official website has a. net driver guide http://mongodb.github.io/mongo-csharp-driver/2.2/getting_started/quick_tour/

 

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.