我怎样才能“并且”? C# 和 MongoDB 的多个 $elemMatch 子句?

发布于 2024-11-14 10:16:35 字数 1436 浏览 3 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

趁微风不噪 2024-11-21 10:16:35

无法使用 C# 驱动程序构建上述查询(至少在 1.0 版本中)。

但是您可以构建另一个更清晰的查询,它将返回相同的结果:

{ "Relationships" : 
          { "$elemMatch" : 
              { "RelationshipType" : "Test", 
                "Attributes.Institution" : { "$all" : ["Location1", "Location2"] } 
              } 
          } 
}

以及来自 c# 的相同查询:

Query.ElemMatch("Relationships", 
    Query.And(
        Query.EQ("RelationshipType", "Test"),
            Query.All("Attributes.Institution", "Location1", "Location2")));

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:

{ "Relationships" : 
          { "$elemMatch" : 
              { "RelationshipType" : "Test", 
                "Attributes.Institution" : { "$all" : ["Location1", "Location2"] } 
              } 
          } 
}

And the same query from c#:

Query.ElemMatch("Relationships", 
    Query.And(
        Query.EQ("RelationshipType", "Test"),
            Query.All("Attributes.Institution", "Location1", "Location2")));
忆沫 2024-11-21 10:16:35

一个简单的解决方案是将多个 IMongoQuery 串在一起,然后用一个 Query 将它们连接起来。最后:

List<IMongoQuery> build = new List<IMongoQuery>();
build.Add(Query.ElemMatch("Relationships", Query.EQ("RelationshipType", "Person")));

var searchQuery =  String.Format("/.*{0}.*/", "sta");
build.Add(Query.ElemMatch("Relationships", Query.Or(Query.EQ("Attributes.FirstName", new BsonRegularExpression(searchQuery)), Query.EQ("Attributes.LastName", new BsonRegularExpression(searchQuery)))));

var _main = Query.And(build.ToArray());

var DB = MongoDatabase.Create("UrlToMongoDB");
DB.GetCollection<ObjectToQuery>("nameOfCollectionInMongoDB").FindAs<ObjectToQuery>(_main).ToList();

`

A simple solution is to string together multiple IMongoQuery(s) and then join them with a Query.And at the end:

List<IMongoQuery> build = new List<IMongoQuery>();
build.Add(Query.ElemMatch("Relationships", Query.EQ("RelationshipType", "Person")));

var searchQuery =  String.Format("/.*{0}.*/", "sta");
build.Add(Query.ElemMatch("Relationships", Query.Or(Query.EQ("Attributes.FirstName", new BsonRegularExpression(searchQuery)), Query.EQ("Attributes.LastName", new BsonRegularExpression(searchQuery)))));

var _main = Query.And(build.ToArray());

var DB = MongoDatabase.Create("UrlToMongoDB");
DB.GetCollection<ObjectToQuery>("nameOfCollectionInMongoDB").FindAs<ObjectToQuery>(_main).ToList();

`

落日海湾 2024-11-21 10:16:35

我已经通过构造一组允许生成以下查询的类解决了眼前的问题:

{  'Relationships': 
    { 
        $all: [
         {$elemMatch: {'RelationshipType':'Student', 'Attributes.Institution': 'Test1'}},            
         {$elemMatch: {'RelationshipType':'Staff',   'Attributes.Institution': 'Test2'}}     
        ]
    }
}

以下是类定义:

class MongoQueryAll
    {
        public string Name { get; set; }
        public List<MongoQueryElement> QueryElements { get; set; }

        public MongoQueryAll(string name)
       {
           Name = name;
           QueryElements = new List<MongoQueryElement>();
       }

       public override string ToString()
       {
           string qelems = "";
           foreach (var qe in QueryElements)
              qelems = qelems + qe + ",";

           string query = String.Format(@"{{ ""{0}"" : {{ $all : [ {1} ] }} }}", this.Name, qelems); 

           return query;
       }
  }

class MongoQueryElement
{
    public List<MongoQueryPredicate> QueryPredicates { get; set; }

    public MongoQueryElement()
    {
        QueryPredicates = new List<MongoQueryPredicate>();
    }

    public override string ToString()
    {
        string predicates = "";
        foreach (var qp in QueryPredicates)
        {
            predicates = predicates + qp.ToString() + ",";
        }

        return String.Format(@"{{ ""$elemMatch"" : {{ {0} }} }}", predicates);
    }
}

class MongoQueryPredicate
{        
    public string Name { get; set; }
    public object Value { get; set; }

    public MongoQueryPredicate(string name, object value)
    {
        Name = name;
        Value = value;
    }

