WHERE子句Azure CosmosDB Mongo子文档

时间:2017-10-13 08:49:07

标签: c# mongodb azure azure-cosmosdb

使用Azure CosmosDB Mongo。

我在文档和子文档中有文本字段。我希望能够搜索。

使用Contains可以正常处理父文档属性。但似乎根本不看儿童。并且甚至不会返回任何错误。

文件:

 {
     "TextField1": "this will be found in search",
     "Comments": [{
         "Comment": "amazing post, let's see if this can be foundtoo",
     }, {
         "Comment": "thanks",
     }]
 }

搜索:

var postFilter = Builders<MyObject>.Filter.Where(p => p.TextField1.ToLowerInvariant().Contains(searchText.ToLowerInvariant())) |
                 Builders<MyObject>.Filter.Where(p => p.Comments.Any(pc => pc.Comment.ToLowerInvariant().Contains(searchText.ToLowerInvariant())));

var posts = await Posts.Find(postFilter).ToListAsync();

如果我使用上面的代码搜索“找到”。它将返回文档。

如果我使用搜索“foundtoo”。它不会返回任何东西。

PS:我尝试过使用Text并且不支持它并且作为错误返回。

1 个答案:

答案 0 :(得分:1)

Cosmos Mongo Db可能没有将整个命令实现为本机MongoDB。我尝试了你提到的代码也得到了与你提到的相同的结果。

  

使用Contains可以在父文档属性上正常工作。但似乎根本不看儿童。并且甚至不会返回任何错误。

请尝试使用以下代码执行此操作。我测试它在我身边,它工作正常。

var filterResult = collection.Find(Builders<MyObject>.Filter.ElemMatch("Comments", Builders<Mydata>.Filter.Where(p=>p.Comment.ToLowerInvariant().Contains(searchText.ToLowerInvariant())))).ToList();

以下是我的详细步骤:

1.创建.net项目并参考MongoDB.Driver更多细节请参阅packages.config。

2.添加Mydata和MyObject类

public class MyObject
{
     public string TextField1;
     public Mydata[] Comments;
     [JsonProperty(PropertyName = "id")]
     public string Id;
 }

 public class Mydata
 {
     public string Comment;
 }

3.使用以下代码进行测试。

  string connectionString = "connection string";
  MongoClientSettings settings = MongoClientSettings.FromUrl(new MongoUrl(connectionString));
  settings.SslSettings = new SslSettings { EnabledSslProtocols = SslProtocols.Tls12 };
  var mongoClient = new MongoClient(settings);
  var searchText = "foundtoo";
  var db = mongoClient.GetDatabase("dbname");
  var collection = db.GetCollection<MyObject>("collectionName");
  var mydata1 = new Mydata {Comment = "Thank you"};
  var mydata2 = new Mydata {Comment = "amazing post, let's see if this can be foundtoo"};
  var list = new List<Mydata> {mydata1, mydata2};
  Mydata[] mydatas = {mydata1,mydata2};
  collection.InsertOneAsync(new MyObject
            {
                Id = Guid.NewGuid().ToString(),
                TextField1 = "this will be found in search",
                Comments = mydatas

            }).Wait();
   var filterResult = collection.Find(Builders<MyObject>.Filter.ElemMatch("Comments", Builders<Mydata>.Filter.Where(p=>p.Comment.ToLowerInvariant().Contains(searchText.ToLowerInvariant())))).ToList();

enter image description here

<强> packages.config

<?xml version="1.0" encoding="utf-8"?>
<packages>
  <package id="MongoDB.Bson" version="2.4.4" targetFramework="net461" />
  <package id="MongoDB.Driver" version="2.4.4" targetFramework="net461" />
  <package id="MongoDB.Driver.Core" version="2.4.4" targetFramework="net461" />
  <package id="Newtonsoft.Json" version="9.0.1" targetFramework="net461" />
  <package id="System.Runtime.InteropServices.RuntimeInformation" version="4.3.0" targetFramework="net461" />
</packages>