MongoDB C # driver tutorial (translated)-mysql tutorial

Source: Internet
Author: User
Draft version (date:) This file is a draft version. Although we believe that the information provided here is very accurate, it is possible that it may change the way minors are given, because we absorb user feedback, and continue to execute the C # driver. 1. Overview this tutorial describes how 10gen supports the C # driver MongoDB. Assume that

Draft version (date:) This file is a draft version. Although we believe that the information provided here is very accurate, it is possible that it may change the way minors are given, because we absorb user feedback, and continue to execute the C # driver. 1. Overview this tutorial describes how 10gen supports the C # driver MongoDB. Assume that

Draft version (date: 2010-09-30)

This file is a draft version. Although we believe that the information provided here is very accurate, it is possible for the Hong Kong Virtual host, it may change the way of minors, because we absorb user feedback, and continue to execute the C # driver.

1. Overview

This tutorial describes how 10gen supports the C # driver MongoDB. Assuming that you are familiar with MongoDB, you will focus on using C # to access MongoDB. It consists of the C # driver and BSON Library. The C # driver is built on the top and its purpose is to independently use the C # driver for the BSON Library ,.

In section 1st, we will introduce the main classes of the C # driver: internal server, external database, Collection, export cursor, export GridFS, export gridfsfileinfo, and SafeMode.

In section 2nd, we will discuss the main classes of the BSON Library: BsonType, BsonValue (and its subclasses), BsonElement, BsonDocument, and BsonArray.

Part 1: C # driver

Part 1 of this tutorial is organized in a top-down mode, so you may sometimes peek in advance if things are meaningless. You may even want to read 2nd if you are completely unfamiliar with BSON concepts, such as documents and elements in the first part.

References and namespaces

To use the C # driver from your program, you must add the following two DLL files:

  • MongoDB. BsonLibrary. dll
  • MongoDB. CSharpDriver. dll
  • You should also add the following statement to your source file:

    Using MongoDB. BsonLibrary;

    Using MongoDB. CSharpDriver;

    Except for a few exceptions, you will use the class name prefix with any "BSON" if they are part of the BsonLibrary or "Mongo" if they are part of the C # driver. This is done to minimize the possibility of name conflict when you add two statements to your program. It is expected that some classes of method parameters (mainly enumeration and flag) have short names without any prefix.

    We like the variables declared using the var statement of C #, because it will lead to shorter, more readable code. Visual Studio can easily see the type of a variable, if you need to move the cursor over the variable or use intelliisense. However, when reading this document, you do not have this capability, so in this file, instead of writing:

    Var server = login server. Create (connectionString );

    Var test = server ["test"];

    Var books = test ["books"];

    Based on our suggestions, we will write:

    Login server = login server. Create (connectionString );

    Relational database test = server ["test"];

    Collections collection Books = test ["books"];

    So you can see what type is being used.

    Thread security

    Only a few C # driver classes are thread-safe. Among them: Replica server, replica database, and replica collection replica GridFS. Generic class, you will use a lot of bsonlibraries (except BsonSymbolTable) that are not thread-safe, including all classes of aggregcursor. Class A is not thread-safe unless it is specifically recorded as thread-safe.

    The static attributes and methods of all classes are thread-safe.

    MongoServer class

    This class of service s works with the MongoDB root object. The client of an instance of this type is a MongoDB server, you want to communicate. Although this class has a common constructor, we recommend that you use the factory method to obtain an instance of this class.

    Each worker server maintains a connection pool with the server. These connections share all the calls to the server. One of the several reasons you may need to call constructor instead of directly calling the create Factory method consumer server is that if you want to keep some operations in a separate connection pool.

    Instances of this class are thread-safe.

    Connection string

    The simplest method is to connect to MongoDB using a connection string. The format of the standard MongoDB connection string is a URL in the following column format:

    Mongodb: // [username: password @] hostname [: port] [/database]

    If you are using the username and password for MongoDB server authentication, you should only. These creden apply to a single database. if the database name exists, otherwise they will set the default creden。 for all databases. The username that adds "(manage)" to the management database for authentication.

    The default port number is 27017.

    If the database name exists, the connection string can also be used with the method used to create the external database. The name (if any) of the databases ignored by the creation method login server (other creden。 are used to determine whether the default creden。 are applicable to a single database or all databases ).

    To connect to a replica set, the specified seed provides multiple names separated by commas. For example:

    Mongodb: // server1, server2: 27017, server2: 27018

    The seed list specified by the connection string consists of three servers (two of which are on the same machine but on different port numbers ).

    C # The driver can connect to the replica set even if the seed list is incomplete. It will find that the primary server responds to at least one subserver on the seed list even if the primary server is not in the seed list (the response will contain the complete replica set and the name of the current primary server ).

    Factory method

    The best way to get the example server is to use the CREATE Factory method. When the same connection string is used, this method returns the same instance of consumer server, so you don't have to worry about a lot of cases if you call Create more than once. In addition, if you only use one database, you may find it easier to skip calling this method and call for the establishment of the factory method to restore the database.

    To connect to the local MongoDB, you will write the following code:

    String connectionString = "mongodb: // localhost ";

    Login server = login server. Create (connectionString );

    Maybe ......

    Login server = login server. Create ();

    Because it is default to connect to the local host.

    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.