使用MongoDB中的扩展过滤器(通过字段中的字段)
有一个列表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个非常相似的问题:
如何在没有创建的情况下使用此条件获得该元素类(即仅使用bsondocument):
select * coll where sc.field2!=“”&amp;&amp; sc.field2!= null
预期结果:
{{ "_id" : 123, "IDD" : 123, "Sc" : { "Field1" : null, "Field2" : "some text|some text|"} }}
如何在没有创建的情况下获得此条件的元素(即仅使用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:
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|"} }}
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
找到了解决方案。不知何故,我错过了该语法:
它在两种情况下都有效。我希望这对遇到同一情况的每个人都会有用。
Found a solution. Somehow I've missed that syntax:
And it works for both cases. I hope it will be useful to everyone who encounters the same case.