2011-12-08 14 views

risposta

16

Non è possibile utilizzare spiegare con la distinta di cui al presente MongoDB jira ticket. Invece è possibile utilizzare runCommand e verificare le statistiche, che è un pò simile a explain()

db.runCommand({ distinct: 'test3',key:'id',query:{key:"value"}}) 

In quanto sopra test3 query è nome della raccolta, chiave è un nome di campo che si desidera applicare distinti e, infine, se si voleva specificare qualsiasi i filtri utilizzano la query.

Controllare i campioni

> db.runCommand({ distinct: 'items',key:'name',query:{offered:true}}) 
{ 
    "values" : [ 
     "test flat", 
     "Another aston martin", 
     "super luxury villa", 
     "Aston martin vanquish y for sale", 
     "Super car", 
     "Test item", 
     "another sports car", 
     "super car" 
    ], 
    "stats" : { 
     "n" : 8, 
     "nscanned" : 10, 
     "nscannedObjects" : 10, 
     "timems" : 45, 
     "cursor" : "BasicCursor" 
    }, 
    "ok" : 1 
} 
> db.runCommand({ distinct: 'items',key:'name',query:{offered:false}}) 
{ 
    "values" : [ 
     "yamaha", 
     "Test item" 
    ], 
    "stats" : { 
     "n" : 2, 
     "nscanned" : 10, 
     "nscannedObjects" : 10, 
     "timems" : 0, 
     "cursor" : "BasicCursor" 
    }, 
    "ok" : 1 
} 
+0

Grazie! Questo funziona alla grande per me. –

+5

Non riesco a vedere le statistiche, torno solo ai valori e ok come risposta. – genericuser

+0

@RyanHoffman: deve essere contrassegnato come risposta accettata, IMHO. Grazie! –