Begin:
Download the driver for C # first. MongoDB offers a variety of mainstream and non-mainstream predictive development drivers.
C # Driver: Bash here
CSharp Driver Tutorial: Bash here
Download the file installation or unpack the package
If you are installing, please go to the installation location to look for, if it is a ZIP archive package, unzip the package to get the following two files:
- MongoDB.Bson.dll: serialization, JSON-related
- MongoDB.Driver.dll: Our Drive
No, only these two files are our favorites.
Go on:
Create a new C # project, whether you are vs2008 or vs2010, maybe you have vs2012 in your hand? Send me a copy, too. Thanks for sharing:)
Add a reference to introduce the above two DLLs into the project
Did you start Mongod.exe? Start the service.
Add a namespace inside the code:
Using Mongodb.bson;
Using Mongodb.driver;
Follow [CSharp Driver Tutorial: Punch here] to continue doing. If you do not open [CSharp Driver Tutorial] Do not open, read the full text to see, so as not to disperse energy.
MongoDB connection string, starting with [MongoDB://]. Here, we are connected to the service of this machine
String connectionString = "Mongodb://localhost";
Connected to a mongoserver.
Mongoserver Server = Mongoserver.create (connectionString);
- MongoDB Connection string
- In the connection string, we can specify the service and connection port formats on the other machines as follows:
- Mongodb://[username:[email protected]]hostname[:p Ort][/[database][?options]]
- Simple example: mongodb://server1,server2:27017,server2:27018
- For further details please see [CSharp Driver Tutorial: Punch here]
- Mongoserver
- There are several different overloads of how to create them:
- Mongoserver Create () If you only connect to this machine, and this machine only launches a service, call this method directly, do not need to connect the string
- Mongoserver Create (Mongoconnectionstringbuilder builder)
- Mongoserver Create (mongourl URL)
- Mongoserver Create (String connectionString)
- Mongoserver Create (URI uri)
- For further details please see [CSharp Driver Tutorial: Punch here]
Go on:
Add a few more lines of code:
MongoDB connection string, starting with [MongoDB://]. Here, we are connected to the service of this machine
String connectionString = "Mongodb://localhost";
Connected to a mongoserver.
Mongoserver Server = Mongoserver.create (connectionString);
// -------------------------------------------------------------------------
Open Database TestDB
Mongodatabase db = server. Getdatabase ("TestDB");
Get Collection Employees
Mongocollection collection = db. GetCollection ("Employees");
- Server. Getdatabase ("TestDB")
- Open database: TestDB
- I don't have a testdb library?? Don't worry, don't be confused, don't waste your time on this issue, and if there is no such library, MongoDB will automatically create it for you.
- Db. GetCollection ("Employees")
- Get Collection: Employees
- Well, with the previous lesson, what does it mean, love doesn't exist, and MongoDB developers will create it for us?
Go on:
MongoDB connection string, starting with [MongoDB://]. Here, we are connected to the service of this machine
String connectionString = "Mongodb://localhost";
Connected to a mongoserver.
Mongoserver Server = Mongoserver.create (connectionString);
// -------------------------------------------------------------------------
Open Database TestDB
Mongodatabase db = server. Getdatabase ("TestDB");
Get Collection Employees
Mongocollection collection = db. GetCollection ("Employees");//-------------------------------------------------------------------------
Create an employee
Bsondocument employee = new Bsondocument
{
{"Name", "Ernest Hemingway"},
{"title", "for Whom the Bell Tolls"}
};
Write it down in the collection.
Collection. Insert (employee);
If your program does not throw any exceptions, then the data is already in. I don't know what bsondocument is.
Let's look at a simple example:
Bsondocument document = new bsondocument {
{ "name", name},
{ "City", City}, //not Added if city is null
{ "DOB", DOB, dobavailable} //not added if dobavailable is false
} ;
It saves the data in the form of a key-value pair, in JSON format. MongoDB enables you to store data in complex formats by bsondocument a set of bsondocument.
- There are some important concepts, after reading the article, you must go again: Bsontype, Bsonvalue, Bsonelement, Bsondocument, Mongoserver, Mongodatabase, mongocollection
- here: [CSharp Driver Tutorial: Punch here]
Go on:
Last few lines of small code:
// -------------------------------------------------------------------------
Query the above data just inserted, this format, look at the querydocument document it
var query = new Querydocument ("name", "Ernest Hemingway");
Traverse results
foreach (Bsondocument emp in collection. Find (query))
{
Bsonvalue There are two ways to take the value, the following two are used one is asxxx, one is toxxx
Console.WriteLine ("Name:{0}\ttitle:{1}", emp["name"]. Asstring, emp["title"]. ToString ());
}
- querydocument
- mongocollection books;
var query = Query.and (
query.eq ( " author ", " Kurt Vonnegut "),
Query.eq ( "title", "Cats Craddle ")
);
- not explained, The various details of the query to look at various documents (because I have not read it, can not explain, hehe). As a first step, it is known here that the accumulation of quantity and detail is obtained in the continuous practice. [CSharp Driver Tutorial: Bash here]
- bsonvalue value
- emp["name"]. asstring This is the first method of value. Related to Asint32,asboolean and so on
- emp["title"]. ToString () This is the second method of value. Related to Toint32,toboolean and so on
- Note the details:
- asxxx value, if the type is inconsistent, you can throw an exception InvalidCastException
- toxxx value method, No exception is thrown and the default value is returned. [CSharp Driver Tutorial: Bash here]
At this point, the first C # program has completed the entire process of accessing MongoDB.
PS: Important Concept:
Bsontype, Bsonvalue, Bsonelement, Bsondocument, Mongoserver, Mongodatabase, mongocollection
Be sure to see. [CSharp Driver Tutorial: Punch here]
C # accessing MongoDB data