我怎样才能“并且”? C# 和 MongoDB 的多个 $elemMatch 子句?
我正在使用 10Gen 认可的 c# 驱动程序用于 mongoDB for ac# 应用程序,而对于数据浏览,我使用 Mongovue。
以下是两个示例文档模式:
{
"_id": {
"$oid": "4ded270ab29e220de8935c7b"
},
"Relationships": [
{
"RelationshipType": "Person",
"Attributes": {
"FirstName": "Travis",
"LastName": "Stafford"
}
},
{
"RelationshipType": "Student",
"Attributes": {
"GradMonth": "",
"GradYear": "",
"Institution": "Test1",
}
},
{
"RelationshipType": "Staff",
"Attributes": {
"Department": "LIS",
"OfficeNumber": "12",
"Institution": "Test2",
}
}
]
},
{
"_id": {
"$oid": "747ecc1dc1a79abf6f37fe8a"
},
"Relationships": [
{
"RelationshipType": "Person",
"Attributes": {
"FirstName": "John",
"LastName": "Doe"
}
},
{
"RelationshipType": "Staff",
"Attributes": {
"Department": "Dining",
"OfficeNumber": "1",
"Institution": "Test2",
}
}
]
}
我需要一个查询来确保满足两个 $elemMatch 条件,以便我可以匹配第一个文档,但不能匹配第二个文档。以下查询适用于 Mongovue。
{
'Relationships': { $all: [
{$elemMatch: {'RelationshipType':'Student', 'Attributes.Institution': 'Test1'}},
{$elemMatch: {'RelationshipType':'Staff', 'Attributes.Institution': 'Test2'}}
]}
}
如何在我的 C# 代码中执行相同的查询?
I am using the 10Gen sanctioned c# driver for mongoDB for a c# application and for data browsing I am using Mongovue.
Here are two sample document schemas:
{
"_id": {
"$oid": "4ded270ab29e220de8935c7b"
},
"Relationships": [
{
"RelationshipType": "Person",
"Attributes": {
"FirstName": "Travis",
"LastName": "Stafford"
}
},
{
"RelationshipType": "Student",
"Attributes": {
"GradMonth": "",
"GradYear": "",
"Institution": "Test1",
}
},
{
"RelationshipType": "Staff",
"Attributes": {
"Department": "LIS",
"OfficeNumber": "12",
"Institution": "Test2",
}
}
]
},
{
"_id": {
"$oid": "747ecc1dc1a79abf6f37fe8a"
},
"Relationships": [
{
"RelationshipType": "Person",
"Attributes": {
"FirstName": "John",
"LastName": "Doe"
}
},
{
"RelationshipType": "Staff",
"Attributes": {
"Department": "Dining",
"OfficeNumber": "1",
"Institution": "Test2",
}
}
]
}
I need a query that ensures that both $elemMatch criteria are met so that I can match the first document, but not the second. The following query works in Mongovue.
{
'Relationships': { $all: [
{$elemMatch: {'RelationshipType':'Student', 'Attributes.Institution': 'Test1'}},
{$elemMatch: {'RelationshipType':'Staff', 'Attributes.Institution': 'Test2'}}
]}
}
How can I do the same query in my c# code?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
无法使用 C# 驱动程序构建上述查询(至少在 1.0 版本中)。
但是您可以构建另一个更清晰的查询,它将返回相同的结果:
以及来自 c# 的相同查询:
There is no way to build above query using c# driver (at least in version 1.0).
But you can build another, more clear query, that will return same result:
And the same query from c#:
一个简单的解决方案是将多个 IMongoQuery 串在一起,然后用一个 Query 将它们连接起来。最后:
`
A simple solution is to string together multiple IMongoQuery(s) and then join them with a Query.And at the end:
`
我已经通过构造一组允许生成以下查询的类解决了眼前的问题:
以下是类定义:
帮助搜索类:
示例用法:
我确信有更好的方法,但这目前有效,并且似乎足够灵活以满足我的需求。欢迎所有建议。
这可能是一个单独的问题,但由于某种原因,对于 100,000 个文档集,此搜索可能需要长达 24 秒的时间。我尝试过添加各种索引但无济于事;在这方面的任何指示都会很棒。
I have solved the immediate issue by contruction a set of class that allowed for the generation of the following query:
Here are the class definitions:
Helper Search Class:
Example Usage:
I am sure there is a much better way but this worked for now and appears to be flexible enough for my needs. All suggestions are welcome.
This is probably a separate question but for some reason this search can take up to 24 seconds on a document set of 100,000. I have tried adding various indexes but to no avail; any pointers in this regards would be fabulous.