Get People Slim

Sometimes we may find we would like to get all of the people in the instance but we do not need all the items attached to a PersonInfo object. When this situation occurs we can use an Aggregate call to access the database and return exactly what we need.

In the example we are using below we have decided that we only need the person’s name, the object (person) id or key, and the person’s Metadata.

Example in C#

var match = new BsonDocument { { "$match", new BsonDocument { { "_t", "Person" } } } };
var project = new BsonDocument { { "$project", new BsonDocument { { "_id", "$_id" }, { "CommonName", "$CommonName" }, { "Metadata", "$Metadata" } } } };
var people = await client.AggregateAsync(currentInstance, "KeepObjects", new[] { match, project }, 30);

Now that we have the people with only the elements that we specifically want we need to deserialize the returned BSON Document. Which can be done for the metadata portion with the following function:

Example in C#

private MetadataItem[] ToMetaData(BsonValue document)
	if ((document?.IsBsonNull ?? true) || !document.IsBsonArray)
		return null;
	List<MetadataItem> items = new List<MetadataItem>();
	foreach (var b in document.AsBsonArray)

		items.Add(new MetadataItem { Application = b["Application"].AsString, Values = b["Values"].IsBsonNull ? string.Empty : b["Values"].ToJson() });
	return items.ToArray();