【问题标题】:Get nested object that has a value in mongodb c#获取在mongodb c#中具有值的嵌套对象
【发布时间】:2018-03-26 19:56:42
【问题描述】:

我正在尝试检索密钥存在的嵌套对象,并尝试了许多不同的过滤器/构建器,但仍然无法使其正常工作。

这是我最近的:

var filter = "{ Properties : { " + propName + ": { $exists: true } } }";
var results = coll.Find(filter).ToList();

propName 是一个字符串变量。

db 内容如下所示:

{
    "_id" : ObjectId("5aaa1e72884cd35eef175c6a"),
    "Hash" : 1164917297,
    "Name" : "N3N_ZN1",
    "Description" : null,
    "Label" : "N3N_ZN1",
    "RelationSource" : {
        "From" : [],
        "To" : [],
        "Pair" : []
    },
    "Relations" : {},
    "Properties" : {
        "SOPName" : [ 
            "SOP for Intrusion"
        ]
    },
    "ObjectTypeName" : "ultrasonic",
    "PlayerTypeName" : null,
    "PlayerProperties" : null
}

我的目标是检索所有存在 SOPName 的文档。 Properties 对象是动态的,因此 SOPName 键可能并不存在于所有文档中。

哦,我上面的查询没有在我的数据库中检索任何文档(计数 0)。有什么想法吗?

【问题讨论】:

    标签: c# mongodb mongodb-query


    【解决方案1】:

    好的,解决了这个问题:

        var coll = DatabaseCommon.Instance.GetDatabase("ps_" + siteId).GetCollection<BsonDocument>("Object");
        var filter = "{ 'Properties." + propName + "' : { '$exists': true } }";
        var results = coll.Find(filter).ToList();
    

    过滤器不能作为典型的嵌套对象工作......这个失败:

    var filter = "{ Properties : { " + propName + " : { '$exists': true } } }";
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-14
      • 1970-01-01
      • 2020-04-23
      • 1970-01-01
      • 2018-10-24
      • 2018-06-12
      相关资源
      最近更新 更多