« Mongodb and csharp » : différence entre les versions
De Banane Atomic
Aller à la navigationAller à la recherche
(Page créée avec « Category:.NET = Filter = <kode lang='cs'> var builder = Builders<MyClass>.Filter; var filter = builder.In(x => x.Property1, Ids); // filter by Property1 in the Ids list </kode> ») |
Aucun résumé des modifications |
||
(8 versions intermédiaires par le même utilisateur non affichées) | |||
Ligne 1 : | Ligne 1 : | ||
[[Category:.NET]] | [[Category:.NET]] | ||
= Links = | |||
* [https://www.mongodb.com/developer/languages/csharp/using-linq-query-mongodb-dotnet-core-application Using LINQ to Query MongoDB in a .NET Core Application] | |||
* [https://www.mongodb.com/developer/languages/csharp/create-restful-api-dotnet-core-mongodb Create a RESTful API with .NET Core and MongoDB] | |||
* [https://www.mongodb.com/developer/languages/csharp/joining-collections-mongodb-dotnet-core-aggregation-pipeline Joining Collections in MongoDB with .NET Core and an Aggregation Pipeline] | |||
* [https://www.mongodb.com/developer/languages/csharp C# and MongoDB] | |||
= Access to a collection = | |||
<kode lang='cs'> | |||
var mongoClient = new MongoClient("MONGODB_CONNECTION_STRING"); | |||
var database = mongoClient.GetDatabase("DB_NAME"); | |||
var collection = database.GetCollection<Collection>("COLLECTION_NAME"); | |||
</kode> | |||
= Filter = | = Filter = | ||
<kode lang='cs'> | <kode lang='cs'> | ||
var builder = Builders<MyClass>.Filter; | var builder = Builders<MyClass>.Filter; | ||
FilterDefinition<MyClass> filter = filterDefinition.Empty; | |||
filter &= builder.Eq(x => x.Property1, "id"); | |||
filter &= builder.In(x => x.Property1, new[] { "id1", "id2" }); // filter by Property1 equals to one of the ids | |||
var result = (await collection.FindAsync(filter)).ToList(); | |||
</kode> | |||
= Pipeline = | |||
<kode lang='cs'> | |||
var stages = new BsonDocument[] | |||
{ | |||
MatchStage(getTimeSeriesQuery), | |||
ProjectionStage(), | |||
SortStage() | |||
}; | |||
var pipeline = PipelineDefinition<InputClass, OutputClass>.Create(stages); | |||
var results = (await myCollection.AggregateAsync(pipeline)).List(); | |||
BsonDocument MatchStage(GetTimeSeriesQuery getTimeSeriesQuery) | |||
{ | |||
var match = new BsonDocument("property1", "value1"); | |||
if (!string.IsNullOrEmpty(variable2)) | |||
{ | |||
match.AddRange( | |||
new BsonDocument("property2", variable2) | |||
); | |||
} | |||
return new BsonDocument("$match", match); | |||
} | |||
BsonDocument ProjectionStage() => | |||
new BsonDocument( | |||
"$project", | |||
new BsonDocument | |||
{ | |||
{ "_id", 0 }, | |||
{ "property1", 1 }, | |||
{ "property2", 1 } | |||
} | |||
); | |||
BsonDocument SortStage() => | |||
new BsonDocument("$sort", new BsonDocument("timestamp", 1)); | |||
</kode> | |||
= [https://stackoverflow.com/questions/30333925/how-do-i-log-my-queries-in-mongodb-c-sharp-driver-2-0 Log] = | |||
<kode lang='cs'> | |||
var mongoUrl = new MongoUrl("MONGODB_CONNECTION_STRING"); | |||
var mongoClientSettings = MongoClientSettings.FromUrl(mongoUrl); | |||
mongoClientSettings.ClusterConfigurator = cb => | |||
{ | |||
cb.Subscribe<CommandStartedEvent>(e => | |||
{ | |||
logger.LogTrace($"{e.CommandName} - {e.Command.ToJson()}"); | |||
}); | |||
}; | |||
this.mongoClient = new MongoClient(mongoClientSettings); | |||
</kode> | </kode> | ||
Configure the [[Asp.net_core_7#Log|Log]] so it will be displayed in Visual Studio Output Debug window. |
Version du 15 avril 2024 à 08:55
Links
- Using LINQ to Query MongoDB in a .NET Core Application
- Create a RESTful API with .NET Core and MongoDB
- Joining Collections in MongoDB with .NET Core and an Aggregation Pipeline
- C# and MongoDB
Access to a collection
var mongoClient = new MongoClient("MONGODB_CONNECTION_STRING"); var database = mongoClient.GetDatabase("DB_NAME"); var collection = database.GetCollection<Collection>("COLLECTION_NAME"); |
Filter
var builder = Builders<MyClass>.Filter; FilterDefinition<MyClass> filter = filterDefinition.Empty; filter &= builder.Eq(x => x.Property1, "id"); filter &= builder.In(x => x.Property1, new[] { "id1", "id2" }); // filter by Property1 equals to one of the ids var result = (await collection.FindAsync(filter)).ToList(); |
Pipeline
var stages = new BsonDocument[] { MatchStage(getTimeSeriesQuery), ProjectionStage(), SortStage() }; var pipeline = PipelineDefinition<InputClass, OutputClass>.Create(stages); var results = (await myCollection.AggregateAsync(pipeline)).List(); BsonDocument MatchStage(GetTimeSeriesQuery getTimeSeriesQuery) { var match = new BsonDocument("property1", "value1"); if (!string.IsNullOrEmpty(variable2)) { match.AddRange( new BsonDocument("property2", variable2) ); } return new BsonDocument("$match", match); } BsonDocument ProjectionStage() => new BsonDocument( "$project", new BsonDocument { { "_id", 0 }, { "property1", 1 }, { "property2", 1 } } ); BsonDocument SortStage() => new BsonDocument("$sort", new BsonDocument("timestamp", 1)); |
Log
var mongoUrl = new MongoUrl("MONGODB_CONNECTION_STRING"); var mongoClientSettings = MongoClientSettings.FromUrl(mongoUrl); mongoClientSettings.ClusterConfigurator = cb => { cb.Subscribe<CommandStartedEvent>(e => { logger.LogTrace($"{e.CommandName} - {e.Command.ToJson()}"); }); }; this.mongoClient = new MongoClient(mongoClientSettings); |
Configure the Log so it will be displayed in Visual Studio Output Debug window.