MongoDB
 sql >> Base de données >  >> NoSQL >> MongoDB

$fitler tableau imbriqué utilisant $lte $gte

Vous pouvez utiliser $filter agrégation pour filtrer les searches tableau

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] }
            ]
          }
        }
      }
    }
  }}
])