2016-07-13 10 views
0

Ich möchte so in einer aggregation filtern. Hier ist ein Beispiel für eine Mungo-Abfrage. Das gleiche möchte ich in einer Aggegration machen. Was ich tun möchte, ist alle Messungen erhalten zwischen 2016-06-01T22: 52: 46Z 2016-06-02T22 und: 52: 46Z und die anderen Werte

Measurement 
    .find({}) 
    .or([{ 
    'date': { 
     '$gte': ISODate('2016-06-01T22:52:46Z'), 
     '$lte': ISODate('2016-06-02T22:52:46Z') 
    } 
    }, { 
    'date': { 
     '$gte': ISODate('2016-06-10T22:52:46Z'), 
     '$lte': ISODate('2016-06-11T22:52:46Z') 
    } 
    }, { 
    'date': { 
     '$gte': ISODate('2016-06-14T22:52:46Z'), 
     '$lte': ISODate('2016-06-15T22:52:46Z') 
    } 
    }, { 
    'date': { 
     '$gte': ISODate('2016-06-26T22:52:46Z'), 
     '$lte': ISODate('2016-06-27T22:52:46Z') 
    } 
    }]) 
    .exec(); 

Dies ist, was habe ich versucht, aber es hat nicht Mach die gleiche Arbeit. Und die IDE sagt Dubletten Schlüssel "Datum"

db.measurements.aggregate([{ 
    $match: { 
    $or: [{ 
     date: { 
     '$gte': ISODate('2016-06-01T22:52:46Z'), 
     '$lte': ISODate('2016-06-02T22:52:46Z') 
     }, 
     date: { 
     '$gte': ISODate('2016-06-10T22:52:46Z'), 
     '$lte': ISODate('2016-06-11T22:52:46Z') 
     }, 
     date: { 
     '$gte': ISODate('2016-06-14T22:52:46Z'), 
     '$lte': ISODate('2016-06-15T22:52:46Z') 
     }, 
     date: { 
     '$gte': ISODate('2016-06-26T22:52:46Z'), 
     '$lte': ISODate('2016-06-27T22:52:46Z') 
     } 
    }] 
    } 
}]); 

Antwort

1

Haben Sie ein Beispiel für Daten? Versuchen Sie diesen Code:

db.measurements.aggregate([{ 
    $match: { 
    $or: [ 
    { date: { 
     '$gte': ISODate('2016-06-01T22:52:46Z'), 
     '$lte': ISODate('2016-06-02T22:52:46Z') 
     }}, 
     { date: { 
     '$gte': ISODate('2016-06-10T22:52:46Z'), 
     '$lte': ISODate('2016-06-11T22:52:46Z') 
     }}, 
     { date: { 
     '$gte': ISODate('2016-06-14T22:52:46Z'), 
     '$lte': ISODate('2016-06-15T22:52:46Z') 
     }}, 
     { date: { 
     '$gte': ISODate('2016-06-26T22:52:46Z'), 
     '$lte': ISODate('2016-06-27T22:52:46Z') 
     }} 
    ] 
    } 
}]); 
+0

Vielen Dank, es macht den Job! – Tim

+0

Meine Empfehlung: Nehmen Sie die MongoDb-Quellen. Es ist kostenlos und sehr hilfreich. https://university.mongodb.com/ –