C # operations MongoDB Getting Started

Source: Internet
Author: User
Tags install mongodb

Disclaimer: This article is a summary of my experience in learning MongoDB for a week. One to give yourself a backup, and second to the same as I just contact MongoDB small white to share, what problem, give a bit please more magnanimous.

1. MongoDB Installation and Configuration

(1) Download:

MongoDB official website Https://www.mongodb.com/download-center

Go to the official website download page, you will find that the version is Windows Server ... But do not worry, I win0 test can also use, so be assured that the bold download can be. The installation process is relatively simple, the next step is enough, not much to say.

(2) configuration

In the "D:\MongoDB\bin (this directory is the Bin folder's directory, and the default location, the following command-line operation is basically under this directory)" directory under the new "Data" folder, it will be stored as the data root folder.

Create a new "log" folder in the "D:\MongoDB\bin" directory as the log folder.

Configure the MONGO service side:

Open the CMD window as an administrator and enter the command as follows:
> D:
> CD MongoDB

>CD bin
> Mongod--dbpath "D:\MongoDB\data"

Then in the browser input: http://localhost:27017/, you can see the following prompt:


You is trying to access MongoDB on the native driver port. For HTTP diagnostic access, add the port number (may not be the same as this, there are words on the line)

Thus, the MongoDB database service has been successfully started.

      Package Service:

Or run cmd, go to the bin directory and execute the following command
>mongod-dbpath "D:\MongoDB\data"-logpath "D:\MongoDB\log\MongoDB.log"-install-servicename "MongoDB"

Here--mongodb.log is the start of the log file,--servicename "MongoDB" service named MongoDB.

Then start the MongoDB service

> D:\mongodb>net START MongoDB

After the service starts successfully, you can test

Enter the bin directory, input MONGO display the following information, the service started successfully.

E:\Program Files\mongodb\bin>mongo
MongoDB Shell version:3.2.9
Connecting To:test
>

The installation configuration is complete.

2. Installing MongoDB C # Driver

Open C #, new project, click Tools >nuget Package Manager > Manage NuGet packages for solution > online,

Search for MongoDB in the search bar

Install Mongodb.driver,mongodb.bson,mongodb.driver.core three packages.

And then add the reference

Using Mongodb.driver;
Using Mongodb.bson;

3. Code

The next is to write the code, the following is the simplest I write to delete and change the operation (level Limited, do look)

1 usingSystem;2 usingSystem.Collections.Generic;3 usingSystem.ComponentModel;4 usingSystem.Data;5 usingSystem.Drawing;6 usingSystem.Linq;7 usingSystem.Text;8 usingSystem.Threading.Tasks;9 usingSystem.Windows.Forms;Ten usingMongodb.driver; One usingMongodb.bson; A  -  - namespacemongodbtest the { -      Public Partial classForm1:form -     { -  +          PublicForm1 () -         { + InitializeComponent (); A         } at  -         Private voidForm1_Load (Objectsender, EventArgs e) -         { - Mongo (); -         } -  in          Public voidMongo () -         { to             //Establish a connection +             varClient =Newmongoclient (); -             //set up a database the             varDatabase = client. Getdatabase ("TestDb"); *             //Establish Collection $             varCollection = database. Getcollection<bsondocument> ("Foo");Panax Notoginseng  -             varDocument =Newbsondocument the             { +{"name","MongoDB"}, A{"type","Database"}, the{"Count",1}, +{"Info",Newbsondocument{{"x",203},{"y",102}}} -             }; $             //Inserting Data $ collection. Insertone (document); -  -             varCount =collection. Count (document); the Console.WriteLine (count); - Wuyi             //Querying Data the             varDocument1 =collection. Find (document); - Console.WriteLine (Document1. ToString ()); Wu  -             //Update Data About             varFilter = Builders<bsondocument>. Filter.eq ("name","MongoDB"); $             varUpdate = Builders<bsondocument>. Update.set ("name","Ghazi"); -  - collection. Updatemany (filter, update); -  A             //Delete Data +             varFilter1 = Builders<bsondocument>. Filter.eq ("Count",101); the  - collection. Deletemany (filter1); $  theBsondocument Document2 =Newbsondocument (); theDocument2. ADD ("name","MongoDB"); theDocument2. ADD ("type","Database"); theDocument2. ADD ("Count","1"); -  in collection. Insertone (Document2); the         } the     } About}
View Code

4. References

Http://mongodb.github.io/mongo-csharp-driver/2.2/getting_started/This must be quiet to see, although it is in English, but look at the code on the line, or not difficult.

Http://www.cnblogs.com/qq75077027/category/441114.html This is definitely the great God, it's very detailed.

  

C # operations MongoDB Getting Started

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.