MongoDB Driver C # syntax

Source: Internet
Author: User
Tags mongodb driver mongodb update sorts

Definitions and Builders
The driver have introduced a number of types related to the specification of filters, updates, projections, sorts, and Inde x keys. These types is used throughout the API.

Most of the definitions also has builders to aid in their creation. Each builder have a generic type parameter tdocument which represents the type of document with which is working. It'll almost always match the generic tdocument parameter used in an imongocollection<tdocument>.

Fields
Fielddefinition<tdocument> and Fielddefinition<tdocument, tfield> define how to get a field name. They is implicitly convertible from a string, so this can simply pass the field name you ' d like. For instance, to use the field named "FN" with a tdocument of bsondocument, do the following:

fielddefinition<bsondocument> field = "FN";
However, if working with a mapped class and then we is able to translate a string that equals the property name. For instance, given the below person class:

Class Person
{
[Bsonelement ("FN")]
public string FirstName {get; set;}

[Bsonelement ("ln")]
public string LastName {get; set;}
}
Since We know the type is person, we can provide the property name, FirstName, from the class and "FN" would still be used.

fielddefinition<person> field = "FirstName";
NOTE
We don ' t validate that the provided string exists as a mapped field, so it's still possible to provide a field that hasn ' T been mapped:

fielddefinition<person> field = "FN";
and the output field name of this would be just "FN".

Filters
Filterdefinition<tdocument> defines a filter. It is implicity convertible from both a JSON string as well as a bsondocument.

filterdefinition<bsondocument> filter = "{x:1}";

Or

filterdefinition<bsondocument> filter = new Bsondocument ("X", 1);
Both of these would render the filter {x:1}.

Filter Definition Builder
See the tests for examples.

The filterdefinitionbuilder<tdocument> provides a type-safe API for building up both simple and complex MongoDB Quer ies.

For example, to build the filter {x:10, y: {$lt: $}}, the following calls is all equivalent.

var builder = Builders<bsondocument>. Filter;
var filter = Builder. Eq ("X", "ten") & Builder. Lt ("Y", 20);
NOTE
The & operator is overloaded. Other overloaded operators include the | Operator for "or" and the! Operator for ' not '.
Given the following class:

Class Widget
{
[Bsonelement ("X")]
public int X {get; set;}

[Bsonelement ("Y")]
public int Y {get; set;}
}
You can achieve the same result in the typed variant:

var builder = Builders<widget>. Filter;
var filter = Builder. Eq (widgets = Widgets. X, ten) & Builder. Lt (widgets = Widgets. Y, 20);
The benefits to this form are the Compile-time safety inherent in using types. In addition, the your IDE can provide refactoring support.

Alternatively, you can elect-use a string-based field name instead.

var filter = Builder. Eq ("X", "ten") & Builder. Lt ("Y", 20);

Or

var filter = Builder. Eq ("X", "ten") & Builder. Lt ("Y", 20);
For more information in valid lambda expressions, see the expressions documentation.

Array Operators
When using entities with properties or fields, serialize to arrays, you can use the methods prefixed with "any" to COM Pare the entire array against a single item.

Given the following class:

public class Post
{
Public ienumerable<string> Tags {get; set;}
}
To see if any of the tags equals "MongoDB":

var filter = Builders<post>. Filter.anyeq (x = x.tags, "MongoDB");

This would not be compile:
var filter = Builders<post>. Filter.eq (x = x.tags, "MongoDB");
Pipelines
A pipeline definition defines an entire aggregation pipeline. It is implicitly convertible from a list<bsondocument>, a bsondocument, a list<ipipelinestagedefinition>, and A ipipelinestagedefinition[].

For example:

Pipelinedefinition pipeline = new bsondocument[]
{
New Bsondocument {"$match", New Bsondocument ("X", 1)},
New Bsondocument {{"$sort", New Bsondocument ("Y", 1)}}
};
NOTE
There is no builder for a pipelinedefinition. In most cases, the Iaggregatefluent<tdocument> interface would be used which are returned from the IMONGOCOLLECTION&L T Tdocument>. Aggregate method.

