Skip to content

Releases: mongodb/mongo-csharp-driver

1.8.2

07 Aug 16:37
Compare
Choose a tag to compare

C#/.NET Driver Version 1.8.2 Release Notes

This is a minor release.

An online version of these release notes is available at:

https://github.com/mongodb/mongo-csharp-driver/blob/master/Release%20Notes/Release%20Notes%20v1.8.2.md

File by file change logs are available at:

https://github.com/mongodb/mongo-csharp-driver/blob/master/Release%20Notes/Change%20Log%20v1.8.2-Bson.txt
https://github.com/mongodb/mongo-csharp-driver/blob/master/Release%20Notes/Change%20Log%20v1.8.2-Driver.txt

The full list of JIRA issues resolved in this release is available at:

https://jira.mongodb.org/secure/IssueNavigator.jspa?mode=hide&requestId=13830

Documentation on the C#/.NET driver can be found at:

http://docs.mongodb.org/ecosystem/drivers/csharp/
http://api.mongodb.org/csharp/current/

BSON Library Changes

Performance improvements

Serialization and deserialization of enumerable types that serialize to BSON
arrays has been sped up. The serializer for the nominal type is only looked
up once, and when using polymorphic types, the actual serializer only has to
be looked up when the actual type changes so runs of identical subtypes are
handled more efficiently.

WriteCString error checking

The BSON spec does not allow CStrings to have embedded nulls. The driver now
enforces this restriction thoroughly.

BsonMemberMaps are now frozen when BsonClassMap is frozen

The BsonMemberMap now has a Freeze method, and BsonClassMap now calls Freeze
on all the member maps when the Freeze is called on the class map.

Better support for mutable default values

A default value for mutable types is vulnerable to being altered by the
application, which would affect future uses of the default value. When using
a mutable type we really need a new instance of the default value every time.
There is now a new overload of SetDefaultValue that allows you to provide a
creator function instead of a value, so the creator function can instantiate
a new instance of the default value each time one is needed.

Driver Changes

Improved tracking of the primary for replica sets

Tracking of the current primary for replica sets has been made more reliable.
There were certain scenarios in which the driver might have two members
marked as being the current primary. With these changes there is a single
field that tracks the most recently seen primary so by definition there will
never be more than one.

Internal restructuring

Some changes were made to the internal implementation of the driver which do
not affect the public API. There is a new set of operation classes that
encapsulate the handling of wire protocol messages and some logic that used
to exist in MongoCollection has been moved to the operations. In addition,
the way commands are run has been refactored somewhat.

IndexCache removed

In the past drivers and the mongo shell kept track of calls to EnsureIndex in
order to optimize away additional round trips to the server for the same index.
But this approach has inherent problems, one of which is that it can't see
any changes made to the indexes by other processes. Therefore, the driver
no longer tracks calls to EnsureIndex and all calls to EnsureIndex are sent to
the server and it is up to the server to decide if the index already exists or
not. Typically this will not cause any backward compatibility problems and
the performance hit will be very small (unless you were calling EnsureIndex
very frequently).

1.8.1

07 Aug 16:32
Compare
Choose a tag to compare

C#/.NET Driver Version 1.8.1 Release Notes

This is a minor release. It should be considered a mandatory upgrade from 1.8. The
issues fixed were minor changes but their impact is not minor. In particular, if
you are using replica sets or are using InsertBatch with very large batches you
should consider 1.8.1 a mandatory upgrade.

An online version of these release notes is available at:

https://github.com/mongodb/mongo-csharp-driver/blob/master/Release%20Notes/Release%20Notes%20v1.8.1.md

File by file change logs are available at:

https://github.com/mongodb/mongo-csharp-driver/blob/master/Release%20Notes/Change%20Log%20v1.8.1-Bson.txt
https://github.com/mongodb/mongo-csharp-driver/blob/master/Release%20Notes/Change%20Log%20v1.8.1-Driver.txt

The full list of JIRA issues resolved in this release is available at:

https://jira.mongodb.org/secure/IssueNavigator.jspa?mode=hide&requestId=13252

Documentation on the C#/.NET driver can be found at:

http://www.mongodb.org/display/DOCS/CSharp+Language+Center
http://api.mongodb.org/csharp/current/

BSON Library Changes

Handling of closed sockets

There was an unfortunate regression in 1.8 with respect to sockets closed by the
server which causes the driver to hang waiting for data that is never going to
arrive.

This was discovered by a user who called Shutdown (which of course resulted in
all sockets being closed), but you could also encounter this issue if
you are connecting to replica sets and the primary closes all sockets when it
steps down (either due to an explicit step down or a re-election).

AscendingGuidGenerator

If you are using Guids as your _ids and you want the values to be ascending so
that they are always inserted at the right hand side of the index you can use
this IdGenerator. Note that for the server to see the Guids as ascending you
have to make sure to store them in the right representation, which is
GuidRepresentation.Standard.