    public override string ToString()
    {
        if (this.Value is int)
            return String.Format(@" ""{0}"" : {1} ", this.Name, this.Value);

        return String.Format(@" ""{0}"" : ""{1}"" ", this.Name, this.Value);
    }
}

帮助搜索类:

public class IdentityAttributeSearch
{
    public string Name { get; set; }
    public object Datum { get; set; }
    public string RelationshipType { get; set; }
}

示例用法:

 public List<IIdentity> FindIdentities(List<IdentityAttributeSearch> searchAttributes)
 {
        var server = MongoServer.Create("mongodb://localhost/");
        var db = server.GetDatabase("IdentityManager");
        var collection = db.GetCollection<MongoIdentity>("Identities");

        MongoQueryAll qAll = new MongoQueryAll("Relationships");

        foreach (var search in searchAttributes)
        {
            MongoQueryElement qE = new MongoQueryElement();
            qE.QueryPredicates.Add(new MongoQueryPredicate("RelationshipType", search.RelationshipType));
            qE.QueryPredicates.Add(new MongoQueryPredicate("Attributes." + search.Name, search.Datum));
            qAll.QueryElements.Add(qE);
        }

        BsonDocument doc = MongoDB.Bson.Serialization
                .BsonSerializer.Deserialize<BsonDocument>(qAll.ToString());

        var identities = collection.Find(new QueryComplete(doc)).ToList();

        return identities;
    }

我确信有更好的方法,但这目前有效,并且似乎足够灵活以满足我的需求。欢迎所有建议。

这可能是一个单独的问题,但由于某种原因,对于 100,000 个文档集,此搜索可能需要长达 24 秒的时间。我尝试过添加各种索引但无济于事;在这方面的任何指示都会很棒。

I have solved the immediate issue by contruction a set of class that allowed for the generation of the following query:

{  'Relationships': 
    { 
        $all: [
         {$elemMatch: {'RelationshipType':'Student', 'Attributes.Institution': 'Test1'}},            
         {$elemMatch: {'RelationshipType':'Staff',   'Attributes.Institution': 'Test2'}}     
        ]
    }
}

Here are the class definitions:

class MongoQueryAll
    {
        public string Name { get; set; }
        public List<MongoQueryElement> QueryElements { get; set; }

        public MongoQueryAll(string name)
       {
           Name = name;
           QueryElements = new List<MongoQueryElement>();
       }

       public override string ToString()
       {
           string qelems = "";
           foreach (var qe in QueryElements)
              qelems = qelems + qe + ",";

           string query = String.Format(@"{{ ""{0}"" : {{ $all : [ {1} ] }} }}", this.Name, qelems); 

           return query;
       }
  }

class MongoQueryElement
{
    public List<MongoQueryPredicate> QueryPredicates { get; set; }

    public MongoQueryElement()
    {
        QueryPredicates = new List<MongoQueryPredicate>();
    }

    public override string ToString()
    {
        string predicates = "";
        foreach (var qp in QueryPredicates)
        {
            predicates = predicates + qp.ToString() + ",";
        }

        return String.Format(@"{{ ""$elemMatch"" : {{ {0} }} }}", predicates);
    }
}

class MongoQueryPredicate
{        
    public string Name { get; set; }
    public object Value { get; set; }

    public MongoQueryPredicate(string name, object value)
    {
        Name = name;
        Value = value;
    }

    public override string ToString()
    {
        if (this.Value is int)
            return String.Format(@" ""{0}"" : {1} ", this.Name, this.Value);

        return String.Format(@" ""{0}"" : ""{1}"" ", this.Name, this.Value);
    }
}

Helper Search Class:

public class IdentityAttributeSearch
{
    public string Name { get; set; }
    public object Datum { get; set; }
    public string RelationshipType { get; set; }
}

Example Usage:

 public List<IIdentity> FindIdentities(List<IdentityAttributeSearch> searchAttributes)
 {
        var server = MongoServer.Create("mongodb://localhost/");
        var db = server.GetDatabase("IdentityManager");
        var collection = db.GetCollection<MongoIdentity>("Identities");

        MongoQueryAll qAll = new MongoQueryAll("Relationships");

        foreach (var search in searchAttributes)
        {
            MongoQueryElement qE = new MongoQueryElement();
            qE.QueryPredicates.Add(new MongoQueryPredicate("RelationshipType", search.RelationshipType));
            qE.QueryPredicates.Add(new MongoQueryPredicate("Attributes." + search.Name, search.Datum));
            qAll.QueryElements.Add(qE);
        }

        BsonDocument doc = MongoDB.Bson.Serialization
                .BsonSerializer.Deserialize<BsonDocument>(qAll.ToString());

        var identities = collection.Find(new QueryComplete(doc)).ToList();

        return identities;
    }

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文