ASP. NET uses MongoDB first experience

Source: Internet
Author: User

First: Drive


If ASP. NET wants to use a link class that mongodb,.net does not own. Third-party or official link classes.

Of course there are many kinds of drivers, and I will not introduce them.

Today I'll introduce a driver-----MongoDB that I use more often.

Next, we're going to download MongoDB's C # driver, which allows us to use MongoDB in C #. : Https://github.com/samus/mongodb-csharp


The driver we need to access MongoDB in C # is the project MongoDB. Compile this project to get it, file name: MongoDB.dll

Using MongoDB in ASP.


First start MongoDB, I have already introduced in the previous article here does not introduce.

Create an Entity class user table

[CSharp]View Plaincopy
  1. Public partial class user{
  2. [MongoId]
  3. public string uid{ get;  set;}
  4. public string name{ get;  set;}
  5. public string sex{ get;  set;}
  6. public int age{ get;  set;}
  7. }

Description: This is a simple class, and [MongoId] in the code is also not possible, if it is written, he will map the "_id" field.


MongoDB Database First build a database called "dbmy", Build a collection (table) "User", in the creation of documents (data).

For example, we need to introduce MongoDB.dll

[CSharp]View Plaincopy
    1. Private String _connectionstring = "server=127.0.0.1"; //Database server IP or address
    2. Private String _dbname = "dbmy";


Add data [CSharp]View Plaincopy
  1. Public void Insert (user user)
  2. {
  3. User. UID = Guid.NewGuid ().  ToString ("N");
  4. //Create a connection first
  5. using (Mongo Mongo = new Mongo (_connectionstring)) {
  6. //Open Connection
  7. Mongo. Connect ();
  8. //Switch to the specified database
  9. var db = Mongo. Getdatabase (_dbname);
  10. //Get the appropriate collection by type
  11. var collection = db. Getcollection<user> ();
  12. //Insert an object into the collection
  13. Collection. Insert (customer);
  14. }
  15. }


Delete data [CSharp]View Plaincopy
  1. Public void Delete (string UId)
  2. {
  3. using (Mongo Mongo = new Mongo (_connectionstring)) {
  4. Mongo. Connect ();
  5. var db = Mongo. Getdatabase (_dbname);
  6. var collection = db. Getcollection<customer> ();
  7. //Delete the specified object from the collection
  8. Collection. Remove (x = X.uid = = UID);
  9. }
  10. }



modifying data [CSharp]View Plaincopy
  1. Public void Update (user user)
  2. {
  3. using (Mongo Mongo = new Mongo (_connectionstring)) {
  4. Mongo. Connect ();
  5. var db = Mongo. Getdatabase (_dbname);
  6. var collection = db. Getcollection<user> ();
  7. //Update Objects
  8. Collection. Update (user, (x = X.uid = = user. UID));
  9. }
  10. }



Get Data [CSharp]View Plaincopy
  1. Public User GetById (string UId)
  2. {
  3. using (Mongo Mongo = new Mongo (_connectionstring)) {
  4. Mongo. Connect ();
  5. var db = Mongo. Getdatabase (_dbname);
  6. var collection = db. Getcollection<user> ();
  7. //Querying a single object
  8. return collection.  FindOne (x = X.uid = = UID);
  9. }
  10. }


Call


If the operation method is encapsulated in a class called Test.cs.

[CSharp]View Plaincopy
      1. Test t=new test ();
      2. Inserting data
      3. T.insert (User);
      4. Update data

ASP. NET uses MongoDB first experience

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.