We used to recommend CombGuidGenerator for this use case, but we have since
realized that the Guids generated by the CombGuid are only considered ascending
when Guids are compared using SQL Server's method of comparing Guids.

Driver Changes

MongoCollection

There is a new overload of Distinct which returns the values as TValue(s)
instead of as BsonValue(s).

There was a bug in InsertBatch that resulted in a high probability of
InsertBatch failing if the batch was big enough to have to be split into
multiple sub-batches.

MongoDatabase

RunCommandAs now uses the standard serialization mechanisms to deserialize
all command results returned from the server.

1.8

07 Aug 16:28
Compare
Choose a tag to compare
1.8

C# Driver Version 1.8 Release Notes

This is a major release.

This release has two major goals: to support the new features of the 2.4 release
of the server and to begin preparing the path for some major cleanup in the 2.0
release of the driver by marking as obsolete in the 1.8 release items that we plan to
remove in the 2.0 release.

An online version of these release notes is available at:

https://github.com/mongodb/mongo-csharp-driver/blob/master/Release%20Notes/Release%20Notes%20v1.8.md

File by file change logs are available at:

https://github.com/mongodb/mongo-csharp-driver/blob/master/Release%20Notes/Change%20Log%20v1.8-Bson.txt
https://github.com/mongodb/mongo-csharp-driver/blob/master/Release%20Notes/Change%20Log%20v1.8-Driver.txt

The full list of JIRA issues resolved in this release is available at:

https://jira.mongodb.org/secure/IssueNavigator.jspa?mode=hide&requestId=13193

Documentation on the C# driver can be found at:

http://www.mongodb.org/display/DOCS/CSharp+Language+Center
http://api.mongodb.org/csharp/current/

General changes

In this release we are deprecating a large number of methods that we intend to
remove in a subsequent release. With few exceptions, these deprecated methods
still exist and continue to work as they used to, so you can choose when to
transition off of them. Because they are deprecated they will result in
compile time warnings.

BSON Library Changes

Changes to the IO classes

The ReadBinaryData, ReadObjectId and ReadRegularExpression methods have simplified
signatures. There is a new ReadBytes method, as well as new ReadRawBsonArray
and ReadRawBsonDocument methods.

The WriteBinaryData, WriteObjectId and WriteRegularExpression methods have simplified
signatures. There is a new WriteBytes method, as well as new WriteRawBsonArray
and WriteRawBsonDocument methods.

The settings classes for the binary readers and writers now have an Encoding property
that you can use to provide an encoding. The value you supply must be an instance of a
UTF8Encoding, but you can configure that encoding any way you want. In particular,
you would want to configure lenient decoding if you have existing data that does not
conform strictly to the UTF8 standard.

BsonDocument object model changes

We are deprecating all the static Create factory methods and encourage you to
simply call the constructors instead. We are also no longer attempting to
cache any precreated instances of any BSON value (e.g. BsonInt32.Zero). These
classes are so light weight that there is no need to cache them.

A really helpful change is that you can now use indexers on documents and
arrays without having to use AsBsonDocument or AsBsonArray first. You can
now write:

var zip = person["Addresses"][0]["Zip"].AsString;

instead of

var zip = person["Addresses"].AsBsonArray[0].AsBsonDocument["Zip"].AsString;

The new LazyBsonDocument and LazyBsonArray classes allow you to defer
deserialization of elements until you first access them. The document or
array is kept in its raw byte form until you first access it, at which point
one level is deserialized. This can be a big performance win if you only
access some parts of a large document.

The new RawBsonDocument and RawBsonArray classes are similar to their lazy
counterparts, except that the data is always kept in raw bytes form only
and any elements you access are deserialized every time you access them.

The new Lazy and Raw classes are useful when you want to copy large documents
from one place to another and only need to examine a small part of the
documents.

New conventions system for automatically creating class maps

The BsonClassMapSerializer relies on class maps to control how it
serializes documents. These class maps have to be created. The easiest
way to create the class maps is to let the driver create them automatically,
which it does based on a number of conventions that control the process.
The implementation of the conventions system is completely new in this
release. Conventions implemented using the old conventions architecture
are still supported but will be based on deprecated classes and interfaces
and should be rewritten to use the new architecture.

Deserialization can now call constructors or static factory methods

You can now identify constructors or static factory methods that can
be used during deserialization to instantiate objects. This means that
it is now possible to deserialize immutable classes. During deserialization
values from the serialized document are matched to parameters of a constructor
or static factory method. If there is more than one possible constructor
or factory method then the one with the most matching parameters is chosen.

Standard serializers are now created and registered as late as possible

The standard built-in serializers are now created and instantiated as late
as possible, giving you a chance to create and register your own if you
prefer. You can either register the standard built-in serializers with
different default serialization options, or you can write and register
your own.

BsonDocument object model serialization standardized

