Mongodb csharp driver download






















For example:. These classes are singletons, so only a single instance of each class exists. You refer to these instances using the static Value property of each class:. Note that C null and BsonNull. Value are two different things. The first example behaves differently in C than in JavaScript. It is used as the building block of BsonDocument , which consists of zero or more elements. You will rarely create BsonElements directly, as they are usually created indirectly as needed.

It is an in-memory object model of a BSON document. There are three ways to create and populate a BsonDocument :. The first two are the ones you are most likely to use. The first creates an empty document, and the second creates a document with one element in both cases you can of course add more elements. All the constructors except the one with allowDuplicateNames simply call the Add method that takes the same parameters, so refer to the corresponding Add method for details about how the new document is initially populated.

A BsonDocument normally does not allow duplicate names, but if you must allow duplicate names call the constructor with the allowDuplicateNames parameter and pass in true.

It is not recommended that you allow duplicate names, and this option exists only to allow handling existing BSON documents that might have duplicate names.

MongoDB makes no particular guarantees about whether it supports documents with duplicate names, so be cautious about sending any such documents you construct to the server.

This is a traditional step by step method to create and populate a document using multiple C statements. This is similar to the previous approach but the fluent interface allows you to chain the various calls to Add so that they are all a single C statement. This is the recommended way to create and initialize a BsonDocument in one statement. A common mistake is to forget the inner set of braces. This will result in a compilation error.

This creates a top level document with two elements name and address. The value of address is a nested BSON document. It is important to note that sometimes the Add methods do NOT add a new element.

This makes it really easy to handle optional elements without having to write any if statements or conditional expressions. If you want to add a BsonNull if a value is missing you have to say so. The IDictionary overloads initialize a BsonDocument from a dictionary. Each key in the dictionary becomes the name of a new element, and each value is mapped to a matching BsonValue and becomes the value of the new element.

The overload with the keys parameter lets you select which dictionary entries to load you might also use the keys parameter to control the order in which the elements are loaded from the dictionary. The recommended way to access BsonDocument elements is to use one of the following indexers:. Note that the return value of the indexers is BsonValue , not BsonElement. This actually makes BsonDocuments much easier to work with if you ever need to get the actual BsonElements use GetElement. Here are some more:.

This class is used to represent BSON arrays. While arrays happen to be represented externally as BSON documents with a special naming convention for the elements , the BsonArray class is unrelated to the BsonDocument class because they are used very differently.

All the constructors with a parameter call the matching Add method. Note that the Add method takes a single parameter.

To create and initialize a BsonArray with multiple values use any of the following approaches:. Array elements are accessed using an integer index. Like BsonDocument , the type of the elements is BsonValue. The lazy classes are special in that they defer the deserialiation of BSON until it is needed. This is useful for when you only need a field or two out of a complex document because it will not incur the cost of deserializing the entire document or array, but just the pieces that are necessary.

This deserialization occurs a level at a time. For example, in the following document, asking for field FirstName will deserialize LastName and BirthDate as well, but it will leave Addresses in its serialized form until they are accessed.

It is very important that these classes be disposed of to ensure high performance and lower memory utilization. The raw classes are special in that they defer the deserialiation of BSON until it is needed. This is useful for when you only need a field or two out of a document and will not be accessing them repeatedly.

The Raw documents are best utilized when shuffling data from one database or collection to another one in MongoDB or to another, external storage location.

The behavior of the RawXXX classes differ from that of the LazyXXX classes in that repeated access to the same field in the LazyXXX version will not incur repeated deserialization overhead whereas the RawXXX version will deserialize the same field without caching on repeated requests.

The remainder of this tutorial focuses on the C Driver. Only a few of the C Driver classes are thread safe. A class is not thread safe unless specifically documented as being thread safe. This class serves as the root object for working with a MongoDB server. The connections to the server are handled automatically behind the scenes a connection pool is used to increase efficiency.

When you are connecting to a replica set you will still use only one instance of MongoClient , which represents the replica set as a whole. The driver automatically finds all the members of the replica set and identifies the current primary. In other words, by default, all write operations will block until the server has acknowledged the write. The easiest way to connect to a MongoDB server is to use a connection string. The standard connection string format is:. The username and password should only be present if you are using authentication on the MongoDB server.

The database segment indicates which database the username is designated. When not specified, the default database is admin. If you are using the database segment as the initial database to use, but the username and password specified are defined in a different database, you can use the authSource option to specify the database in which the credential is defined.

Versions prior to 1. This is not recognized as of version 1. Consult the official connection string documentation for more information. To connect to multiple servers, specify the seed list by providing multiple hostnames and port numbers if required separated by commas. This connection string specifies a seed list consisting of three servers two of which are on the same machine but on different port numbers. Because specifying multiple servers is ambiguous as to whether or not it is a replica set or multiple mongos in a sharded setup , the driver will go through a discovery phase of connecting to the servers to determine their type.

This has a little overhead at connection time and can be avoided by specifying a connection mode in the connection string:. Each MongoDB server reports its hostname back through the isMaster command and the driver uses this name to talk with the server. Skip To Content. Toggle navigation. There is a newer prerelease version of this package available. See the version list below for details. Package Manager. For projects that support PackageReference , copy this XML node into the project file to reference the package.

The NuGet Team does not provide support for this client. Please contact its maintainers for support. NET Interactive. When you use the Versioned API feature, you can update your driver or server without worrying about backward compatibility issues with any commands covered by the Versioned API. If you need to run a MongoDB server on your local machine for development purposes instead of using an Atlas cluster, you need to complete the following:.

Always secure your MongoDB server from malicious attacks. See our Security Checklist for a list of security recommendations. After you successfully start your MongoDB server, specify your connection string in your driver connection code. To test whether you can connect to your server, replace the connection string in the Connect to MongoDB Atlas code example and run it.

For more information on how to read the compatibility tables, see our guide on MongoDB Compatibility Tables.



0コメント

  • 1000 / 1000