Search code examples
c#mongodbchangestream

Mongodb c# changestream , How can I use Array variable instead of defining values in [] in $in , in filter


I am working on a program which creates multiple change stream, so I am working on doing the same in single stream rather than creating multiple watches, my sessionValues contains multiple ids,

 string[] sessionValues = list.ToArray();
                 filter = "{ $and: [ { operationType: 'insert' }, " + "{ 'fullDocument.appId' : {$in:['" + sessionValues + "']}} ] }";


                    var pipeline = new EmptyPipelineDefinition<ChangeStreamDocument<BsonDocument>>().Match(filter);

                // var changeStream = Collection.Watch(pipeline, options).ToEnumerable().GetEnumerator();

                DBObjects.DB.WriteLog(DBObjects.LogLevel.Info, "Log data change stream is now configured");
                    var cursor = Collection.Watch(pipeline, options);
                    flag = true;

                    var enumerator = cursor.ToEnumerable().GetEnumerator();                    

I want to use $in to find in my sessionValues variable rather than defining values in conventional [] way, coz I wont be able to define as I am adding values in my array. and want $in to find in array.


Solution

  • Because my format was bson, i managed to do it with the following,

    string sessionValues = "";
    foreach (var item in array)
    {
    sessionValues += "'" + item + "',";
    }
    filter = "{ $and: [ { operationType: 'insert' }, " + "{ 'fullDocument.appId' : {$in:[" + sessionValues + "]}} ] }";
    

    the above method worked perfectly fine for me, the values in the list were fetching properly.