flyerno1
flyerno1

Reputation: 1

Is it possible to get an element's position in an array through MongoDB C# Query?

I store instances of a .NET class in a MongoDB collection via C# drivers. The class contains an array for example - MyArray. Is it possible to query a certain string(element)'s position in the array with the MongoDB C# driver? Practical example is I want to find the documents in the collection whose MyArray property has a certain string on the position 0.

Thanks a lot.

the .NET Class:

    namespace TestClasses
{
     public class TestClass
    {
        public TestClass() {

        }

        public string[] MyArray
        {
            get;
            set;
        }

        protected string Name
        {
            get;
            set;
        }

    }
}

Upvotes: 0

Views: 858

Answers (1)

jeffsaracco
jeffsaracco

Reputation: 1269

Yes, by using the dot notation in your query (where searchValue is the value you are looking):

MongoServer server = MongoServer.Create(host);
MongoDatabase db = server.GetDatabase(databaseName);
MongoCollection Collection = db.GetCollection("collection");

var query = new QueryDocument("MyArray.0", searchValue);
var testCollection = matchCollection.FindAs<TestClass>(query);

Upvotes: 1

Related Questions