MongoDB
 sql >> Datenbank >  >> NoSQL >> MongoDB

$fitler verschachteltes Array mit $lte $gte

Sie können $filter verwenden Aggregation, um die searches herauszufiltern Array

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