you can find the C# code example : by set the **Select your language** drop-down menu to set the language of the examples in this page.
on https://www.mongodb.com/docs/atlas/atlas-vector-search/vector-search-stage/#std-label-vectorSearch-agg-pipeline-filter
`
using System.Reflection.Emit;
using MongoDB.Bson;
using MongoDB.Bson.Serialization.Attributes;
using MongoDB.Bson.Serialization.Conventions;
using MongoDB.Driver;
using MongoDB.Driver.Search;
public class vectorSearchFilterQuery
{
// define connection to your Atlas cluster
private const string MongoConnectionString = "<connection-string>";
public static void Main(string[] args){
var camelCaseConvention = new ConventionPack { new CamelCaseElementNameConvention() };
ConventionRegistry.Register("CamelCase", camelCaseConvention, type => true);
// connect to your Atlas cluster
var mongoClient = new MongoClient(MongoConnectionString);
// define namespace
var moviesDatabase = mongoClient.GetDatabase("sample_mflix");
var moviesCollection = moviesDatabase.GetCollection<EmbeddedMovie>("embedded_movies");
// define vector embeddings to search
var vector = new[] {0.02421053,...};
// define filter
var yearGtFilter = Builders<EmbeddedMovie>.Filter.Gt("year", 1955);
var yearLtFilter = Builders<EmbeddedMovie>.Filter.Lt("year", 1975);
// define options
var options = new VectorSearchOptions<EmbeddedMovie>() {
Filter = Builders<EmbeddedMovie>.Filter.And(yearGtFilter, yearLtFilter),
IndexName = "vector_index",
NumberOfCandidates = 150
};
// run query
var results = moviesCollection.Aggregate()
.VectorSearch(m => m.Embedding, vector, 10, options)
.Project(Builders<EmbeddedMovie>.Projection
.Include(m => m.Title)
.Include(movie => movie.Plot)
.Include(movie => movie.Year))
.ToList();
// print results
foreach (var movie in results)
{
Console.WriteLine(movie.ToJson());
}
}
}
[BsonIgnoreExtraElements]
public class EmbeddedMovie
{
[BsonIgnoreIfDefault]
public string Title { get; set; }
public string Plot { get; set; }
public int Year { get; set; }
[BsonElement("plot_embedding")]
public double[] Embedding { get; set; }
}