MongoDB
 sql >> Database >  >> NoSQL >> MongoDB

$fitler array nidificato utilizzando $lte $gte

Puoi utilizzare $filter aggregazione per filtrare le searches matrice

db.collection.aggregate([
  { "$match": { "username": "admin" }},
  { "$project": {
    "total": { "$size": "$searches" },
    "lasthour": {
      "$size": {
        "$filter": {
          "input": "$searches",
          "as": "search",
          "cond": {
            "$and": [
              { "$gte": ["$$search", onehourago] },
              { "$lte": ["$$search", now] }
            ]
          }
        }
      }
    },
    "today": {
      "$size": {
        "$filter": {
          "input": "$searches",
          "as": "search",
          "cond": {
            "$and": [
              { "$gte": ["$$search", yesterday] },
              { "$lte": ["$$search", now] }
            ]
          }
        }
      }
    }
  }}
])