Active Cards Last Used Report

This generates an item for each card that Expires later then today only returning the person common name, display number and its last used item. To truly only have active cards would need to also filter on active date.

Example in C#

var currentInstance  = await client.GetCurrentInstanceAsync();

var match1 = new BsonDocument("$match", 
    new BsonDocument("$or", 
    new BsonArray
            {
                new BsonDocument("_t", "Person"),
                new BsonDocument("_t", "Attendee")
            }));
	
var unwind1 = new BsonDocument("$unwind", 
    new BsonDocument
        {
            { "path", "$CardAssignments" }, 
            { "preserveNullAndEmptyArrays", true }
        });
var match2 = new BsonDocument("$match", 
    		new BsonDocument("$or", 
			new BsonArray
			{
				new BsonDocument("CardAssignments.ExpiresOn", 
new BsonDocument("$gte", DateTime.UtcNow)),
			       new BsonDocument("Card.ExpiresOn", 
new BsonDocument("$gte", DateTime.UtcNow))
			}));
var project = new BsonDocument("$project", 
    new BsonDocument
        {
            { "CommonName", 1 }, 
            { "_id", 1 }, 
            { "CardAssignments.DisplayCardNumber", 1 }, 
            { "CardAssignments.LastUsed", 1 }, 
            { "Card.DisplayCardNumber", 1 }, 
            { "Card.LastUsed", 1 }
        });

var ActiveCardsLastUsed = await client.AggregateAsync(currentInstance, "KeepObjects", new[] {match1,unwind1, match2, project}, 30);

Example in CURL

curl -X POST \
  'https://keepapi.feenicshosting.com/api/f/INSTANCE_KEY_HERE/aggregate/KeepObjects?queryTimeout=30&jsonStrict=False%20' \
  -H 'Authorization: Bearer TOKEN_GOES_HERE' \
  -H 'Content-Type: application/json' \
  -d '["
        { 
            \"$match\" : 
            { 
                \"$or\" : 
                [
                    { \"_t\" : \"Person\" },
                    { \"_t\" : \"Attendee\" }
                ] 
            } 
        }","
        { 
            \"$unwind\" : 
            { 
                \"path\" : \"$CardAssignments\",
                \"preserveNullAndEmptyArrays\" : true 
            } 
        }","
        { 
            \"$match\" : 
            { 
                \"$or\" : 
                [
                    { 
                        \"CardAssignments.ExpiresOn\" : { \"$gte\" : ISODate(\"2019-06-12T18:45:58.389Z\") } 
                    },
                    {
                        \"Card.ExpiresOn\" : { \"$gte\" : ISODate(\"2019-06-12T18:45:58.389Z\") } 
                    }
                ] 
            } 
        }","
        { 
            \"$project\" : 
            { 
                \"CommonName\" : 1,
                \"_id\" : 1,
                \"CardAssignments.DisplayCardNumber\" : 1, 
                \"CardAssignments.LastUsed\" : 1, 
                \"Card.DisplayCardNumber\" : 1, 
                \"Card.LastUsed\" : 1 
            } 
        }"
    ]'

Example Of Aggregate Used in JSON

[
    { 
        "$match" : 
        { 
            "$or" : 
            [
                { "_t" : "Person" },
                { "_t" : "Attendee" }
            ] 
        } 
    },
    { 
        "$unwind" : 
        { 
            "path" : "$CardAssignments",
            "preserveNullAndEmptyArrays" : true 
        } 
    },
    { 
        "$match" : 
        { 
            "$or" : 
            [
                { 
                    "CardAssignments.ExpiresOn" : { "$gte" : ISODate("2019-06-12T18:45:58.389Z") } 
                },
                {
                    "Card.ExpiresOn" : { "$gte" : ISODate("2019-06-12T18:45:58.389Z") } 
                }
            ] 
        } 
    },
    { 
        "$project" : 
        { 
            "CommonName" : 1,
            "_id" : 1,
            "CardAssignments.DisplayCardNumber" : 1, 
            "CardAssignments.LastUsed" : 1, 
            "Card.DisplayCardNumber" : 1, 
            "Card.LastUsed" : 1 
        } 
    }
]