我正在尝试将具有LUUID(在此示例中为NUUID)的过滤器反序列化为BsonDocument:
var tmpQry = "{'ValueId': NUUID('ca7ac84f-18bf-42f0-b028-333ed144c549')";
var tmpBson = MongoDB.Bson.Serialization.BsonSerializer.Deserialize<BsonDocument>(tmpQry);
并得到一个错误:
System.FormatException: 'JSON reader was expecting a value but found 'NUUID'.'
我知道,LUUID对JSON无效,但是可以从我的字符串中获取BsonDocument吗?
在我的特殊情况下,我无法实现MongoDB嵌套的$ elemMatch查询,例如在this issue中。
但是我的查询包含标识符:
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' }
}
}
]);
,甚至无法将其反序列化为BsonDocument。
我的问题有什么解决办法吗?
非常感谢。
参考方案
最后,我解决了这个问题。我创建了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();
Guid没有任何问题。
如何在没有for循环的情况下在Javascript中使用Django模板标签 - javascript我想在JavaScript中使用模板变量:我的问题是在javascript代码中使用for循环,for循环之间的所有事情都会重复..但我不想要....下面粘贴了我的代码..有人可以告诉我更好的方法吗这..因为这看起来很丑..这是我的代码: {% block extra_javascript %} <script src="/static/js…
Mongo抛出“元素名称'名称'无效”异常 - c#我正在更新一个简单的字段。var filterDocument = new BsonDocument { { "name", "alice" } }; var newDocument = new BsonDocument { { "name", "Alice" } }; coll…
T-SQL等价的正则表达式'\ b' - c#我正在将利用regex的CLR函数转换为SQL函数。我知道SQL Server并不完全支持正则表达式,但是我只需要一种情况就可以搜索单词。搜索字段值:{"Id":1234, "Title": "The quick brown"}.NET中的正则表达式模式:'\b' + '…
您如何在列表内部调用一个字符串位置? - python我一直在做迷宫游戏。我首先决定制作一个迷你教程。游戏开发才刚刚开始,现在我正在尝试使其向上发展。我正在尝试更改PlayerAre变量,但是它不起作用。我试过放在列表内和列表外。maze = ["o","*","*","*","*","*",…
将字符串分配给numpy.zeros数组[重复] - pythonThis question already has answers here: Weird behaviour initializing a numpy array of string data (4个答案) …