Serialization of BsonDocument object model classes has been completely
moved to IBsonSerializer based classes. The existing ReadFrom/WriteTo
methods in the BsonDocument object model have been deprecated.

Driver Changes

New authentication model

In previous versions of the driver there was only one kind of credential
(a username/password credential) and there was a mapping that specified
which credential to use for each database being accessed. Based on that
mapping the driver would determine which credential was needed for the
database being accessed and would ensure that the connection being used
was authenticated properly.

The authentication model on the server has changed drastically, and it is no
longer required that the credential used to access a database be stored
in that same database. Therefore it is impossible to determine in advance
which credential is the one that will give you access to a database. So
now the drivers simply work with a list of credentials, with no attempt
to figure out which credential should be used with which database. Instead,
all connections are authenticated against all of the credentials supplied.

Strict enforcement of MaxConnectionIdleTime and MaxConnectionLifeTime

Previously these values were only loosely enforced and were treated more
like hints. Now they are enforced strictly. This helps prevent network
errors in certain environments where the networking infrastructure will
terminate connections that are idle for a certain amount of time or that
have been open too long (for example, some firewalls and Azure).

Driver no longer opens and closes a connection every 10 seconds

The driver pings the state of any server it is connected to once every
10 seconds. In earlier releases it would open and close a new connection
each time. While this guaranteed that the ping would happen wihtout delay
it had the unfortunate consequence of filling the server logs with
messages reporting the opening and closing of the connections. The
driver now tries to reuse an existing connection for the ping, and will
only open a new one if it can't acquire an existing one rapidly.

Removed DeprecatedQuery and DeprecatedQueryBuilder classes

These classes have been deprecated for several releases now and have
been removed entirely.

Changes to Fields builder

There is a new ElemMatch method to select which elements of an array
to include in the result documents.

Changes to Query builder

The new GeoIntersects method supports the $geoIntersects query operator with
GeoJson values. In addition, new overloads of Near and Within support
using GeoJson values with these existing query operators.

Changes to Update builder

New SetOnInsert method to support the $setOnInsert update operator.

New overload of PushEach that has a PushEachOptions parameter that can
be used to provide advanced options to the $pushEach update operator.

Changes to Index builder

Added support for creating hashed and 2dsphere indexes.

GeoJson object model

The GeoJson object model is a type-safe in memory representation of GeoJSON
objects. When these objects are serialized to BSON documents the resulting
BSON documents will be valid GeoJSON documents. You can use the GeoJson
object model to represent GeoJSON properties in your POCOs and to provide
values to the new methods added to the Query builder to support GeoJson queries.

Simplified settings classes

The settings classes have been simplified a bit and we have standardized how
settings are stored internally and how default values are applied. See the
comments below on the different settings classes.

MongoClientSettings

The CredentialsStore and DefaultCredentials properties have been replaced by
the new Credentials property. The CredentialsStore property was a mapping from
a database name to the credential to use to access that database. The Credentials
property is simply a list of credentials that will be used with all connections,
regardless of which database is being accessed.

There is a new SslSettings property that lets you control every aspect of an
SSL connection to the server.

The new ReadEncoding and WriteEncoding settings can be used to configure the
details of UTF8 encoding.

MongoServerSettings

While this class has not yet been deprecated, it eventually will be. We recommend
you always use MongoClientSettings instead.

The new settings added to MongoClientSettings have also been added to
MongoServerSettings.

MongoDatabaseSettings

The database name has been moved out of the settings class and is now an
argument to the GetDatabase method. Therefore the DatabaseName property
has been deprecated.

The proper way to create an instance of MongoDatabaseSettings is to call the
constructor. The CreateDatabaseSettings method of MongoServer ...

Read more

v1.7.1.4791

07 Aug 16:28
Compare
Choose a tag to compare
Version 1.7.1.4719.

1.7

07 Aug 16:16
Compare
Choose a tag to compare
1.7

C# Driver Version 1.7 Release Notes

This is a major release.

This release has two major goals: to standardize on the name WriteConcern and
to make Acknowledged the new default WriteConcern.

The following classes are being replaced:

  • SafeMode is replaced by WriteConcern
  • SafeModeResult is replaced by WriteConcernResult
  • MongoSafeModeException is replaced by WriteConcernException

To make Acknowledged the new default WriteConcern without breaking any existing
code that might rely on the old default we are introducing a new root class
called MongoClient.

An online version of these release notes is available at:

https://github.com/mongodb/mongo-csharp-driver/blob/master/Release%20Notes/Release%20Notes%20v1.7.md

File by file change logs are available at:

https://github.com/mongodb/mongo-csharp-driver/blob/master/Release%20Notes/Change%20Log%20v1.7-Bson.txt
https://github.com/mongodb/mongo-csharp-driver/blob/master/Release%20Notes/Change%20Log%20v1.7-Driver.txt

The full list of JIRA issues resolved in this release is available at:

https://jira.mongodb.org/secure/IssueNavigator.jspa?mode=hide&requestId=12915

