简体   繁体   中英

WHERE clause Azure CosmosDB Mongo subdocument

Using Azure CosmosDB Mongo.

I have text fields in documents and subdocuments. Which I want be able to search.

Using Contains works fine on the parent document properties. But doesn't seem to look at children at all. And doesn't even return any error.

Document:

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

Search:

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();

If I use the above code with search "found". It will return the document.

If I use it with search "foundtoo". It will not return anything.

PS: I have tried using Text and it is not supported and comes back as an error.

Cosmos Mongo Db may have not implemented whole commands as native MongoDB. I tried the code you mentioned also get the same result as you mentioned.

Using Contains works fine on the parent document properties. But doesn't seem to look at children at all. And doesn't even return any error.

Please have a try to use the follow code to do that. I test it on my side, it works correctly.

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

The following is my detail steps:

1.Create a .net project and reference MongoDB.Driver more detail please refer to packages.config.

2.Add the Mydata and MyObject class

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

 public class Mydata
 {
     public string Comment;
 }

3.Test with following code.

  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();

在此处输入图片说明

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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