MongoDB.live, free & fully virtual. June 9th - 10th. Register Now MongoDB.live, free & fully virtual. June 9th - 10th. Register Now

Can Atlas Search be used from the C# driver?

Can this feature be used from the C# driver?

Hi @Graeme_Henderson,

Yes, Atlas search queries take the form of an aggregation pipeline stage $seachBeta. For example:

var pipeline = new BsonDocument[] 
{
    new BsonDocument ( "$searchBeta", new BsonDocument(
      "search", new BsonDocument(
          "query", "foobar").Add("path", "field")
           ).Add(
       "highlight", new BsonDocument(
           "path", "field")
        )
    )
};
var aggregation = collection.Aggregate<BsonDocument>(pipeline);

Note that you can also build aggregation pipeline using MongoDB Compass and then Export Pipeline to Specific Programming Language i.e. C# to help you build the aggregation pipeline.

See also Tutorial: Build a Movie Search Engine Using Atlas Full-Text Search in 10 Minutes

Regards,
Wan.

1 Like