C# 如何将具有LUUID值的查询反序列化到BsonDocument

C# 如何将具有LUUID值的查询反序列化到BsonDocument,c#,mongodb,bsondocument,C#,Mongodb,Bsondocument,我正在尝试使用LUUID(本例中为NUUID)将筛选器反序列化为BsonDocument: var tmpQry = "{'ValueId': NUUID('ca7ac84f-18bf-42f0-b028-333ed144c549')"; var tmpBson = MongoDB.Bson.Serialization.BsonSerializer.Deserialize<BsonDocument>(tmpQry); 我明白,LUUID对JSON无效,但是否有可能从我的字符串中获取

我正在尝试使用LUUID(本例中为NUUID)将筛选器反序列化为BsonDocument:

var tmpQry = "{'ValueId': NUUID('ca7ac84f-18bf-42f0-b028-333ed144c549')";
var tmpBson = MongoDB.Bson.Serialization.BsonSerializer.Deserialize<BsonDocument>(tmpQry);
我明白,LUUID对JSON无效,但是否有可能从我的字符串中获取BsonDocument

在我的特殊情况下,我无法实现MongoDB嵌套的$elemMatch查询,如中所示。 但我的查询包含标识符:

db.getCollection('my_db').aggregate([
    {
        $match: {
            'Event.key_attributes': {
                $all: [
                    { '$elemMatch': { 'Type.Code': 'code1', 'ValueId': LUUID('00000000-0000-0000-0000-000000000001') } },
                    { '$elemMatch': { 'Type.Code': 'code2', 'ValueId': LUUID("00000000-0000-0000-0000-000000000002") } },
                    { '$elemMatch': { 'Type.Code': 'code3', 'ValueId': LUUID("00000000-0000-0000-0000-000000000003") } }
                ]
            }
        }
    },
    {
        $group: {
            '_id': '$$CURRENT.Event.type._id',
            'code': { '$last': '$$CURRENT.Event.type.Code' },
            'value': { '$sum': '$$CURRENT.Event.value' }
        }
    }
]);
,我甚至无法将其反序列化为B文档。 我的问题有解决办法吗?
非常感谢。

最后,我解决了这个问题。我没有尝试从字符串序列化查询,而是创建了BsonDocument管道:

var filter = new BsonDocument {{
    "$match", new BsonDocument {{
        "Event.key_attributes", new BsonDocument {{
        "$all", new BsonArray().AddRange(limit.KeyAttributes.Select(ka => new BsonDocument(
            "$elemMatch", new BsonDocument().AddRange(new List<BsonElement>{
            new BsonElement("Type.Code", ka.Type.Code),
            new BsonElement("ValueId", ka.ValueId)
            })
        )).ToList())
        }}
    }}
}};


var group = new BsonDocument {{
    "$group", new BsonDocument().AddRange(new List<BsonElement>{
        new BsonElement("_id", "$$CURRENT.Event.type._id"),
        new BsonElement("code", new BsonDocument{{
            "$last", "$$CURRENT.Event.type.Code" }}),
        new BsonElement("value", new BsonDocument{{
            "$sum", "$$CURRENT.Event.value" }})
    })
}};

var pipeline = new BsonDocument[]
{
    filter,
    group
};

var result = collection.Aggregate<MyOutputClass>(pipeline).ToListAsync();
var filter=新的BsonDocument{{
“$match”,新的B文件{{
“事件.关键属性”,新的BsonDocument{{
“$all”,new BsonArray().AddRange(limit.KeyAttributes.Select(ka=>new BsonDocument(
“$elemMatch”,新的BsonDocument().AddRange(新列表{
新的BsonElement(“Type.Code”,ka.Type.Code),
新BsonElement(“ValueId”,ka.ValueId)
})
)).ToList())
}}
}}
}};
var组=新的BsonDocument{{
“$group”,新的BsonDocument().AddRange(新列表{
新的BSOneElement(“\u id”,“$$CURRENT.Event.type.\u id”),
新BsonElement(“代码”),新BsonDocument{{
“$last”、“$$CURRENT.Event.type.Code”}),
新BsonElement(“值”),新BsonDocument{{
“$sum”、“$$CURRENT.Event.value”})
})
}};
var管道=新的BsonDocument[]
{
过滤器,
组
};
var result=collection.Aggregate(pipeline.ToListAsync();

Guid没有问题。

海报解决了他的问题,但对于后来出现的问题:使用
CSUUID('Guid-string-here')
作为CSharp遗留格式。

c#mongodb反序列化器不支持LUUID。您可能需要将数据库中的GUID转换为字符串,或者使用ObjectId。@u272; u309Νu953; u915; u919;∧ψu922;,您能否更详细地解释ObjectId?如果'ValueId'的值是ObjectId类型,则序列化程序可以正确地解析它。例如:var-tmpQry=“{'ValueId':ObjectId('xxxxxxxxxxxx')。基本上在数据库中完全去掉GUID。@cĵ
var filter = new BsonDocument {{
    "$match", new BsonDocument {{
        "Event.key_attributes", new BsonDocument {{
        "$all", new BsonArray().AddRange(limit.KeyAttributes.Select(ka => new BsonDocument(
            "$elemMatch", new BsonDocument().AddRange(new List<BsonElement>{
            new BsonElement("Type.Code", ka.Type.Code),
            new BsonElement("ValueId", ka.ValueId)
            })
        )).ToList())
        }}
    }}
}};


var group = new BsonDocument {{
    "$group", new BsonDocument().AddRange(new List<BsonElement>{
        new BsonElement("_id", "$$CURRENT.Event.type._id"),
        new BsonElement("code", new BsonDocument{{
            "$last", "$$CURRENT.Event.type.Code" }}),
        new BsonElement("value", new BsonDocument{{
            "$sum", "$$CURRENT.Event.value" }})
    })
}};

var pipeline = new BsonDocument[]
{
    filter,
    group
};

var result = collection.Aggregate<MyOutputClass>(pipeline).ToListAsync();