如何使用C#在MongoDB中的对象数组中搜索字符串

时间:2017-11-02 23:27:54

标签: c# arrays json mongodb string-search

我的MongoDB中有以下记录。我想在“星期一”字段中搜索字符串。我想搜索“learningexperience”字段,以返回与该字段中某个单词匹配的任何文档。

不确定如何使用MongoDB / C#。任何帮助都会很棒。

{
    "_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."
        }
]
}

1 个答案:

答案 0 :(得分:0)

如果您想使用%keyword%搜索它。你可以试试这个

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

对于C#,您可以尝试使用此过滤器(mongodb c#driver 2.0或更高版本)

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