Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I have solved the immediate issue by contruction a set of class that allowed for the generation of the following query:</p> <pre><code>{ 'Relationships': { $all: [ {$elemMatch: {'RelationshipType':'Student', 'Attributes.Institution': 'Test1'}}, {$elemMatch: {'RelationshipType':'Staff', 'Attributes.Institution': 'Test2'}} ] } } </code></pre> <p>Here are the class definitions:</p> <pre><code>class MongoQueryAll { public string Name { get; set; } public List&lt;MongoQueryElement&gt; QueryElements { get; set; } public MongoQueryAll(string name) { Name = name; QueryElements = new List&lt;MongoQueryElement&gt;(); } 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&lt;MongoQueryPredicate&gt; QueryPredicates { get; set; } public MongoQueryElement() { QueryPredicates = new List&lt;MongoQueryPredicate&gt;(); } 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); } } </code></pre> <p>Helper Search Class:</p> <pre><code>public class IdentityAttributeSearch { public string Name { get; set; } public object Datum { get; set; } public string RelationshipType { get; set; } } </code></pre> <p>Example Usage:</p> <pre><code> public List&lt;IIdentity&gt; FindIdentities(List&lt;IdentityAttributeSearch&gt; searchAttributes) { var server = MongoServer.Create("mongodb://localhost/"); var db = server.GetDatabase("IdentityManager"); var collection = db.GetCollection&lt;MongoIdentity&gt;("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&lt;BsonDocument&gt;(qAll.ToString()); var identities = collection.Find(new QueryComplete(doc)).ToList(); return identities; } </code></pre> <p>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.</p> <p>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.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload