Compass Atlas Search pipeline export

It does not work

If you have an Atlas generated Export Pipeline like
var pipeline = new BsonArray{ … }.

How do you use it with collection.Aggregate<BSonDocument(piprline) ???
There is no way to convert BsonArray to BSonDocument…

Thanks,
Eddy

Hi @Eddy_Hahn,

Just to clarify, is your question relating to a direct copy and paste of the contents from Compass for C# for aggregation pipelines?

For your reference, I have the following sample document in my test environment using the default search index definition:

{ name: 'john smith' }

From Compass:

Shell pipeline (from export to language view):

[
  {
    $search:
      {
        index: "default",
        text: {
          query: "john",
          path: "name"
        }
      }
  }
  {
    $project:
      {
        _id: 0
      }
  }
]

To C# (exported pipeline):

new BsonArray
{
    new BsonDocument("$search", 
    new BsonDocument
        {
            { "index", "default" }, 
            { "text", 
    new BsonDocument
            {
                { "query", "john" }, 
                { "path", "name" }
            } }
        }),
    new BsonDocument("$project", 
    new BsonDocument("_id", 0))
}

Example code snippet for an exported BsonArray from Compass that has a $search and $project stage:

namespace tour
{
    class Program
    {
        static void Main(string[] args)
        {
            var uri = "mongodb+srv://USER:PASS@Cluster0.abcde.mongodb.net/?retryWrites=true&w=majority";
            var mongoURL = new MongoUrl(uri);
            var client = new MongoClient(mongoURL);
            var database = client.GetDatabase("test");            
            var collection = database.GetCollection<BsonDocument>("c");

            BsonArray pipelineArray = new BsonArray
            {
                new BsonDocument("$search", 
                new BsonDocument
                    {
                        { "index", "default" }, 
                        { "text", 
                new BsonDocument
                        {
                            { "query", "john" }, 
                            { "path", "name" }
                        } }
                    }),
                new BsonDocument("$project", 
                new BsonDocument("_id", 0))
            };
          
          	var pipeline = pipelineArray.Cast<BsonDocument>().ToList();
          	var resultList = collection.Aggregate<BsonDocument>(pipeline).ToList();

            foreach (var doc in resultList)
            {
                Console.WriteLine(doc); 
            }
          	
            Console.WriteLine("Finished!");
        }
    }
}

Output:

{ "name" : "john smith" }
Finished!

In saying the above, it might be worth splitting the pipeline stages for readability, maintainability and re-usability (generally). One example of this is here. From Compass, you could take the contents of the BsonArray from the export copy/paste and split those into separate stages in your code.

Hope the above helps in some way.

You may wish to also check out the Search builder as well.

Best Regards,
Jason