Cómo encontrar datos condicionalmente con mangosta al verificar el último elemento del campo de state de la matriz de history .
{ "_id" : ObjectId("5ff4360b3a1119002a6b8e04"), "name" : "sample" "history" : [ { "_id" : ObjectId("56b0402a92b2a18116bf51de"), "state" : "pending" }, { "_id" : ObjectId("56b0402a92b2a18116bf51fn"), "state" : "completed" } ], }Intento seguir,
collection.find({$expr: {$eq: [{"$arrayElemAt": ["stateHistory", -1]}, "completed"]}}) ¿Hay alguna forma de hacer esto usando la función de find sin usar la agregación?
puedes usar $where
collection.find({ $where: function(){ return this.history[this.history.length-1].state === "completed" } })