2013-03-14 15 views
5

ho impilati per costruire questa query MongoDB in C# conducente:MongoDB: Costruire query in C# conducente

{ 
    Location: { "$within": { "$center": [ [1, 1], 5 ] } }, 
    Properties: { 
     $all: [ 
      { $elemMatch: { Type: 1, Value: "a" } }, 
      { $elemMatch: { Type: 2, Value: "b" } } 
     ] 
    } 
} 

Qualcosa prossimo:

var geoQuery = Query.WithinCircle("Location", x, y, radius); 
var propertiesQuery = **?**; 
var query = Query.And(geoQuery, propertiesQuery); 

Aggiunta:

La query sopra tratto dalla mia altra domanda: MongoDB: Match multiple array elements Siete invitati a prendere t nella sua soluzione.

risposta

5

Ecco come se si vuole ottenere quella domanda precisa:

// create the $elemMatch with Type and Value 
// as we're just trying to make an expression here, 
// we'll use $elemMatch as the property name 
var qType1 = Query.EQ("$elemMatch", 
    BsonValue.Create(Query.And(Query.EQ("Type", 1), 
        Query.EQ("Value", "a")))); 
// again 
var qType2 = Query.EQ("$elemMatch", 
    BsonValue.Create(Query.And(Query.EQ("Type", 2), 
        Query.EQ("Value", "b")))); 
// then, put it all together, with $all connection the two queries 
// for the Properties field 
var query = Query.All("Properties", 
    new List<BsonValue> { 
     BsonValue.Create(qType1), 
     BsonValue.Create(qType2) 
    }); 

La parte subdolo è che mentre molti dei parametri dei vari soddisfatti hods aspettano BsonValue s piuttosto che domande, è possibile creare un'istanza BsonValue da un'istanza Query facendo qualcosa di simile:

// very cool/handy that this works 
var bv = BsonValue.Create(Query.EQ("Type", 1)); 

La query attuale inviato corrisponde alla richiesta originale esattamente:

query = { 
    "Properties": { 
    "$all": [ 
     { "$elemMatch": { "Type": 1, "Value": "a" }}, 
     { "$elemMatch": { "Type": 2, "Value": "b" }} 
    ] 
    } 
} 

(I' Non ho mai visto lo stile di utilizzo di $all, ma a quanto pare, suona come it's just not documented.)

3

Mentre posso confermare che la query che si pubblicato opere sulla mia macchina, il documentation of $all sembra indicare che non dovrebbe accettare le espressioni o query, ma solo i valori:

Syntax: { field: { $all: [ <value> , <value1> ... ] } 

(Gli usi di documentazione <expression> se le query sono consentite, compare to $and). Di conseguenza, il driver C# accetta solo un array di BsonValue anziché IMongoQuery.

Tuttavia, la seguente query deve essere equivalente:

{ 
    $and: [ 
     { "Location": { "$within": { "$center": [ [1, 1], 5 ] } } }, 
     { "Properties" : { $elemMatch: { "Type": 1, "Value": "a" } } }, 
     { "Properties" : { $elemMatch: { "Type": 2, "Value": "b" } } } 
    ] 
} 

che si traduce per il conducente C# come

var query = 
Query.And(Query.WithinCircle("Location", centerX, centerY, radius), 
Query.ElemMatch("Properties", Query.And(Query.EQ("Type", 1), Query.EQ("Value", "a"))), 
Query.ElemMatch("Properties", Query.And(Query.EQ("Type", 2), Query.EQ("Value", "b")))); 
+0

La query con $ e non è equivalente al 100% a quella originale. Si prega di vedere il link in fondo alla mia domanda per i dettagli. Comunque grazie per la tua risposta. – Kamarey

Problemi correlati