当前位置: 动力学知识库 > 问答 > 编程问答 >

MongoDB query with multiple search terms(regexes) in c# with 10gens driver?

问题描述:

If we have

Blog{

Name 'Blog1'

Tags ['testing','visual-studio','2010','c#']

}

Blog{

Name 'Blog2'

Tags ['parallel','microsoft','c#']

}

Via the console we can execute and find all blog posts that contains some of the provided tags:

db.BlogPost.find({ 'Tags' : { '$regex' : ['/^Test/', '/^microsoft/', '/^visual/', '/^studio/', '/^c#/'] } });

How can we write the same query in c# 10gens driver ?

Is there any alternative if it can not be written via the 10gens c# driver ?

Query.Match only support one regex. Can we provide him multiple regexes, or we should combine

Query.Or(Query.Match("Test"), Query.Match("Micro"), Query.Match("Visual"))

I've managed to solve it with

I've managed to do it with

{ "$or" : [{ "Tags" : /^programm/i }, { "Tags" : /^microsoft/i }, { "Tags" : /^visual/i }, { "Tags" : /^studio/i }, { "Tags" : /^assert/i }, { "Tags" : /^2010/i }, { "Tags" : /^c#/i }] }

But something tells me that this is an ugly hack that may result in performance issues. What do you think guys ?

The final answer to the problem can be found on:

Official mongodb forum

网友答案:

Yes, the MongoDB c# driver should automatically do the right thing with instances of System.Text.RegularExpressions.Regex.

So you should be able to build the exact same query, except that you would use instances of Regex for ^Test, ^microsoft, ^visual, etc instead of strings.

分享给朋友:
您可能感兴趣的文章:
随机阅读: