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

Kriterier/aggregering:søg efter alle komplette dokumenter, hvis matrixfelt har element med senest givne status

Du kan bruge $expr ( 3.6 mongo version operator ) for at bruge aggregeringsfunktioner i almindelig forespørgsel.

Sammenlign query operators vs aggregation comparison operators .

Brug $indexOfArray operatør for at finde $max opdateret tidsstempelelement i statusarray efterfulgt af projicering af statusværdien og sammenligning for at kontrollere værdien mod inputstatussen.

Så shell-forespørgsel er

{
  "key":"56h68ab4c876dbe1cd0b1ee",
  "$expr":{
    "$eq":[
      {"$let":{
        "vars":{
          "status":{"$arrayElemAt":["$status",{"$indexOfArray":["$status.updatedTimeStamp",{"$max":"$status.updatedTimeStamp"}]}]}
        },
        "in":"$$status.orderStatus"
      }},
      "CONFIRM_PAYMENT"]
  }
}

Forårskode:

Query query = new BasicQuery("{key:'56h68ab4c876dbe1cd0b1ee','$expr':{'$eq':[{$let:{vars:{status:{'$arrayElemAt':['$status',{'$indexOfArray':['$status.updatedTimeStamp',{'$max':'$status.updatedTimeStamp'}]}]}}, in:'$$status.orderStatus'}},'CONFIRM_PAYMENT']}}");
List<Document> results = mongoTemplate.find(query, Document.class);

Mongo 3.4 klageversion:

Shell-forespørgsel:

db.order.aggregate([ 
  { "$match" : { "key" : "56h68ab4c876dbe1cd0b1ee"}} , 
  { "$addFields" : {
    "cmpret" : { 
      "$eq" : [ 
        { "$let" : {
          "vars" : { "status" : { "$arrayElemAt" : [ "$status" , { "$indexOfArray" : [ "$status.updatedTimeStamp" , { "$max" : "$status.updatedTimeStamp"}]}]}} ,
          "in" : "$$status.orderStatus"
        }} , 
        "CONFIRM_PAYMENT"
      ]
    }
  }} , 
  { "$match" : { "cmpret" : true}} , 
  { "$project" : { "cmpret" : 0}}
])

Forårskode:

  AggregationOperation match1 = Aggregation.match(Criteria.where("key").is("56h68ab4c876dbe1cd0b1ee"));
        AggregationOperation addFields = new AggregationOperation() {
            @Override
            public Document toDocument(AggregationOperationContext aggregationOperationContext) {
                Document cmpret = Document.parse("{'$eq':[{$let:{vars:{status:{'$arrayElemAt':['$status',{'$indexOfArray':['$status.updatedTimeStamp',{'$max':'$status.updatedTimeStamp'}]}]}}, in:'$$status.orderStatus'}},'CONFIRM_PAYMENT']}}");
                return new Document("$addFields", new Document("cmpret", cmpret));
            }
        };

  AggregationOperation match2 =  Aggregation.match(Criteria.where("cmpret").is(true));

   AggregationOperation dropFields = new AggregationOperation() {
            @Override
            public Document toDocument(AggregationOperationContext aggregationOperationContext) {
                return new Document("$project", new Document("cmpret", 0));
            }
        };
   Aggregation aggregation = Aggregation.newAggregation(
                match1,
                addFields,
                match2,
                dropFields
    );

   AggregationResults<Document> results = mongoTemplate.aggregate(aggregation, "order", Document.class);



  1. Sådan bruges $ positional operator i MongoDB C# driver version 2

  2. Generer PDF med JasperReports Library og MongoDB

  3. Mongodb aggregeret forespørgsel på specifikke poster i stedet for samling

  4. Mongoengine creation_time attribut i dokument