Mr Jones
Mr Jones

Reputation: 181

How to search for a string in an array of objects in MongoDB with C#

I have the following record in my MongoDB. I want to search for a string in the "monday" fields. I want to search the "learningexperience" field to return any documents that matches a certain word in that field.

Not sure how to do it using MongoDB/C#. Any help would be fantastic.

{
    "_id" : ObjectId("59ee623844bd6b042809d492"),
    "title" : "Zebra Room (2017-10-30)",
    "weekcommencing" : "2017-10-30", 
    "monday" : [ 
        {
            "categoryid" : "59ee610244bd6b042809d48d",
            "category" : "Culture & Community",
            "learningexperience" : "Dress up to reflect where i am from. Children are asked to dress in the national uniform of their origins."
        }, 
        {
            "categoryid" : "59edca4344bd6d1e200c3c32",
            "category" : "Literacy & Numeracy",
            "learningexperience" : "Today the children will be focusing on simple additions. For those more advance we will also have subtractions in place."
        }
]
}

Upvotes: 1

Views: 846

Answers (1)

If you want to search it with something like %keyword%. You may try this

db.tablename.find({"monday.learningexperience": /.*keyword.*/})

For C# you may try this filter (mongodb c# driver 2.0 or higher )

Builders<tablename>.Filter.Regex("monday.learningexperience", new BsonRegularExpression("keyword", "i"));

Upvotes: 1

Related Questions