Projections
There is both forms of a projection definition:one where the type of the projection is known, projectiondefinition<tdo Cument, Tprojection>, and one where the type of the projection is not yet known, projectiondefinition<tdocument>. The latter, while implicitly convertible to the first, is merely used as a building block. The high-level APIs that take a projection would always take the former. This was because, when determining how to handle a projection client-side, it was not enough to know what fields and TRANSFO Rmations'll take place. It also requires that we know how to interpret the projected shape as a. NET type. Since The driver allows you to work with custom classes, it's imperative that any projection also include the "Interpreta tion instructions "for projecting to a custom class.

Each projection definition was implicity convertible from both a JSON string as well as a bsondocument.

Projectiondefinition<bsondocument> projection = "{x:1}";

Or

projectiondefinition<bsondocument> projection = new Bsondocument ("X", 1);
Both of these would render the projection {x:1}.

Projection Definition Builder
See the tests for examples.

The projectiondefinitionbuilder<tdocument> exists to make it easier-to-build projections in MongoDB ' s syntax. For the projection {x:1, y:1, _id:0}:

var projection = Builders<bsondocument>. Projection.include ("X"). Include ("Y"). Exclude ("_id");
Using the Widget class:

Class Widget
{
Public ObjectId Id {get; set;}

[Bsonelement ("X")]
public int X {get; set;}

[Bsonelement ("Y")]
public int Y {get; set;}
}
We can render the same projection in a couple of ways:

var projection = Builders<widget>. Projection.include ("X"). Include ("Y"). Exclude ("Id");

Or

var projection = Builders<widget>. Projection.include ("X"). Include ("Y"). Exclude ("_id");

Or

var projection = Builders<widget>. Projection.include (x = x.x). Include (x = x.y). Exclude (x = x.id);

Or

var projection = Builders<widget>. Projection.expression (x = = new {x = x.x, Y = x.y});
This last projection where we've used the Expression method is subtly different as was explained below, and its return type is a (projectiondefinition<tdocument, tprojection>) as opposed to the others which return a (PROJECTIONDEFINITION&L T tdocument>).

Lambda Expressions
The driver supports using expression trees to render projections. The same expression tree would sometimes render differently when used in a Find operation versus when used in an Aggregate Operation. Inherently, a lambda expression contains all the information necessary to form both the projection on the server as well a s The client-side result and requires no further information.

Find
See the tests for examples.

When a Find projection was defined using a lambda expression, it is run client-side. The driver inspects the lambda expression to determine which fields is referenced and automatically constructs a server-s IDE projection to return is those fields.

Given the following class:

Class Widget
{
Public ObjectId Id {get; set;}

[Bsonelement ("X")]
public int X {get; set;}

[Bsonelement ("Y")]
public int Y {get; set;}
}
The following lambda expressions would all result in the projection {x:1, y:1, _id:0}. This is because we inspect the The expression tree to discover all of the fields is used and the server to include them. We then run the lambda expression client-side. As such, Find projections support virtually the entire breadth of the C # language.

var projection = Builders<widget>. Projection.expression (x = = new {x = x.x, Y = x.y});

var projection = Builders<widget>. Projection.expression (x = new {Sum = x.x + x.y});

var projection = Builders<widget>. Projection.expression (x = new {AVG = (x.x + x.y)/2});

var projection = Builders<widget>. Projection.expression (x = (x.x + x.y)/2);
The _id field is excluded automatically if we know for certain that it isn ' t necessary, as was the case in all the above Examples.

Aggregate
See the tests for examples.

When an aggregate projection was defined using a lambda expression, a majority of the aggregation expression operators are Supported and translated. Unlike a project for Find, no part of the lambda expression is run client-side. This means, all expressions with a projection for the Aggregation Framework must is expressible on the server.

