繁体   English   中英

如何使用C#驱动程序在无类MongoDB上查找匹配的'StartsWith'文档

[英]How to find matching 'StartsWith' documents on classless MongoDB using C# driver

我有文件:

{"handler":"north"}
{"handler":"south"}
{"handler":"west"}
{"handler":"east"}

我想从给定的字符串输入中找到匹配的处理程序,例如"westtown"并期望该处理程序将是"west"

请帮助,我下面的代码不起作用。

String inputstring = "westtown";
IMongoCollection<BsonDocument> collection = null;
List<BsonDocument> pages = null;

try
{
     collection = db.GetCollection<BsonDocument>("handlers");
     pages = await collection.Find(x => (inputstring.StartsWith(x["handler"].AsString))).ToListAsync<BsonDocument>();
}
...

我使用无类方案,所以我在动态上使用BsonDocument

AFAIK,在MongoDB中,您可以使用$where运算符找到它,如下所示:

{  $where: "'westtown'.startsWith(this.handler)" }

因此,我认为在C#中,您可以使用以下方法做到这一点:

var filter =
    new BsonDocument(
        new Dictionary<string, BsonValue>
            {
                {
                    "$where",
                    new BsonString("'westtown'.startsWith(this.handler)")
                }
            });
var result = col.Find(filter).ToList();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM