2

I'm using MongoDB Atlas in my .net core application, using c# driver to connect the database, recently came to know about Atlas Search and I'm able to create an index for my collections, Is there a way to use $searchbeta from my application to query my index?

Annamalai D
  • 859
  • 1
  • 7
  • 21

2 Answers2

1

You can use the aggregate method and BsonDocument.Parse:

var pipeline = BsonDocument.Parse("{ $searchBeta: { search: { path: 'foo', query : 'bar' } }}");
var result = col.Aggregate<BsonDocument>(pipeline);
Doug
  • 14,387
  • 17
  • 74
  • 104
  • when i use this i get compilation error as below: Severity Code Description Project File Line Suppression State Error CS0411 The type arguments for method 'IMongoCollection.Aggregate(PipelineDefinition, AggregateOptions, CancellationToken)' cannot be inferred from the usage. Try specifying the type arguments explicitly. – Annamalai D Mar 02 '20 at 13:22
  • added type argument – Doug Mar 02 '20 at 17:00
1

You can use Aggregation Pipeline

For c# driver see Pipelines section in the Definitions and Builders docs

var pipeline = new BsonDocument[] 
{
    BsonDocument.Parse("{ $searchBeta: ... }"),
    BsonDocument.Parse("{ $sort: ... }")
};
var result = _db.GetCollection<Person>("people").Aggregate<Person>(pipeline).ToList();
Pavel Shastov
  • 2,657
  • 1
  • 18
  • 26