Grouping
See the tests for examples.

A projection is also used when performing grouping in the Aggregation Framework. In addition to the expression operators used in an aggregate projection, the aggregation accumulator operators is also SU Pported.

Sorts
Sortdefinition<tdocument> defines how to render a valid sort document. It is implicity convertible from both a JSON string as well as a bsondocument.

Sortdefinition<bsondocument> sort = "{x:1}";

Or

sortdefinition<bsondocument> sort = new Bsondocument ("X", 1);
Both of these would render the sort {x:1}.

Sort Definition Builder
See the tests for examples.

The sortdefinitionbuilder<tdocument> provides a type-safe API for building up MongoDB sort syntax.

For example, to build the sort {x:1, Y:-1}, do the following:

var builder = Builders<bsondocument>. Sort;
var sort = Builder. Ascending ("X"). Descending ("Y");
Given the following class:

Class Widget
{
[Bsonelement ("X")]
public int X {get; set;}

[Bsonelement ("Y")]
public int Y {get; set;}
}
We can achieve the same result in the typed variant:

var builder = Builders<widget>. Sort;
var sort = Builder. Ascending (x = x.x). Descending (x = x.y);

Or

var sort = Builder. Ascending ("X"). Descending ("Y");

Or

var sort = Builder. Ascending ("X"). Descending ("Y");
Updates
Updatedefinition<tdocument> defines how to render a valid update document. It is implicity convertible from both a JSON string as well as a bsondocument.

Invocation
updatedefinition<bsondocument> update = "{$set: {x:1}}";

Or

updatedefinition<bsondocument> update = new Bsondocument ("$set", New Bsondocument ("X", 1));
Both of these would render the update {$set: {x:1}}.

Update Definition Builder
See the tests for examples.

The updatedefinitionbuilder<tdocument> provides a type-safe API for building the MongoDB update specification.

For example, to-build the update {$set: {x:1, Y:3}, $inc: {z:1}}, do the following:

var builder = Builders<bsondocument>. Update;
var update = Builder. Set ("x", 1). Set ("Y", 3). INC ("Z", 1);
Given the following class:

Class Widget
{
[Bsonelement ("X")]
public int X {get; set;}

[Bsonelement ("Y")]
public int Y {get; set;}

[Bsonelement ("Z")]
public int Z {get; set;}
}
We can achieve the same result in a typed variant:

var builder = Builders<widget>. Update;
var update = Builder. Set (widget = = Widget. X, 1). Set (widget = = Widget. Y, 3). INC (widget = widgets). Z, 1);

Or

var update = Builder. Set ("X", 1). Set ("Y", 3). INC ("Z", 1);

Or

var update = Builder. Set ("x", 1). Set ("Y", 3). INC ("Z", 1);
Index Keys
Indexkeysdefinition<tdocument> defines the keys for index. It is implicity convertible from both a JSON string as well as a bsondocument.

indexkeysdefinition<bsondocument> keys = "{x:1}";

Or

indexkeysdefinition<bsondocument> keys = new Bsondocument ("X", 1);
Both of these would render the keys {x:1}.

Index Keys Definition Builder
See the tests for examples.

The indexkeysdefinitionbuilder<tdocument> provides a type-safe API to build an index keys definition.

For example, to build up the keys {x:1, Y:-1}, do the following:

var builder = Builders<bsondocument>. Indexkeys;
var keys = Builder. Ascending ("X"). Descending ("Y");
Given the following class:

Class Widget
{
[Bsonelement ("X")]
public int X {get; set;}

[Bsonelement ("Y")]
public int Y {get; set;}
}
We can achieve the same result in the typed variant:

var builder = Builders<widget>. Indexkeys;
var keys = Builder. Ascending (x = x.x). Descending (x = x.y);

Or

var keys = Builder. Ascending ("X"). Descending ("Y");

Or

var keys = Builder. Ascending ("X"). Descending ("Y");

MongoDB Driver C # syntax

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.