We are about to start the simplest MongoDB connection to access data. Before that, you have installed MongoDB! Maybe, probably, it should have been installed. Really not installed yet? So: Click here to [install MongoDB]
Start:
Download a C # driver first. MongoDB provides development drivers for various mainstream and non-mainstream predictions.
C # DRIVER: click here
CSHARP driver Tutorial: click here
Download file installation or decompress package
If you are installing the package, go to the installation location. If you are using a zip package, extract the package and obtain the following two files:
- MongoDB. bson. dll: serialization, JSON-related
- MongoDB. Driver. dll: Our driver
No. Only these two files are our favorites.
Continue:
Create a C # project. Do you have vs2012 or vs2010? Send me a copy. Thanks for sharing :)
Add reference to introduce the above two DLL files to the Project
Have you started mongod.exe? What is it? Click here. Start the service.
CodeAdd a namespace:
Using MongoDB. bson;
Using MongoDB. driver;
Follow [CSHARP driver Tutorial: Click Here] to continue. If you do not open [CSHARP driver tutorial], do not open it. Read the full text to avoid distracting effort.
// MongoDB connection string, with [MongoDB: // . Here, we are connected to a local service
String Connectionstring = " MongoDB: // localhost " ;
// Connect to a physical server
Mongoserver Server = Mongoserver. Create (connectionstring );
- MongoDB connection string
- In the connection string, we can specify the service and connection port formats on other machines as follows:
- MongoDB: // [Username: password @] hostname [: Port] [/[database] [? Options]
- Simple Example: MongoDB: // server1, server2: 27017, server2: 27018
- For more details, refer to [CSHARP driver Tutorial: Click here]
- Mongoserver
- There are several different methods to create a reload:
- External server create () If you only connect to the local machine and the local machine only starts one service, you can directly call this method without a connection string.
- Mongoserver create (mongoonstringbuilder builder)
- Login Server create (login url)
- Login Server create (string connectionstring)
- Login Server create (URI)
- For more details, refer to [CSHARP driver Tutorial: Click here]
Continue:
Add several lines of code:
// MongoDB connection string, with [MongoDB: // . Here, we are connected to a local service
String Connectionstring = " MongoDB: // localhost " ;
// Connect to a physical server
Mongoserver Server = Mongoserver. Create (connectionstring );
// -------------------------------------------------------------------------
// Open the database testdb
Analytic database DB = Server. getdatabase ( " Testdb " );
// Obtain the set employees
Mongocollection collection = DB. getcollection ( " Employees " );
- Server. getdatabase ("Testdb")
- Open the database: testdb
- I don't have a database named testdb ?? Don't worry, don't worry, don't waste time on this issue, if you don't have this inventory, MongoDB will automatically create it for you
- DB. getcollection ("Employees")
- Get set: Employees
- Well, with the previous lesson, do you care about him? If love does not exist, developers of MongoDB will create it for us?
Continue:
// MongoDB connection string, with [MongoDB: // . Here, we are connected to a local service
String Connectionstring = " MongoDB: // localhost " ;
// Connect to a physical server
Mongoserver Server = Mongoserver. Create (connectionstring );
// -------------------------------------------------------------------------
// Open the database testdb
Analytic database DB = Server. getdatabase ( " Testdb " );
// Obtain the set employees
Mongocollection collection = DB. getcollection ( " Employees " ); // -------------------------------------------------------------------------
// Create an employee
Bsondocument employee = New Bsondocument
{
{ " Name " , " Ernest Hemingway " },
{ " Title " , " For Whom the Bell Tolls " }
};
// Write it to the above set.
Collection. insert (employee );
If yourProgramIf no exception is thrown, the data already exists. I don't know what bsondocument is?
Let's look at a simple example:
Bsondocument document =NewBsondocument {
{"Name", Name },
{"City", City },// Not addedIfCity isNull
{"Dob", DOB, dobavailable}// Not addedIfDobavailable isFalse
};
It stores data in JSON format as a key-value pair. MongoDB uses bsondocument to set up bsondocument so that you can store data in complex formats.
- There are some important concepts.ArticleIn the future, you must go through bsontype, bsonvalue, bsonelement, bsondocument, server, database, and collection.
- Here: [CSHARP driver Tutorial: Click here]
Continue:
The last few lines of small code:
// -------------------------------------------------------------------------
// Query the data just inserted above. This format is used. Please refer to the querydocument document.
VaR Query = New Querydocument ( " Name " , " Ernest Hemingway " );
// Traverse results
Foreach (Bsondocument EMP In Collection. Find (query ))
{
// Bsonvalue can be set to asxxx or toxxx.
Console. writeline ( " Name: {0} \ ttitle: {1} " , EMP [ " Name " ]. Asstring, EMP [ " Title " ]. Tostring ());
}
- querydocument
- collect collection books;
var query = query. and (
query. eq ( "author" , "Kurt Vonnegut" ),
query. eq ( "title" , "Cats craddle" )
);
- I don't want to explain it. Please refer to the various documents for details of the query usage (because I haven't read it yet, I can't explain it, huh, huh ). As the first step, here we know that the accumulation of quantity and details has been continuously obtained in practice. [CSHARP driver Tutorial: Click Here]
- Bsonvalue
- EMP ["Name"]. AsstringThis is the first value method. Asint32, asboolean, etc.
- EMP ["Title"]. Tostring() This is the second value method. Toint32, toboolean, etc.
- Note the following details:
- Asxxx value method. If the types are inconsistent, an invalidcastexception can be thrown.
- Toxxx value method. If no exception is thrown, the default value is returned. [CSHARP driver Tutorial: Click here]
So far, you have completed the first access to MongoDB through the C # program. Other methods and details in development are an accumulation process. After taking the first step of success, I wish you a further step.
PS: It is finished.
I reiterate that important concepts:
Bsontype, bsonvalue, bsonelement, bsondocument, server, database, and collection
It depends on. [CSHARP driver Tutorial: Click here]