使用MongoDB中的扩展过滤器(通过字段中的字段)

发布于 2025-02-10 15:50:41 字数 1318 浏览 1 评论 0原文

有一个列表list< bsondocument>带有类似的值:

{{ "_id" : 123, "IDD" : 123, "Sc" : { "Field1" : null, "Field2" : "some text|some text|"} }}
{{ "_id" : 124, "IDD" : 124, "Sc" : { "Field1" : { "fl" : "something" }, "Field2" : ""} }}
{{ "_id" : 125, "IDD" : 125, "Sc" : { "Field1" : { }, "Field2" : null} }}

它是用这样的简单过滤器拍摄的:

var builder = Builders<BsonDocument>.Filter;
var filter = builder.Gt("IDD", 122);
var collection = database.GetCollection<BsonDocument>("coll").Find(filter).ToList();

我有2个非常相似的问题:

  1. 如何在没有创建的情况下使用此条件获得该元素类(即仅使用bsondocument):

    select * coll where sc.field2!=“”&amp;&amp; sc.field2!= null

预期结果:

{{ "_id" : 123, "IDD" : 123, "Sc" : { "Field1" : null, "Field2" : "some text|some text|"} }}
  1. 如何在没有创建的情况下获得此条件的元素(即仅使用bsondocument):

    select * coll where sc.field1!= {}&amp;&amp; sc.field2!= null

预期结果:

{{ "_id" : 124, "IDD" : 124, "Sc" : { "Field1" : { "fl" : "something" }, "Field2" : ""} }}

。 我尝试了这样的施工,但它行不通:

filter &= !builder.ElemMatch<BsonValue>("Sc", new BsonDocument { { "Field2", BsonNull.Value } })

There's a list List<BsonDocument> with values like that:

{{ "_id" : 123, "IDD" : 123, "Sc" : { "Field1" : null, "Field2" : "some text|some text|"} }}
{{ "_id" : 124, "IDD" : 124, "Sc" : { "Field1" : { "fl" : "something" }, "Field2" : ""} }}
{{ "_id" : 125, "IDD" : 125, "Sc" : { "Field1" : { }, "Field2" : null} }}

It's was taken with simple filter like that:

var builder = Builders<BsonDocument>.Filter;
var filter = builder.Gt("IDD", 122);
var collection = database.GetCollection<BsonDocument>("coll").Find(filter).ToList();

I have 2 quite similar questions:

  1. How to get the element with this condition without the creation a class (i.e. just using BsonDocument):

    SELECT * FROM coll WHERE Sc.Field2 != "" && Sc.Field2 != null

Expected result:

{{ "_id" : 123, "IDD" : 123, "Sc" : { "Field1" : null, "Field2" : "some text|some text|"} }}
  1. How to get the element with this condition without the creation a class (i.e. just using BsonDocument):

    SELECT * FROM coll WHERE Sc.Field1 != {} && Sc.Field2 != null

Expected result:

{{ "_id" : 124, "IDD" : 124, "Sc" : { "Field1" : { "fl" : "something" }, "Field2" : ""} }}

.

.

.
I tried construction like that and it doesn't work:

filter &= !builder.ElemMatch<BsonValue>("Sc", new BsonDocument { { "Field2", BsonNull.Value } })

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

银河中√捞星星 2025-02-17 15:50:41

找到了解决方案。不知何故,我错过了该语法:

filter &= !builder.Eq($"Sc.Field1", ""); 
filter &= !builder.Eq($"Sc.Field1", BsonNull.Value); 
filter &= !builder.Eq($"Sc.Field1", new BsonDocument()); 

它在两种情况下都有效。我希望这对遇到同一情况的每个人都会有用。

Found a solution. Somehow I've missed that syntax:

filter &= !builder.Eq(
quot;Sc.Field1", ""); 
filter &= !builder.Eq(
quot;Sc.Field1", BsonNull.Value); 
filter &= !builder.Eq(
quot;Sc.Field1", new BsonDocument()); 

And it works for both cases. I hope it will be useful to everyone who encounters the same case.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文