2013-04-16 21 views
12

Sto provando una semplice query in Mongo che in MySQL sarebbe simile a questa.Campo Mongo A maggiore del campo B

select * from emails where bounceCount > sentCount; 

Finora ho.

db.email.find({ bounceCount : { $gt : sentCount } }); 

Ma ottengo questo errore

JS Error: ReferenceError: sentCount is not defined (shell):0 

Come faccio a riferimento il sentCount in quel guscio?

risposta

12

db.so.find("this.bounceCount > this.sentCount") è quello che stai cercando.

Equivalente: db.so.find({"$where":"this.bounceCount > this.sentCount"})

Documentazione: http://docs.mongodb.org/manual/reference/operator/where/

Shell uscita:

> db.so.insert({bounceCount:1, sentCount:2}) 
> db.so.insert({bounceCount:5, sentCount:3}) 
> db.so.insert({bounceCount:5, sentCount:4}) 
> db.so.insert({bounceCount:5, sentCount:7}) 
> db.so.insert({bounceCount:9, sentCount:7}) 

> db.so.find() 
{ "_id" : ObjectId("516d7f30675a2a8d659d7594"), "bounceCount" : 1, "sentCount" : 2 } 
{ "_id" : ObjectId("516d7f37675a2a8d659d7595"), "bounceCount" : 5, "sentCount" : 3 } 
{ "_id" : ObjectId("516d7f3b675a2a8d659d7596"), "bounceCount" : 5, "sentCount" : 4 } 
{ "_id" : ObjectId("516d7f3d675a2a8d659d7597"), "bounceCount" : 5, "sentCount" : 7 } 
{ "_id" : ObjectId("516d7f40675a2a8d659d7598"), "bounceCount" : 9, "sentCount" : 7 } 

> db.so.find({"bounceCount":5}) 
{ "_id" : ObjectId("516d7f37675a2a8d659d7595"), "bounceCount" : 5, "sentCount" : 3 } 
{ "_id" : ObjectId("516d7f3b675a2a8d659d7596"), "bounceCount" : 5, "sentCount" : 4 } 
{ "_id" : ObjectId("516d7f3d675a2a8d659d7597"), "bounceCount" : 5, "sentCount" : 7 } 

> db.so.find("this.bounceCount > this.sentCount") 
{ "_id" : ObjectId("516d7f37675a2a8d659d7595"), "bounceCount" : 5, "sentCount" : 3 } 
{ "_id" : ObjectId("516d7f3b675a2a8d659d7596"), "bounceCount" : 5, "sentCount" : 4 } 
{ "_id" : ObjectId("516d7f40675a2a8d659d7598"), "bounceCount" : 9, "sentCount" : 7 } 
1

È possibile utilizzare l'operatore $ in cui per fare questo, che consente di utilizzare codice Javascript nella query.

Per esempio, si dovrebbe fare:

db.email.find({ $where: "this.bounceCount > this.sentCount" }); 

consultare la pagina di documentazione MongoDB per maggiori dettagli sul $ in cui l'operatore: http://docs.mongodb.org/manual/reference/operator/where/#op._S_where

16

Tutti sembrano parlare $where senza sapere che è:

  • lento
  • insicuro (EVALED)
  • JavaScript, non MongoDB interni
  • E, su versioni precedenti alla 2.4, a thread singolo e globale bloccato

Un altro metodo che sarebbe molto meglio per circa il 99% dei casi è quello di utilizzare il framework di aggregazione:

db.col.aggregate([ 
    {$project: {ab: {$cmp: ['$bounceCount','$sentCount']}}}, 
    {$match: {ab:{$gt:0}}} 
]) 
+0

non vedo la parte "insicuro" della risposta che ho dato. –

+0

@JoeFrambach Il parametro '$ where' prende una stringa, proprio come scrivere SQL senza libreria di escape. – Sammaye

+3

Ma è lo sviluppatore che scrive la query. In nessun momento l'input dell'utente è mai stato valutato. –

Problemi correlati