Documentation on the C# driver can be found at:

http://www.mongodb.org/display/DOCS/CSharp+Language+Center
http://api.mongodb.org/csharp/current/

Standardizing on WriteConcern instead of SafeMode

Some MongoDB drivers (the C# driver included) have used SafeMode as the name
for the class which determines whether writes to the database are checked for
errors. We are now standardizing across all drivers to use the name WriteConcern
instead of SafeMode. The C# driver will continue to support the SafeMode class
for a few releases but eventually it will be removed.

You should start using the new WriteConcern class, but we have also provided
an implicit conversion from SafeMode to WriteConcern so any code that passes
a SafeMode argument to a method taking a WriteConcern parameter will still
compile and work.

New MongoClient class and default WriteConcern

The new default WriteConcern is Acknowledged, but we have introduced the new
default in a way that doesn't alter the behavior of existing programs. We
are introducing a new root class called MongoClient that defaults the
WriteConcern to Acknowledged. The existing MongoServer Create methods are
deprecated but when used continue to default to a WriteConcern of Unacknowledged.

In prior releases you would start using the C# driver with code like this:

var connectionString = "mongodb://localhost";
var server = MongoServer.Create(connectionString); // deprecated
var database = server.GetDatabase("test"); // WriteConcern defaulted to Unacknowledged

The new way to start using the C# driver is:

var connectionString = "mongodb://localhost";
var client = new MongoClient(connectionString);
var server = client.GetServer();
var database = server.GetDatabase("test"); // WriteConcern defaulted to Acknowledged

If you use the old way to start using the driver the default WriteConcern will
be Unacknowledged, but if you use the new way (using MongoClient) the default
WriteConcern will be Acknowledged.

1.6.1

07 Aug 16:11
Compare
Choose a tag to compare

C# Driver Version 1.6.1 Release Notes

This is a minor release containing a few bug fixes, particularly related to ReadPreference support
and sending commands to secondaries.

An online version of these release notes is available at:

https://github.com/mongodb/mongo-csharp-driver/blob/master/Release%20Notes/Release%20Notes%20v1.6.1.md

File by file change logs are available at:

https://github.com/mongodb/mongo-csharp-driver/blob/master/Release%20Notes/Change%20Log%20v1.6.1-Bson.txt
https://github.com/mongodb/mongo-csharp-driver/blob/master/Release%20Notes/Change%20Log%20v1.6.1-Driver.txt

The full list of JIRA issues resolved in this release is available at:

https://jira.mongodb.org/secure/IssueNavigator.jspa?mode=hide&requestId=12609

Changes to ReadPreference

The implementation of ReadPreference has been changed to more accurately follow the ReadPreference spec:

http://docs.mongodb.org/manual/applications/replication/#read-preference

The changes are:

  • SecondaryPreferred only uses the Primary if no secondaries are available (regardless of latency)
  • SecondayAcceptableLatency is now configurable
  • when sending queries to mongos:
    • ReadPreference.Primary is encoded setting the SlaveOk bit on the wire protocol to 0
    • ReadPreference.SecondaryPreferred (without tags) is encoded setting the SlaveOk bit on the wire protocol to 1
    • all other ReadPreferences are encoded using $readPreference on the wire
    • $query is now encoded before $readPreference as required by mongos
  • commands now correctly use the collection settings (they were using the database settings)

Sending commands to secondaries

Only a limited set of commands are now allowed to be sent to secondaries. All other commands
will be sent to the primary regardless of the ReadPreference you specify. The commands
that can be sent to secondaries are:

  • aggregate
  • collStats
  • count
  • dbStats
  • distinct
  • geoNear
  • geoSearch
  • geoWalk
  • group
  • mapReduce (but only if using Inline results)

The corresponding helper methods in the C# driver are:

  • MongoCollection.Aggregate
  • MongoCollection.GetStats
  • MongoCollection.Count, MongoCursor.Count and MongoCursor.Size
  • MongoDatabase.GetStats
  • MongoCollection.Distinct
  • MongoCollection.GeoNear and MongoCollection.GeoNearAs
  • MongoCollection.GeoHaystackSearch and MongoCollection.GeoHaystackSearchAs
  • MongoCollection.Group
  • MongoCollection.MapReduce (with MapReduceOutputMode.Inline)

There is no helper method (yet) for the geoWalk command.

1.6

07 Aug 16:08
Compare
Choose a tag to compare
1.6

C# Driver Version 1.6 Release Notes

This is a major release featuring support for server 2.2. The major change is
support for read preferences, allowing tag based granularity for selecting
servers to send commands and queries to. In addition, we have added support
for SSL and a helper method for the new aggregation framework.

An online version (with possible corrections) of these release notes is available at:

https://github.com/mongodb/mongo-csharp-driver/blob/master/Release%20Notes/Release%20Notes%20v1.6.md

File by file change logs are available at:

https://github.com/mongodb/mongo-csharp-driver/blob/master/Release%20Notes/Change%20Log%20v1.6-Bson.txt
https://github.com/mongodb/mongo-csharp-driver/blob/master/Release%20Notes/Change%20Log%20v1.6-Driver.txt

The full list of JIRA issues resolved in this release is available at:

https://jira.mongodb.org/secure/IssueNavigator.jspa?mode=hide&requestId=12011

Information about read preferences is available online at:

http://docs.mongodb.org/manual/applications/replication/#read-preference

These release notes describe the changes at a higher level, and omits describing
some of the minor changes.

Breaking changes

  • Commands are no longer always sent to primaries. If you are expecting this
    behavior, ensure that your read preference is set to Primary.
  • ConnectWaitFor has been removed and replaced with read preferences. Anyone
    using the ConnectWaitFor enumeration will need to change their code.
  • The serialized representation for a C# null of type BsonNull has been changed
    from { $csharpnull : true } to { _csharpnull : true } to work around limitations
    of the server. Existing data will still be correctly deserialized but new data
    will be written in the new format. This is very unlikely to affect you because
    it is very unlikely you have any properties of type BsonNull in your classes.

New features

  • There is a new [BsonSerializer] attribute that can be used to specify which
    serializer to use for a class.
  • Instances of ReadOnlyCollection are now serializable/deserializable.
  • Queries involving Mod now work with 64-bit integers also.
  • Support for TTL collections (see IndexOptions.SetTimeToLive).
  • Simple helper method for aggregation framework (see MongoCollection.Aggregate).
  • SlaveOK has been deprecated and replaced with the more flexible ReadPreference options.
  • Support for SSL connections.
  • Improved support for LINQ queries from VB.NET.
  • Support for connecting to multiple mongos’ with load balancing

1.5

07 Aug 16:02
Compare
Choose a tag to compare
1.5

C# Driver Version 1.5 Release Notes

This is a major release featuring Typed Builders, enhanced LINQ support, and serialization of custom collections. There are significant serialization performance improvements, specifically when using class maps. In addition, there are a number of other enhancements and bug fixes.

File by file change logs are available at:

https://github.com/mongodb/mongo-csharp-driver/blob/master/Release%20Notes/Change%20Log%20v1.5-Bson.txt
https://github.com/mongodb/mongo-csharp-driver/blob/master/Release%20Notes/Change%20Log%20v1.5-Driver.txt

These release notes describe the changes at a higher level, and omit describing
some of the minor changes.

Breaking changes

  • Any custom IBsonSerializer implementations utilizing the methods GetDocumentId, SetDocumentId, GetMemberSerializationInfo, or GetItemSerializationInfo will need to implement the corresponding interface to restore functionality; IBsonIdProvider, IBsonDocumentSerializer, IBsonArraySerializer

  • A call to BsonSerializer.RegisterSerializer will now fail for types that implement IBsonSerializable

  • The BsonDefaultSerializer methods IsTypeDiscriminated, LookupActualType, LookupDiscriminatorConvention, RegisterDiscriminator, and RegisterDiscriminatorConvention have been moved to BsonSerializer.

  • ObjectId.TryParse will now return false instead of throwing an exception when argument is null

  • BsonDocumentWrapper no longer ignores null, but wrather wraps it with a BsonNull.Value. Any code relying on BsonDocumentWrapper ignoring null will need to be evaluated.

  • Custom collection/dictionary classes are now serialized by a collection/dictionary serializer instead of the class map serializer. This means that the items will be serialized instead of the properties and fields. Any code relying on the old behaviour will need to use BsonClassMap.RegisterClassMap with their custom collection/dictionary to preserve the old behaviour.

  • The static Query class used to build queries has changed significantly. Users of this class can either modify their code or add a using statement alias to the old version. The DeprecatedQuery version will get dropped in version 2.0.

    using Query = MongoDB.Driver.Builders.DeprecatedQuery;
    

JIRA issues resolved

The full list of JIRA issues resolved in this release is available at:

https://jira.mongodb.org/secure/IssueNavigator.jspa?mode=hide&requestId=11900

High-Level Library Changes

Medium Trust

Support for medium trust is still not here. The communication protocol with a mongodb server is over TCP, which is dissallowed in vanilla medium trust environments. However, a slightly altered, custom medium trust permission system allowing sockets enables the driver to run fully. This can be done by copying the existing medium trust policy file and:

  • adding the SocketPermission:

    <SecurityClass Name="SocketPermission" Description="System.Net.SocketPermission, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"/>
    
  • adding an IPermission for the new SocketPermission security class:

    <IPermission class="SocketPermission" version="1" Unrestricted="true"/>
    

Support for Azure partial trust should work without changes. However, the default trust level for Azure is full, so this will only affect you if you have changed your Azure defaults.

Custom Collection Serialization

Classes that implement IDictionary or IDictionary<TKey, TValue> are now always serialized by a DictionarySerializer. Classes that implement IEnumerable ot IEnumerable< T > are now always serialized by a CollectionSerializer. We believe that a large majority of the time, classes implementing the collection interfaces intend for their items to be persisted rather than any properties (such as Count). This should enable the use of custom collection classes without any extra effort on your part.

Query Builder

We have rewritten the static Query class. The old Query class followed the odd query syntax of mongodb and was found to be somewhat unintuitive for those coming from traditional C# backgrounds and relational databases. In addition, as we completed the new typed Query< T > static class (discussed below) to aid the building of queries for classes that are using class maps underneath, we found that the difference in the old one and the new one was too stark.

In the older version, a complex query would be built as follows.

var query = Query.Or(
    Query.Exists("fn", true).NE("Jack"),
    Query.GTE("age", 20).LTE(40));

There are some implied "ands" for the two fields(name and age) that we wanted to remove so that the generated query was as predictable as possible. The new query syntax is a little more verbose, but we believe overall easier to understand.

var query = Query.And(
    Query.And(
        Query.Exists("fn"),
        Query.NE("fn", "Jack"))
    Query.And(
        Query.GTE("age", 20),
        Query.LET("age", 40)));

In many cases, you might find that you don't need to change anything, as the syntax is only different when a conjunction is chained. However, if you use this syntax a lot, then you can still use the old query builder by including a using statement in your files as follows:

using Query = MongoDB.Driver.Builders.DeprecatedQuery;

In version 2.0, we'll be removing the DeprecatedQuery class, so you'll need to update eventually.

Typed Builders

In conjunction with the new query builder, we have also included typed builders that mirror all the existing builders. So, Query has a corresponding Query< T > class, Update has a corresponding Update< T > class, etc... The huge benefit to this is that you can remove your "magic" strings from your code! In addition, anyone using custom serializers with class maps has support built-in for value based comparisons.

For instance, given that we have a Person class defined:

public class Person
{
    [BsonRepresentation(BsonType.ObjectId)]
    public string Id { get; set;}

    [BsonElement("fn")]
    public string FirstName { get; set;}

    [BsonElement("ln")]
    public string LastName { get; set;}

    [BsonElement("age")]
    public int Age { get; set;}
}   

Without the typed builder, a typical query might look like this:

ObjectId idFromUserInput = ...;
var query = Query.And(
    Query.NE("_id", idFromUserInput),
    Query.EQ("fn", "Jack"));

In the above query for a person, we need special knowledge to construct a valid query. First, that "Id" is "_id" in mongodb. Second, that "_id" is an ObjectId, even though our class exposes it as a string. And third, that "FirstName" is "fn" in mongodb. With the typed builders, you can specify this configuration information in one place, either as attributes or through the fluent configuration api, and never need to think about it again, as is demonstrated below, where the exact same query is generated as above.

string idFromUserInput = ...;
var query = Query.And(
    Query<Person>.NE(p => p.Id, idFromUserInput),
    Query<Person>.EQ(p => p.FirstName, "Jack"));

In addition, the typed query builders are type-safe, so you can't put an integer value where you have declared your property as a string. However, the biggest benefit to our internal refactoring is that you can now express your queries as predicates, making the above query even easier and more readable.

string idFromUserInput = ...;
var query = Query<Person>.Where(p => p.Id != idFromUserInput && p.FirstName == "Jack");

LINQ Enhancements

We continue to make Linq improvements. Thanks to all who report missing features and problematic queries. Linq is difficult to implement because IQueryable provides a lot of flexibility and operators that simply aren't supported in MongoDB. Where implementation makes sense, we will continue to enhance our linq implementations. As such, we have implemented a number of new operators:

  • Added support for & and | operators when both sides evaluate to a boolean.

  • Added support for the Any operator when the target is an enumerable of documents. This will generate an $elemMatch query. We do NOT support targets that are enumerables of primitives because the mongodb server does not support those. As soon as the server supports this, we will add this in as well.

  • Using ToUpper or ToLower will generate a case-insentive query to mongodb using a regular expression.

  • There are a number of times when certain queries will always evaluate to false. These queries will generate a special query that will utilize an index when possible, but still always evaluate to false on the server. Don't be surprised to see this query in your query plans: { "_id" : { $type : -1 } }. In addition, there are some queries that always evaluate to true. These will generate an empty query document: { }.

    // { "_id" : { $type : -1 } }
    
    var query = Query<Person>.Where(p => p.Name.ToUpper() == "Abc");
    
  • Nullable Enums are now supported.

  • ContainsKey on any typed impementing IDictionary or IDictionary<K,V> is now supported and will generate a query corresponding to it's serialization format.

  • Contains can now be used on any type implementing IEnumerable or IEnumerable< T > and will be serialized to it's corresponding form. In the case of a local collection containing a field, this would generate an $in clause.

    var local = new List<int> { 1, 2, 3};
    
    // {"Age" : { $in : [1,2,3] } }
    
    var query = from p in people.AsQueryable()
            where local.Contains(p.Age)
            select p;
    
  • Type queries either via comparison

    Query<A>.Where(a => a.GetType() == typeof(T)) 
    

    or in LINQ

    collection.AsQueryable().OfType<T>() 
    

    are supported for those of you using inheritance in your class maps. These will generate queries where the type discriminator is checked for the proper value.

GridFS Changes

In previous releases, download...

Read more

1.4.2

07 Aug 15:59
Compare
Choose a tag to compare

C# Driver Version 1.4.2 Release Notes

This minor release fixes a few issues found in the 1.4.1 release.

File by file change logs are available at:

https://github.com/mongodb/mongo-csharp-driver/blob/master/Release%20Notes/Change%20Log%20v1.4.2-Bson.txt
https://github.com/mongodb/mongo-csharp-driver/blob/master/Release%20Notes/Change%20Log%20v1.4.2-Driver.txt

These release notes describe the changes at a higher level, and omit describing
some of the minor changes.

Breaking changes

After 1.4.1 was released it was discovered that there were some minor breaking
changes. The breaking changes were in methods that we considered to be internal,
but that were not made private so that they leaked out into the public API.
Those methods have now been marked obsolete and will be made private in
a future release. The 1.4.2 release restores backward compatibility for these
methods (GetDocumentId and SetDocumentId in BsonDocument).

JIRA issues resolved

The full list of JIRA issues resolved in this release is available at:

https://jira.mongodb.org/secure/IssueNavigator.jspa?mode=hide&requestId=11409

BSON library changes

GetDocumentId/SetDocumentId marked obsolete

These methods were intended to be private. They have been marked as obsolete
and will be made private in a future release.

Driver changes

Query.All/In/NotIn

There was an issue with Query.All/In/NotIn that might have affected you. If you
cast a BsonArray to IEnumerable<BsonValue> before calling Query.All/In/NotIn
you would get an exception. This only happened when casting a BsonArray to
IEnumerable<BsonValue>. If you passed a BsonArray to the BsonArray overload or
passed an IEnumerable<BsonValue> that was not a BsonArray to the
IEnumerable<BsonValue> overload no exception was thrown.

RequestStart/RequestDone

Calling RequestStart when the connection pool was oversubscribed would often
result in a deadlock. This has been fixed in the 1.4.2 release.

Ping/VerifyState

These methods are usually called from a timer to monitor the state of the
server (or of multiple servers if connected to a replica set), but you can
also call them yourself. These methods now use a new connection instead
of one from the connection pool so that they are not delayed waiting for a
connection when the connection pool is oversubscribed.

1.4.1

07 Aug 15:56
Compare
Choose a tag to compare

C# Driver Version 1.4.1 Release Notes

This minor release fixes a few issues found by users of the LINQ support added
in v1.4 of the C# driver and also adds support for a few new LINQ query
operators and where clauses.

File by file change logs are available at:

https://github.com/mongodb/mongo-csharp-driver/blob/master/Release%20Notes/Change%20Log%20v1.4.1-Bson.txt
https://github.com/mongodb/mongo-csharp-driver/blob/master/Release%20Notes/Change%20Log%20v1.4.1-Driver.txt

These release notes describe the changes at a higher level, and omit describing
some of the minor changes.

Breaking changes

There are no breaking changes in this release.

JIRA issues resolved

The full list of JIRA issues resolved in this release is available at:

https://jira.mongodb.org/secure/IssueNavigator.jspa?mode=hide&requestId=11397

LINQ query support

The main purpose of this minor release is to fix some issues found by users of
the new LINQ support added in v1.4.

One bug that many have encountered is a NullReferenceException when writing a
query against an inherited property.

https://jira.mongodb.org/browse/CSHARP-418

You would hit this error if you had any queries that were similar to this:

public class B
{
    public ObjectId Id; 
}

public class C : B
{
    public int X;
}

var query =
    from c in collection.AsQueryable<C>()
    where c.Id = id // class C inherits Id from class B
    select c;

Another bug that a few users have encountered is an ArgumentOutOfRangeException
when writing a LINQ query that consists of a bare AsQueryable and nothing else:

https://jira.mongodb.org/browse/CSHARP-419

as in this sample:

var query = collection.AsQueryable<C>(); // no where clause

Normally a query would contain something else besides the call to AsQueryable
(like a where clause), but this is a legal query and is now supported.

BSON library changes

MaxSerializationDepth

The BSON serialization mechanism does not support circular references in your
object graph. In earlier versions of the C# driver if you attempted to
serialize an object with circular references you would get a
StackOverflowExpection. The 1.4.1 version now tracks the serialization depth
as it serializes an object and if it exceeds MaxSerializationDepth a
BsonSerializationException is thrown. The problem with StackOverflowException
was that it was fatal to your process, but the BsonSerializationException can
be caught and your process can continue executing if you choose.

The default MaxSerializationDepth is 100.

Interpretation of C# null vs BsonNull.Value

When working with the BsonDocument object model a C# null is usually ignored,
specially when creating BsonDocuments using functional construction. However,
when mapping between .NET types and the BsonDocument object model a C# null
will now be mapped to a BsonNull. For example:

var dictionary = new Dictionary<string, object> { { "x", null } };
var document = new BsonDocument(dictionary);
// document["x"] == BsonNull.Value

and when mapping in the reverse direction a BsonNull will map to a C# null:

var document = new BsonDocument { { "x", BsonNull.Value } };
var dictionary = document.ToDictionary();
// dictionary["x"] == null

Usually mapping between .NET types and the BsonDocument object model happens
automatically as needed, but if you want to invoke the mapping yourself you
can access the BsonTypeMapper directly:

var dictionary = new Dictionary<string, object> { { "x", null } };
var document = BsonTypeMapper.MapToBsonValue(dictionary);
// document["x"] == BsonNull.Value

or in the other direction:

var document = new BsonDocument { { "x", BsonNull.Value } };
var dictionary = (IDictionary<string, object>)BsonTypeMapper.MapToDotNetValue(document);
// dictionary["x"] == null

Serializing read-only properties

The class map based serialization support normally serializes only public
read-write properties (or fields). Sometimes it can be useful to serialize
read-only properties as well, specially if you want to query against them.
You can now opt-in your read-only properties so that they appear in the
serialized document. For example:

public class Book
{
    public ObjectId Id;
    public string Title;
    [BsonElement] // opt-in the read-only LowercaseTitle property
    public string LowercaseTitle { get { return Title.ToLower(); } }
}

Now when a Book is serialized the document will look like:

{
    _id : ObjectId("4f8d771dae879111d289dbc0"),
    Title : "For Whom the Bell Tolls",
    LowercaseTitle : "for whom the bell tolls"
}

During deserialization any elements in the serialized document that
correspond to read-only properties are ignored.

Driver changes

MongoServer

There is a new method called IsDatabaseNameValid that you can call to test if
a database name is valid.

MongoDatabase

There is a new method called IsCollectionNameValid that you can call to test if a
collection name is valid.

MongoGridFS

You can now disable computing the MD5 at the server when uploading a GridFS
file. You can also disable the client side verification of the MD5 that is
normally done on Upload or Download. The reason you might choose to disable
MD5 verification is that it is computationally expensive to compute the MD5.

LINQ OfType query operator

You can now use the OfType query operator in LINQ queries. For example:

var query = collection.AsQueryable<B>().OfType<C>();

this generates a query against the "_t" discriminator value that is used to
identify the actual type of a serialized document.

Additional expressions supported in LINQ where clauses

The following expressions are now supported in LINQ where clauses:

// d is the document
// p is a property of the document
// c is a character constant
// ca is an array of character constants
// s is a string constant
// i, j, k, n are integer constants

where d.p.Equals(constant)
where string.IsNullOrEmpty(d.p)
where d.p.IndexOf(c) == i
where d.p.IndexOf(c, j) == i
where d.p.IndexOf(c, j, k) == i
where d.p.IndexOf(s) == i
where d.p.IndexOf(s, j) == i
where d.p.IndexOf(s, j, k) == i
where d.p.IndexOfAny(ca) == i
where d.p.IndexOfAny(ca, j) == i
where d.p.IndexOfAny(ca, j, k) == i
where d.p[i] == c
where d.p.Length == n
where d.p.ToLower().Contains("xyz")
where d.p.ToLower().StartsWith("xyz")
where d.p.ToLower().EndsWith("xyz")
where d.p.ToUpper().Contains("xyz")
where d.p.ToUpper().StartsWith("xyz")
where d.p.ToUpper().EndsWith("xyz")
where d.p.Trim().Contains("xyz")
where d.p.Trim().StartsWith("xyz")
where d.p.Trim().EndsWith("xyz")
where d.p.TrimStart().Contains("xyz")
where d.p.TrimStart().StartsWith("xyz")
where d.p.TrimStart().EndsWith("xyz")
where d.p.TrimEnd().Contains("xyz")
where d.p.TrimEnd().StartsWith("xyz")
where d.p.TrimEnd().EndsWith("xyz")
where d.GetType() == typeof(T)
where d is T

// you can use any combination of ToLower/ToUpper/Trim/TrimStart/TrimEnd
// before Contains/StartsWith/EndsWith

In the 1.4 version of the C# driver the constant always had to appear on the
right of a comparison operator. That restriction is lifted in 1.4.1 so now the
following are equivalent:

where d.Height < 60
where 60 > d.Height

Type of in AsQueryable can now be deduced

The type of in the call to AsQueryable can now be deduced from the collection argument:

var collection = database.GetCollection<MyDocument>("mydocuments")
var query = collection.AsQueryable(); // <T> is deduced to be MyDocument