你的位置:首页 > 软件开发 > 数据库 > mongodb driver c#语法

mongodb driver c#语法

发布时间:2016-06-20 00:00:07
Definitions and BuildersThe driver has introduced a number of types related to the specification of filters, updates, projections, sorts, an ...

Definitions and BuildersMost of the definitions also have builders to aid in their creation. Each builder has a generic type parameter TDocument which represents the type of document with which you are working. It will almost always match the generic TDocument parameter used in an IMongoCollection<TDocument>.FieldsFieldDefinition<BsonDocument> field = "fn";class Person    [BsonElement("ln")]FieldDefinition<Person> field = "FirstName";FieldDefinition<Person> field = "fn";FiltersFilterDefinition<BsonDocument> filter = "{ x: 1 }";// orFilterDefinition<BsonDocument> filter = new BsonDocument("x", 1);Filter Definition BuilderThe FilterDefinitionBuilder<TDocument> provides a type-safe API for building up both simple and complex MongoDB queries.For example, to build up the filter { x: 10, y: { $lt: 20 } }, the following calls are all equivalent.var builder = Builders<BsonDocument>.Filter;class Widget    [BsonElement("y")]var builder = Builders<Widget>.Filter;Alternatively, you can elect to use a string-based field name instead.var filter = builder.Eq("X", 10) & builder.Lt("Y", 20);// orvar filter = builder.Eq("x", 10) & builder.Lt("y", 20);Array OperatorsGiven the following class:public class Postvar filter = Builders<Post>.Filter.AnyEq(x => x.Tags, "mongodb");// This will NOT compile:For example:PipelineDefinition pipeline = new BsonDocument[] ProjectionsEach projection definition is implicity convertible from both a JSON string as well as a BsonDocument.ProjectionDefinition<BsonDocument> projection = "{ x: 1 }";// orProjectionDefinition<BsonDocument> projection = new BsonDocument("x", 1);Projection Definition BuilderThe ProjectionDefinitionBuilder<TDocument> exists to make it easier to build up 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");class Widget    [BsonElement("x")]    [BsonElement("y")]var projection = Builders<Widget>.Projection.Include("X").Include("Y").Exclude("Id");// orvar projection = Builders<Widget>.Projection.Include("x").Include("y").Exclude("_id");// orvar projection = Builders<Widget>.Projection.Include(x => x.X).Include(x => x.Y).Exclude(x => x.Id);// orvar projection = Builders<Widget>.Projection.Expression(x => new { X = x.X, Y = x.Y });Lambda ExpressionsFindWhen a Find projection is defined using a lambda expression, it is run client-side. The driver inspects the lambda expression to determine which fields are referenced and automatically constructs a server-side projection to return only those fields.Given the following class:class Widget    [BsonElement("x")]    [BsonElement("y")]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);AggregateWhen an aggregate projection is 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 that all expressions in a projection for the Aggregation Framework must be expressible on the server.GroupingA 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 are also supported.SortsSortDefinition<BsonDocument> sort = "{ x: 1 }";// orSortDefinition<BsonDocument> sort = new BsonDocument("x", 1);Sort Definition BuilderThe SortDefinitionBuilder<TDocument> provides a type-safe API for building up MongoDB sort syntax.For example, to build up the sort { x: 1, y: -1 }, do the following:var builder = Builders<BsonDocument>.Sort;class Widget    [BsonElement("y")]var builder = Builders<Widget>.Sort;// orvar sort = builder.Ascending("X").Descending("Y");// orvar sort = builder.Ascending("x").Descending("y");// invocation// orUpdateDefinition<BsonDocument> update = new BsonDocument("$set", new BsonDocument("x", 1));Update Definition BuilderThe UpdateDefinitionBuilder<TDocument> provides a type-safe API for building the MongoDB update specification.For example, to build up the update { $set: { x: 1, y: 3 }, $inc: { z: 1 } }, do the following:var builder = Builders<BsonDocument>.Update;class Widget    [BsonElement("y")]    [BsonElement("z")]var builder = Builders<Widget>.Update;// orvar update = builder.Set("X", 1).Set("Y", 3).Inc("Z", 1);// orvar update = builder.Set("x", 1).Set("y", 3).Inc("z", 1);IndexKeysDefinition<BsonDocument> keys = "{ x: 1 }";// orIndexKeysDefinition<BsonDocument> keys = new BsonDocument("x", 1);Index Keys Definition BuilderThe 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;class Widget    [BsonElement("y")]var builder = Builders<Widget>.IndexKeys;// orvar keys = builder.Ascending("X").Descending("Y");// orvar keys = builder.Ascending("x").Descending("y");


原标题:mongodb driver c#语法

关键词:C#

C#
*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

可能感兴趣文章

我的浏览记录