A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.mongodb.com/docs/manual/tutorial/aggregation-examples/filtered-subset/ below:

Filter Data - Database Manual

This tutorial illustrates how to construct an aggregation pipeline, perform the aggregation on a collection, and display the results using the language of your choice.

This tutorial demonstrates how to query for a specific subset of documents in a collection.

The aggregation pipeline performs the following operations:

➤ Use the Select your language drop-down menu in the upper-right to set the language of the following examples or select MongoDB Shell.

This example uses a persons collection, which contains documents describing each person's name, date of birth, vocation, and other details. The aggregation selects documents based on whether their field values match specified criteria.

To create the persons collection, use the insertMany() method:

db.persons.insertMany( [   {      person_id: "6392529400",      firstname: "Elise",      lastname: "Smith",      dateofbirth: new Date("1972-01-13T09:32:07Z"),      vocation: "ENGINEER",      address: {         number: 5625,         street: "Tipa Circle",         city: "Wojzinmoj",      }   },   {      person_id: "1723338115",      firstname: "Olive",      lastname: "Ranieri",      dateofbirth: new Date("1985-05-12T23:14:30Z"),      gender: "FEMALE",      vocation: "ENGINEER",      address: {         number: 9303,         street: "Mele Circle",         city: "Tobihbo",      }   },   {      person_id: "8732762874",      firstname: "Toni",      lastname: "Jones",      dateofbirth: new Date("1991-11-23T16:53:56Z"),      vocation: "POLITICIAN",      address: {         number: 1,         street: "High Street",         city: "Upper Abbeywoodington",      }   },   {      person_id: "7363629563",      firstname: "Bert",      lastname: "Gooding",      dateofbirth: new Date("1941-04-07T22:11:52Z"),      vocation: "FLORIST",      address: {         number: 13,         street: "Upper Bold Road",         city: "Redringtonville",      }   },   {      person_id: "1029648329",      firstname: "Sophie",      lastname: "Celements",      dateofbirth: new Date("1959-07-06T17:35:45Z"),      vocation: "ENGINEER",      address: {         number: 5,         street: "Innings Close",         city: "Basilbridge",      }   },   {      person_id: "7363626383",      firstname: "Carl",      lastname: "Simmons",      dateofbirth: new Date("1998-12-26T13:13:55Z"),      vocation: "ENGINEER",      address: {         number: 187,         street: "Hillside Road",         city: "Kenningford",      }   }] )

Before you begin following this aggregation tutorial, you must set up a new C app. You can use this app to connect to a MongoDB deployment, insert sample data into MongoDB, and run the aggregation pipeline.

After you install the driver, create a file called agg-tutorial.c. Paste the following code in this file to create an app template for the aggregation tutorials.

Important

In the following code, read the code comments to find the sections of the code that you must modify for the tutorial you are following.

If you attempt to run the code without making any changes, you will encounter a connection error.

#include <stdio.h>#include <bson/bson.h>#include <mongoc/mongoc.h>int main(void){    mongoc_init();        char *uri = "<connection string>";    mongoc_client_t* client = mongoc_client_new(uri);                                                                                                                                {        const bson_t *doc;                bson_t *pipeline = BCON_NEW("pipeline", "[",                "]");                        bson_destroy(pipeline);                while (mongoc_cursor_next(results, &doc))        {            char *str = bson_as_canonical_extended_json(doc, NULL);            printf("%s\n", str);            bson_free(str);        }        bson_error_t error;        if (mongoc_cursor_error(results, &error))        {            fprintf(stderr, "Aggregation error: %s\n", error.message);        }        mongoc_cursor_destroy(results);    }            mongoc_client_destroy(client);    mongoc_cleanup();    return EXIT_SUCCESS;}

For every tutorial, you must replace the connection string placeholder with your deployment's connection string.

Tip

To learn how to locate your deployment's connection string, see the Create a Connection String step of the C Get Started guide.

For example, if your connection string is "mongodb+srv://mongodb-example:27017", your connection string assignment resembles the following:

char *uri = "mongodb+srv://mongodb-example:27017";

This example uses a persons collection, which contains documents describing each person's name, date of birth, vocation, and other details. The aggregation selects documents based on whether their field values match specified criteria.

To create the persons collection and insert the sample data, add the following code to your application:

mongoc_collection_t *persons = mongoc_client_get_collection(client, "agg_tutorials_db", "persons");{    bson_t *filter = bson_new();    bson_error_t error;    if (!mongoc_collection_delete_many(persons, filter, NULL, NULL, &error))    {        fprintf(stderr, "Delete error: %s\n", error.message);    }    bson_destroy(filter);}{    size_t num_docs = 6;    bson_t *docs[num_docs];    docs[0] = BCON_NEW(        "person_id", "6392529400",        "firstname", "Elise",        "lastname", "Smith",        "dateofbirth", BCON_DATE_TIME(653616727000UL),         "vocation", "ENGINEER",        "address", "{",        "number", BCON_INT32(5625),        "street", "Tipa Circle",        "city", "Wojzinmoj",        "}");    docs[1] = BCON_NEW(        "person_id", "1723338115",        "firstname", "Olive",        "lastname", "Ranieri",        "dateofbirth", BCON_DATE_TIME(485113500000UL),         "gender", "FEMALE",        "vocation", "ENGINEER",        "address", "{",        "number", BCON_INT32(9303),        "street", "Mele Circle",        "city", "Tobihbo",        "}");    docs[2] = BCON_NEW(        "person_id", "8732762874",        "firstname", "Toni",        "lastname", "Jones",        "dateofbirth", BCON_DATE_TIME(690559710000UL),         "vocation", "POLITICIAN",        "address", "{",        "number", BCON_INT32(1),        "street", "High Street",        "city", "Upper Abbeywoodington",        "}");    docs[3] = BCON_NEW(        "person_id", "7363629563",        "firstname", "Bert",        "lastname", "Gooding",        "dateofbirth", BCON_DATE_TIME(449595112000UL),         "vocation", "FLORIST",        "address", "{",        "number", BCON_INT32(13),        "street", "Upper Bold Road",        "city", "Redringtonville",        "}");    docs[4] = BCON_NEW(        "person_id", "1029648329",        "firstname", "Sophie",        "lastname", "Celements",        "dateofbirth", BCON_DATE_TIME(316265745000UL),         "vocation", "ENGINEER",        "address", "{",        "number", BCON_INT32(5),        "street", "Innings Close",        "city", "Basilbridge",        "}");    docs[5] = BCON_NEW(        "person_id", "7363626383",        "firstname", "Carl",        "lastname", "Simmons",        "dateofbirth", BCON_DATE_TIME(915250045000UL),         "vocation", "ENGINEER",        "address", "{",        "number", BCON_INT32(187),        "street", "Hillside Road",        "city", "Kenningford",        "}");    bson_error_t error;    if (!mongoc_collection_insert_many(persons, (const bson_t **)docs, num_docs, NULL, NULL, &error))    {        fprintf(stderr, "Insert error: %s\n", error.message);    }    for (int i = 0; i < num_docs; i++)    {        bson_destroy(docs[i]);    }}

Before you begin following an aggregation tutorial, you must set up a new C++ app. You can use this app to connect to a MongoDB deployment, insert sample data into MongoDB, and run the aggregation pipeline.

After you install the driver, create a file called agg-tutorial.cpp. Paste the following code in this file to create an app template for the aggregation tutorials.

Important

In the following code, read the code comments to find the sections of the code that you must modify for the tutorial you are following.

If you attempt to run the code without making any changes, you will encounter a connection error.

#include <iostream>#include <bsoncxx/builder/basic/document.hpp>#include <bsoncxx/builder/basic/kvp.hpp>#include <bsoncxx/json.hpp>#include <mongocxx/client.hpp>#include <mongocxx/instance.hpp>#include <mongocxx/pipeline.hpp>#include <mongocxx/uri.hpp>#include <chrono>using bsoncxx::builder::basic::kvp;using bsoncxx::builder::basic::make_document;using bsoncxx::builder::basic::make_array;int main() {   mongocxx::instance instance;      mongocxx::uri uri("<connection string>");   mongocxx::client client(uri);   auto db = client["agg_tutorials_db"];      db.drop();                     mongocxx::pipeline pipeline;            auto cursor = orders.aggregate(pipeline);   for (auto&& doc : cursor) {      std::cout << bsoncxx::to_json(doc, bsoncxx::ExtendedJsonMode::k_relaxed) << std::endl;   }}

For every tutorial, you must replace the connection string placeholder with your deployment's connection string.

Tip

To learn how to locate your deployment's connection string, see the Create a Connection String step of the C++ Get Started tutorial.

For example, if your connection string is "mongodb+srv://mongodb-example:27017", your connection string assignment resembles the following:

mongocxx::uri uri{"mongodb+srv://mongodb-example:27017"};

This example uses a persons collection, which contains documents describing each person's name, date of birth, vocation, and other details. The aggregation selects documents based on whether their field values match specified criteria.

To create the persons collection and insert the sample data, add the following code to your application:

auto persons = db["persons"];std::vector<bsoncxx::document::value> docs = {    bsoncxx::from_json(R"({ "person_id": "6392529400", "firstname": "Elise", "lastname": "Smith", "dateofbirth": {"$date": 620947927}, "vocation": "ENGINEER", "address": { "number": 5625, "street": "Tipa Circle", "city": "Wojzinmoj" } })"),    bsoncxx::from_json(R"({ "person_id": "1723338115", "firstname": "Olive", "lastname": "Ranieri", "dateofbirth": {"$date": 485529270000}, "gender": "FEMALE", "vocation": "ENGINEER", "address": { "number": 9303, "street": "Mele Circle", "city": "Tobihbo" } })"),    bsoncxx::from_json(R"({ "person_id": "8732762874", "firstname": "Toni", "lastname": "Jones", "dateofbirth": {"$date": 690978836000}, "vocation": "POLITICIAN", "address": { "number": 1, "street": "High Street", "city": "Upper Abbeywoodington" } })"),    bsoncxx::from_json(R"({ "person_id": "7363629563", "firstname": "Bert", "lastname": "Gooding", "dateofbirth": {"$date": -88368048000}, "vocation": "FLORIST", "address": { "number": 13, "street": "Upper Bold Road", "city": "Redringtonville" } })"),    bsoncxx::from_json(R"({ "person_id": "1029648329", "firstname": "Sophie", "lastname": "Celements", "dateofbirth": {"$date": -31561935000}, "vocation": "ENGINEER", "address": { "number": 5, "street": "Innings Close", "city": "Basilbridge" } })"),    bsoncxx::from_json(R"({ "person_id": "7363626383", "firstname": "Carl", "lastname": "Simmons", "dateofbirth": {"$date": 915148835000}, "vocation": "ENGINEER", "address": { "number": 187, "street": "Hillside Road", "city": "Kenningford" } })")};auto result = persons.insert_many(docs); 

Before you begin following this aggregation tutorial, you must set up a new C#/.NET app. You can use this app to connect to a MongoDB deployment, insert sample data into MongoDB, and run the aggregation pipeline.

After you install the driver, paste the following code into your Program.cs file to create an app template for the aggregation tutorials.

Important

In the following code, read the code comments to find the sections of the code that you must modify for the tutorial you are following.

If you attempt to run the code without making any changes, you will encounter a connection error.

using MongoDB.Bson;using MongoDB.Bson.Serialization.Attributes;using MongoDB.Driver;var uri = "<connection string>";var client = new MongoClient(uri);var aggDB = client.GetDatabase("agg_tutorials_db");foreach (var result in results.ToList()){    Console.WriteLine(result);}

For every tutorial, you must replace the connection string placeholder with your deployment's connection string.

For example, if your connection string is "mongodb+srv://mongodb-example:27017", your connection string assignment resembles the following:

var uri = "mongodb+srv://mongodb-example:27017";

This example uses a persons collection, which contains documents describing each person's name, date of birth, vocation, and other details. The aggregation selects documents based on whether their field values match specified criteria.

First, create C# classes to model the data in the persons collection:

public class Person{    [BsonId]    public ObjectId Id { get; set; }    public string PersonId { get; set; } = "";    public string FirstName { get; set; } = "";    public string LastName { get; set; } = "";    public DateTime DateOfBirth { get; set; }    [BsonIgnoreIfNull]    public string? Gender { get; set; }    public string Vocation { get; set; } = "";    public Address? Address { get; set; }}public class Address{    public int Number { get; set; }    public string Street { get; set; } = "";    public string City { get; set; } = "";}

To create the persons collection and insert the sample data, add the following code to your application:

var persons = aggDB.GetCollection<Person>("persons");persons.InsertMany(new List<Person>{    new Person    {        PersonId = "6392529400",        FirstName = "Elise",        LastName = "Smith",        DateOfBirth = DateTime.Parse("1972-01-13T09:32:07Z"),        Vocation = "ENGINEER",        Address = new Address        {            Number = 5625,            Street = "Tipa Circle",            City = "Wojzinmoj"        }    },    new Person    {        PersonId = "1723338115",        FirstName = "Olive",        LastName = "Ranieri",        DateOfBirth = DateTime.Parse("1985-05-12T23:14:30Z"),        Gender = "FEMALE",        Vocation = "ENGINEER",        Address = new Address        {            Number = 9303,            Street = "Mele Circle",            City = "Tobihbo"        }    },    new Person    {        PersonId = "8732762874",        FirstName = "Toni",        LastName = "Jones",        DateOfBirth = DateTime.Parse("1991-11-23T16:53:56Z"),        Vocation = "POLITICIAN",        Address = new Address        {            Number = 1,            Street = "High Street",            City = "Upper Abbeywoodington"        }    },    new Person    {        PersonId = "7363629563",        FirstName = "Bert",        LastName = "Gooding",        DateOfBirth = DateTime.Parse("1941-04-07T22:11:52Z"),        Vocation = "FLORIST",        Address = new Address        {            Number = 13,            Street = "Upper Bold Road",            City = "Redringtonville"        }    },    new Person    {        PersonId = "1029648329",        FirstName = "Sophie",        LastName = "Celements",        DateOfBirth = DateTime.Parse("1959-07-06T17:35:45Z"),        Vocation = "ENGINEER",        Address = new Address        {            Number = 5,            Street = "Innings Close",            City = "Basilbridge"        }    },    new Person    {        PersonId = "7363626383",        FirstName = "Carl",        LastName = "Simmons",        DateOfBirth = DateTime.Parse("1998-12-26T13:13:55Z"),        Vocation = "ENGINEER",        Address = new Address        {            Number = 187,            Street = "Hillside Road",            City = "Kenningford"        }    }});

Before you begin following this aggregation tutorial, you must set up a new Go app. You can use this app to connect to a MongoDB deployment, insert sample data into MongoDB, and run the aggregation pipeline.

After you install the driver, create a file called agg_tutorial.go. Paste the following code in this file to create an app template for the aggregation tutorials.

Important

In the following code, read the code comments to find the sections of the code that you must modify for the tutorial you are following.

If you attempt to run the code without making any changes, you will encounter a connection error.

package mainimport (    "context"    "fmt"    "log"    "time"    "go.mongodb.org/mongo-driver/v2/bson"    "go.mongodb.org/mongo-driver/v2/mongo"    "go.mongodb.org/mongo-driver/v2/mongo/options")func main() {        const uri = "<connection string>"    client, err := mongo.Connect(options.Client().ApplyURI(uri))    if err != nil {        log.Fatal(err)    }    defer func() {        if err = client.Disconnect(context.TODO()); err != nil {            log.Fatal(err)        }    }()    aggDB := client.Database("agg_tutorials_db")                                                        if err != nil {        log.Fatal(err)    }    defer func() {        if err := cursor.Close(context.TODO()); err != nil {            log.Fatalf("failed to close cursor: %v", err)        }    }()        var results []bson.D    if err = cursor.All(context.TODO(), &results); err != nil {        log.Fatalf("failed to decode results: %v", err)    }        for _, result := range results {        res, _ := bson.MarshalExtJSON(result, false, false)        fmt.Println(string(res))    }}

For every tutorial, you must replace the connection string placeholder with your deployment's connection string.

Tip

To learn how to locate your deployment's connection string, see the Create a MongoDB Cluster step of the Go Quick Start guide.

For example, if your connection string is "mongodb+srv://mongodb-example:27017", your connection string assignment resembles the following:

const uri = "mongodb+srv://mongodb-example:27017";

This example uses a persons collection, which contains documents describing each person's name, date of birth, vocation, and other details. The aggregation selects documents based on whether their field values match specified criteria.

First, create Go structs to model the data in the persons collection:

type Person struct {	PersonID    string        `bson:"person_id"`	Firstname   string        `bson:"firstname"`	Lastname    string        `bson:"lastname"`	Gender      string        `bson:"gender,omitempty"`	DateOfBirth bson.DateTime `bson:"dateofbirth"`	Vocation    string        `bson:"vocation"`	Address     Address       `bson:"address"`}type Address struct {	Number int	Street string	City   string}

To create the persons collection and insert the sample data, add the following code to your application:

persons := aggDB.Collection("persons")persons.DeleteMany(context.TODO(), bson.D{})_, err = persons.InsertMany(context.TODO(), []interface{}{	Person{		PersonID:    "6392529400",		Firstname:   "Elise",		Lastname:    "Smith",		DateOfBirth: bson.NewDateTimeFromTime(time.Date(1972, 1, 13, 9, 32, 7, 0, time.UTC)),		Vocation:    "ENGINEER",		Address:     Address{Number: 5625, Street: "Tipa Circle", City: "Wojzinmoj"},	},	Person{		PersonID:    "1723338115",		Firstname:   "Olive",		Lastname:    "Ranieri",		Gender:      "FEMALE",		DateOfBirth: bson.NewDateTimeFromTime(time.Date(1985, 5, 12, 23, 14, 30, 0, time.UTC)),		Vocation:    "ENGINEER",		Address:     Address{Number: 9303, Street: "Mele Circle", City: "Tobihbo"},	},	Person{		PersonID:    "8732762874",		Firstname:   "Toni",		Lastname:    "Jones",		DateOfBirth: bson.NewDateTimeFromTime(time.Date(1991, 11, 23, 16, 53, 56, 0, time.UTC)),		Vocation:    "POLITICIAN",		Address:     Address{Number: 1, Street: "High Street", City: "Upper Abbeywoodington"},	},	Person{		PersonID:    "7363629563",		Firstname:   "Bert",		Lastname:    "Gooding",		DateOfBirth: bson.NewDateTimeFromTime(time.Date(1941, 4, 7, 22, 11, 52, 0, time.UTC)),		Vocation:    "FLORIST",		Address:     Address{Number: 13, Street: "Upper Bold Road", City: "Redringtonville"},	},	Person{		PersonID:    "1029648329",		Firstname:   "Sophie",		Lastname:    "Celements",		DateOfBirth: bson.NewDateTimeFromTime(time.Date(1959, 7, 6, 17, 35, 45, 0, time.UTC)),		Vocation:    "ENGINEER",		Address:     Address{Number: 5, Street: "Innings Close", City: "Basilbridge"},	},	Person{		PersonID:    "7363626383",		Firstname:   "Carl",		Lastname:    "Simmons",		DateOfBirth: bson.NewDateTimeFromTime(time.Date(1998, 12, 26, 13, 13, 55, 0, time.UTC)),		Vocation:    "ENGINEER",		Address:     Address{Number: 187, Street: "Hillside Road", City: "Kenningford"},	},})if err != nil {	log.Fatal(err)}

Before you begin following an aggregation tutorial, you must set up a new Java app. You can use this app to connect to a MongoDB deployment, insert sample data into MongoDB, and run the aggregation pipeline.

After you install the driver, create a file called AggTutorial.java. Paste the following code in this file to create an app template for the aggregation tutorials.

Important

In the following code, read the code comments to find the sections of the code that you must modify for the tutorial you are following.

If you attempt to run the code without making any changes, you will encounter a connection error.

package org.example;import com.mongodb.client.*;import com.mongodb.client.model.Accumulators;import com.mongodb.client.model.Aggregates;import com.mongodb.client.model.Field;import com.mongodb.client.model.Filters;import com.mongodb.client.model.Sorts;import com.mongodb.client.model.Variable;import org.bson.Document;import org.bson.conversions.Bson;import java.time.LocalDateTime;import java.util.ArrayList;import java.util.Arrays;import java.util.Collections;import java.util.List;public class AggTutorial {    public static void main(String[] args) {                String uri = "<connection string>";        try (MongoClient mongoClient = MongoClients.create(uri)) {            MongoDatabase aggDB = mongoClient.getDatabase("agg_tutorials_db");                                                                                    List<Bson> pipeline = new ArrayList<>();                                                                                    for (Document document : aggregationResult) {                System.out.println(document.toJson());            }        }    }}

For every tutorial, you must replace the connection string placeholder with your deployment's connection string.

Tip

To learn how to locate your deployment's connection string, see the Create a Connection String step of the Java Sync Quick Start guide.

For example, if your connection string is "mongodb+srv://mongodb-example:27017", your connection string assignment resembles the following:

String uri = "mongodb+srv://mongodb-example:27017";

This example uses a persons collection, which contains documents describing each person's name, date of birth, vocation, and other details. The aggregation selects documents based on whether their field values match specified criteria.

To create the persons collection and insert the sample data, add the following code to your application:

MongoDatabase aggDB = mongoClient.getDatabase("agg_tutorials_db");MongoCollection<Document> persons = aggDB.getCollection("persons");persons.insertMany(        Arrays.asList(                new Document("person_id", "6392529400")                        .append("firstname", "Elise")                        .append("lastname", "Smith")                        .append("dateofbirth", LocalDateTime.parse("1972-01-13T09:32:07"))                        .append("vocation", "ENGINEER")                        .append("address", new Document("number", 5625)                                .append("street", "Tipa Circle")                                .append("city", "Wojzinmoj")),                new Document("person_id", "1723338115")                        .append("firstname", "Olive")                        .append("lastname", "Ranieri")                        .append("dateofbirth", LocalDateTime.parse("1985-05-12T23:14:30"))                        .append("gender", "FEMALE")                        .append("vocation", "ENGINEER")                        .append("address", new Document("number", 9303)                                .append("street", "Mele Circle")                                .append("city", "Tobihbo")),                new Document("person_id", "8732762874")                        .append("firstname", "Toni")                        .append("lastname", "Jones")                        .append("dateofbirth", LocalDateTime.parse("1991-11-23T16:53:56"))                        .append("vocation", "POLITICIAN")                        .append("address", new Document("number", 1)                                .append("street", "High Street")                                .append("city", "Upper Abbeywoodington")),                new Document("person_id", "7363629563")                        .append("firstname", "Bert")                        .append("lastname", "Gooding")                        .append("dateofbirth", LocalDateTime.parse("1941-04-07T22:11:52"))                        .append("vocation", "FLORIST")                        .append("address", new Document("number", 13)                                .append("street", "Upper Bold Road")                                .append("city", "Redringtonville")),                new Document("person_id", "1029648329")                        .append("firstname", "Sophie")                        .append("lastname", "Celements")                        .append("dateofbirth", LocalDateTime.parse("1959-07-06T17:35:45"))                        .append("vocation", "ENGINEER")                        .append("address", new Document("number", 5)                                .append("street", "Innings Close")                                .append("city", "Basilbridge")),                new Document("person_id", "7363626383")                        .append("firstname", "Carl")                        .append("lastname", "Simmons")                        .append("dateofbirth", LocalDateTime.parse("1998-12-26T13:13:55"))                        .append("vocation", "ENGINEER")                        .append("address", new Document("number", 187)                                .append("street", "Hillside Road")                                .append("city", "Kenningford"))        ));

Before you begin following an aggregation tutorial, you must set up a new Kotlin app. You can use this app to connect to a MongoDB deployment, insert sample data into MongoDB, and run the aggregation pipeline.

In addition to the driver, you must also add the following dependencies to your build.gradle.kts file and reload your project:

dependencies {        implementation("org.jetbrains.kotlinx:kotlinx-serialization-core:1.5.1")        implementation("org.jetbrains.kotlinx:kotlinx-datetime:0.6.1")}

After you install the driver, create a file called AggTutorial.kt. Paste the following code in this file to create an app template for the aggregation tutorials.

Important

In the following code, read the code comments to find the sections of the code that you must modify for the tutorial you are following.

If you attempt to run the code without making any changes, you will encounter a connection error.

package org.exampleimport com.mongodb.client.model.*import com.mongodb.kotlin.client.coroutine.MongoClientimport kotlinx.coroutines.runBlockingimport kotlinx.datetime.LocalDateTimeimport kotlinx.datetime.toJavaLocalDateTimeimport kotlinx.serialization.Contextualimport kotlinx.serialization.Serializableimport org.bson.Documentimport org.bson.conversions.Bson@Serializabledata class MyClass(    ...)suspend fun main() {        val uri = "<connection string>"    MongoClient.create(uri).use { mongoClient ->        val aggDB = mongoClient.getDatabase("agg_tutorials_db")                                                                val pipeline = mutableListOf<Bson>()                                                aggregationResult.collect { println(it) }    }}

For every tutorial, you must replace the connection string placeholder with your deployment's connection string.

Tip

To learn how to locate your deployment's connection string, see the Connect to your Cluster step of the Kotlin Driver Quick Start guide.

For example, if your connection string is "mongodb+srv://mongodb-example:27017", your connection string assignment resembles the following:

val uri = "mongodb+srv://mongodb-example:27017"

This example uses a persons collection, which contains documents describing each person's name, date of birth, vocation, and other details. The aggregation selects documents based on whether their field values match specified criteria.

First, create Kotlin data classes to model the data in the persons collection:

@Serializabledata class Address(    val number: Int,    val street: String,    val city: String)@Serializabledata class Person(    val personID: String,    val firstname: String,    val lastname: String,    @Contextual val dateOfBirth: LocalDateTime,    val vocation: String,    val address: Address,    val gender: String? = null)

To create the persons collection and insert the sample data, add the following code to your application:

val persons = aggDB.getCollection<Person>("persons")persons.deleteMany(Filters.empty())persons.insertMany(    listOf(        Person(            "6392529400",            "Elise",            "Smith",            LocalDateTime.parse("1972-01-13T09:32:07"),            "ENGINEER",            Address(5625, "Tipa Circle", "Wojzinmoj")        ),        Person(            "1723338115",            "Olive",            "Ranieri",            LocalDateTime.parse("1985-05-12T23:14:30"),            "ENGINEER",            Address(9303, "Mele Circle", "Tobihbo"),            "FEMALE"        ),        Person(            "8732762874",            "Toni",            "Jones",            LocalDateTime.parse("1991-11-23T16:53:56"),            "POLITICIAN",            Address(1, "High Street", "Upper Abbeywoodington")        ),        Person(            "7363629563",            "Bert",            "Gooding",            LocalDateTime.parse("1941-04-07T22:11:52"),            "FLORIST",            Address(13, "Upper Bold Road", "Redringtonville")        ),        Person(            "1029648329",            "Sophie",            "Celements",            LocalDateTime.parse("1959-07-06T17:35:45"),            "ENGINEER",            Address(5, "Innings Close", "Basilbridge")        ),        Person(            "7363626383",            "Carl",            "Simmons",            LocalDateTime.parse("1998-12-26T13:13:55"),            "ENGINEER",            Address(187, "Hillside Road", "Kenningford")        )    ))

Before you begin following this aggregation tutorial, you must set up a new Node.js app. You can use this app to connect to a MongoDB deployment, insert sample data into MongoDB, and run the aggregation pipeline.

After you install the driver, create a file to run the tutorial template. Paste the following code in this file to create an app template for the aggregation tutorials.

Important

In the following code, read the code comments to find the sections of the code that you must modify for the tutorial you are following.

If you attempt to run the code without making any changes, you will encounter a connection error.

const { MongoClient } = require('mongodb');const uri = '<connection-string>';const client = new MongoClient(uri);export async function run() {  try {    const aggDB = client.db('agg_tutorials_db');                                        const pipeline = [];                        for await (const document of aggregationResult) {      console.log(document);    }  } finally {    await client.close();  }}run().catch(console.dir);

For every tutorial, you must replace the connection string placeholder with your deployment's connection string.

Tip

To learn how to locate your deployment's connection string, see the Create a Connection String step of the Node.js Quick Start guide.

For example, if your connection string is "mongodb+srv://mongodb-example:27017", your connection string assignment resembles the following:

const uri = "mongodb+srv://mongodb-example:27017";

This example uses a persons collection, which contains documents describing each person's name, date of birth, vocation, and other details. The aggregation selects documents based on whether their field values match specified criteria.

To create the persons collection and insert the sample data, add the following code to your application:

const persons = aggDB.collection('persons');await persons.insertMany([  {    person_id: '6392529400',    firstname: 'Elise',    lastname: 'Smith',    dateofbirth: new Date('1972-01-13T09:32:07Z'),    vocation: 'ENGINEER',    address: {      number: 5625,      street: 'Tipa Circle',      city: 'Wojzinmoj',    },  },  {    person_id: '1723338115',    firstname: 'Olive',    lastname: 'Ranieri',    dateofbirth: new Date('1985-05-12T23:14:30Z'),    gender: 'FEMALE',    vocation: 'ENGINEER',    address: {      number: 9303,      street: 'Mele Circle',      city: 'Tobihbo',    },  },  {    person_id: '8732762874',    firstname: 'Toni',    lastname: 'Jones',    dateofbirth: new Date('1991-11-23T16:53:56Z'),    vocation: 'POLITICIAN',    address: {      number: 1,      street: 'High Street',      city: 'Upper Abbeywoodington',    },  },  {    person_id: '7363629563',    firstname: 'Bert',    lastname: 'Gooding',    dateofbirth: new Date('1941-04-07T22:11:52Z'),    vocation: 'FLORIST',    address: {      number: 13,      street: 'Upper Bold Road',      city: 'Redringtonville',    },  },  {    person_id: '1029648329',    firstname: 'Sophie',    lastname: 'Celements',    dateofbirth: new Date('1959-07-06T17:35:45Z'),    vocation: 'ENGINEER',    address: {      number: 5,      street: 'Innings Close',      city: 'Basilbridge',    },  },  {    person_id: '7363626383',    firstname: 'Carl',    lastname: 'Simmons',    dateofbirth: new Date('1998-12-26T13:13:55Z'),    vocation: 'ENGINEER',    address: {      number: 187,      street: 'Hillside Road',      city: 'Kenningford',    },  },]);

Before you begin following this aggregation tutorial, you must set up a new PHP app. You can use this app to connect to a MongoDB deployment, insert sample data into MongoDB, and run the aggregation pipeline.

After you install the library, create a file called agg_tutorial.php. Paste the following code in this file to create an app template for the aggregation tutorials.

Important

In the following code, read the code comments to find the sections of the code that you must modify for the tutorial you are following.

If you attempt to run the code without making any changes, you will encounter a connection error.

<?phprequire 'vendor/autoload.php';use MongoDB\Client;use MongoDB\BSON\UTCDateTime;use MongoDB\Builder\Pipeline;use MongoDB\Builder\Stage;use MongoDB\Builder\Type\Sort;use MongoDB\Builder\Query;use MongoDB\Builder\Expression;use MongoDB\Builder\Accumulator;use function MongoDB\object;$uri = '<connection string>';$client = new Client($uri);foreach ($cursor as $doc) {    echo json_encode($doc, JSON_PRETTY_PRINT), PHP_EOL;}

For every tutorial, you must replace the connection string placeholder with your deployment's connection string.

Tip

To learn how to locate your deployment's connection string, see the Create a Connection String step of the Get Started with the PHP Library tutorial.

For example, if your connection string is "mongodb+srv://mongodb-example:27017", your connection string assignment resembles the following:

$uri = 'mongodb+srv://mongodb-example:27017';

This example uses a persons collection, which contains documents describing each person's name, date of birth, vocation, and other details. The aggregation selects documents based on whether their field values match specified criteria.

To create the persons collection and insert the sample data, add the following code to your application:

$persons = $client->agg_tutorials_db->persons;$persons->deleteMany([]);$persons->insertMany(    [        [            'person_id' => '6392529400',            'firstname' => 'Elise',            'lastname' => 'Smith',            'dateofbirth' => new UTCDateTime(new DateTimeImmutable('1972-01-13T09:32:07')),            'vocation' => 'ENGINEER',            'address' => ['number' => 5625, 'Street' => 'Tipa Circle', 'city' => 'Wojzinmoj'],        ],        [            'person_id' => '1723338115',            'firstname' => 'Olive',            'lastname' => 'Ranieri',            'gender' => 'FEMALE',            'dateofbirth' => new UTCDateTime(new DateTimeImmutable('1985-05-12T23:14:30')),            'vocation' => 'ENGINEER',            'address' => ['number' => 9303, 'street' => 'Mele Circle', 'city' => 'Tobihbo'],        ],        [            'person_id' => '8732762874',            'firstname' => 'Toni',            'lastname' => 'Jones',            'dateofbirth' => new UTCDateTime(new DateTimeImmutable('1991-11-23T16:53:56')),            'vocation' => 'POLITICIAN',            'address' => ['number' => 1, 'street' => 'High Street', 'city' => 'Upper Abbeywoodington'],        ],        [            'person_id' => '7363629563',            'firstname' => 'Bert',            'lastname' => 'Gooding',            'dateofbirth' => new UTCDateTime(new DateTimeImmutable('1941-04-07T22:11:52')),            'vocation' => 'FLORIST',            'address' => ['number' => 13, 'street' => 'Upper Bold Road', 'city' => 'Redringtonville'],        ],        [            'person_id' => '1029648329',            'firstname' => 'Sophie',            'lastname' => 'Celements',            'dateofbirth' => new UTCDateTime(new DateTimeImmutable('1959-07-06T17:35:45')),            'vocation' => 'ENGINEER',            'address' => ['number' => 5, 'street' => 'Innings Close', 'city' => 'Basilbridge'],        ],        [            'person_id' => '7363626383',            'firstname' => 'Carl',            'lastname' => 'Simmons',            'dateofbirth' => new UTCDateTime(new DateTimeImmutable('1998-12-26T13:13:55')),            'vocation' => 'ENGINEER',            'address' => ['number' => 187, 'street' => 'Hillside Road', 'city' => 'Kenningford'],        ]    ]);

Before you begin following this aggregation tutorial, you must set up a new Python app. You can use this app to connect to a MongoDB deployment, insert sample data into MongoDB, and run the aggregation pipeline.

After you install the library, create a file called agg_tutorial.py. Paste the following code in this file to create an app template for the aggregation tutorials.

Important

In the following code, read the code comments to find the sections of the code that you must modify for the tutorial you are following.

If you attempt to run the code without making any changes, you will encounter a connection error.

from pymongo import MongoClienturi = "<connection-string>"client = MongoClient(uri)try:    agg_db = client["agg_tutorials_db"]                                    pipeline = []                        for document in aggregation_result:        print(document)finally:    client.close()

For every tutorial, you must replace the connection string placeholder with your deployment's connection string.

Tip

To learn how to locate your deployment's connection string, see the Create a Connection String step of the Get Started with the PHP Library tutorial.

For example, if your connection string is "mongodb+srv://mongodb-example:27017", your connection string assignment resembles the following:

uri = "mongodb+srv://mongodb-example:27017"

This example uses a persons collection, which contains documents describing each person's name, date of birth, vocation, and other details. The aggregation selects documents based on whether their field values match specified criteria.

To create the persons collection and insert the sample data, add the following code to your application:

person_coll = agg_db["persons"]person_data = [    {        "person_id": "6392529400",        "firstname": "Elise",        "lastname": "Smith",        "dateofbirth": datetime(1972, 1, 13, 9, 32, 7),        "vocation": "ENGINEER",        "address": {            "number": 5625,            "street": "Tipa Circle",            "city": "Wojzinmoj",        },    },    {        "person_id": "1723338115",        "firstname": "Olive",        "lastname": "Ranieri",        "dateofbirth": datetime(1985, 5, 12, 23, 14, 30),        "gender": "FEMALE",        "vocation": "ENGINEER",        "address": {            "number": 9303,            "street": "Mele Circle",            "city": "Tobihbo",        },    },    {        "person_id": "8732762874",        "firstname": "Toni",        "lastname": "Jones",        "dateofbirth": datetime(1991, 11, 23, 16, 53, 56),        "vocation": "POLITICIAN",        "address": {            "number": 1,            "street": "High Street",            "city": "Upper Abbeywoodington",        },    },    {        "person_id": "7363629563",        "firstname": "Bert",        "lastname": "Gooding",        "dateofbirth": datetime(1941, 4, 7, 22, 11, 52),        "vocation": "FLORIST",        "address": {            "number": 13,            "street": "Upper Bold Road",            "city": "Redringtonville",        },    },    {        "person_id": "1029648329",        "firstname": "Sophie",        "lastname": "Celements",        "dateofbirth": datetime(1959, 7, 6, 17, 35, 45),        "vocation": "ENGINEER",        "address": {            "number": 5,            "street": "Innings Close",            "city": "Basilbridge",        },    },    {        "person_id": "7363626383",        "firstname": "Carl",        "lastname": "Simmons",        "dateofbirth": datetime(1998, 12, 26, 13, 13, 55),        "vocation": "ENGINEER",        "address": {            "number": 187,            "street": "Hillside Road",            "city": "Kenningford",        },    },]person_coll.insert_many(person_data)

Before you begin following this aggregation tutorial, you must set up a new Ruby app. You can use this app to connect to a MongoDB deployment, insert sample data into MongoDB, and run the aggregation pipeline.

After you install the driver, create a file called agg_tutorial.rb. Paste the following code in this file to create an app template for the aggregation tutorials.

Important

In the following code, read the code comments to find the sections of the code that you must modify for the tutorial you are following.

If you attempt to run the code without making any changes, you will encounter a connection error.

require 'mongo'require 'bson'uri = "<connection string>"Mongo::Client.new(uri) do |client|  agg_db = client.use('agg_tutorials_db')                        aggregation_result.each do |doc|    puts doc  endend

For every tutorial, you must replace the connection string placeholder with your deployment's connection string.

Tip

To learn how to locate your deployment's connection string, see the Create a Connection String step of the Ruby Get Started guide.

For example, if your connection string is "mongodb+srv://mongodb-example:27017", your connection string assignment resembles the following:

uri = "mongodb+srv://mongodb-example:27017"

This example uses a persons collection, which contains documents describing each person's name, date of birth, vocation, and other details. The aggregation selects documents based on whether their field values match specified criteria.

To create the persons collection and insert the sample data, add the following code to your application:

persons = agg_db[:persons]persons.delete_many({})persons.insert_many(  [    {      person_id: "6392529400",      firstname: "Elise",      lastname: "Smith",      dateofbirth: DateTime.parse("1972-01-13T09:32:07Z"),      vocation: "ENGINEER",      address: {        number: 5625,        street: "Tipa Circle",        city: "Wojzinmoj",      },    },    {      person_id: "1723338115",      firstname: "Olive",      lastname: "Ranieri",      dateofbirth: DateTime.parse("1985-05-12T23:14:30Z"),      gender: "FEMALE",      vocation: "ENGINEER",      address: {        number: 9303,        street: "Mele Circle",        city: "Tobihbo",      },    },    {      person_id: "8732762874",      firstname: "Toni",      lastname: "Jones",      dateofbirth: DateTime.parse("1991-11-23T16:53:56Z"),      vocation: "POLITICIAN",      address: {        number: 1,        street: "High Street",        city: "Upper Abbeywoodington",      },    },    {      person_id: "7363629563",      firstname: "Bert",      lastname: "Gooding",      dateofbirth: DateTime.parse("1941-04-07T22:11:52Z"),      vocation: "FLORIST",      address: {        number: 13,        street: "Upper Bold Road",        city: "Redringtonville",      },    },    {      person_id: "1029648329",      firstname: "Sophie",      lastname: "Celements",      dateofbirth: DateTime.parse("1959-07-06T17:35:45Z"),      vocation: "ENGINEER",      address: {        number: 5,        street: "Innings Close",        city: "Basilbridge",      },    },    {      person_id: "7363626383",      firstname: "Carl",      lastname: "Simmons",      dateofbirth: DateTime.parse("1998-12-26T13:13:55Z"),      vocation: "ENGINEER",      address: {        number: 187,        street: "Hillside Road",        city: "Kenningford",      },    },  ])

Before you begin following this aggregation tutorial, you must set up a new Rust app. You can use this app to connect to a MongoDB deployment, insert sample data into MongoDB, and run the aggregation pipeline.

After you install the driver, create a file called agg-tutorial.rs. Paste the following code in this file to create an app template for the aggregation tutorials.

Important

In the following code, read the code comments to find the sections of the code that you must modify for the tutorial you are following.

If you attempt to run the code without making any changes, you will encounter a connection error.

use mongodb::{    bson::{doc, Document},    options::ClientOptions,    Client,};use futures::stream::TryStreamExt;use std::error::Error;#[tokio::main]async fn main() mongodb::error::Result<()> {        let uri = "<connection string>";    let client = Client::with_uri_str(uri).await?;    let agg_db = client.database("agg_tutorials_db");                                    let mut pipeline = Vec::new();                let mut results = some_coll.aggregate(pipeline).await?;    while let Some(result) = results.try_next().await? {       println!("{:?}\n", result);    }    Ok(())}

For every tutorial, you must replace the connection string placeholder with your deployment's connection string.

Tip

To learn how to locate your deployment's connection string, see the Create a Connection String step of the Rust Quick Start guide.

For example, if your connection string is "mongodb+srv://mongodb-example:27017", your connection string assignment resembles the following:

let uri = "mongodb+srv://mongodb-example:27017";

This example uses a persons collection, which contains documents describing each person's name, date of birth, vocation, and other details. The aggregation selects documents based on whether their field values match specified criteria.

First, create Rust structs to model the data in the persons collection:

#[derive(Debug, Serialize, Deserialize)]struct Address {    number: i32,    street: String,    city: String,}#[derive(Debug, Serialize, Deserialize)]struct Person {    person_id: String,    firstname: String,    lastname: String,    dateofbirth: DateTime,    vocation: String,    address: Address,}

To create the persons collection and insert the sample data, add the following code to your application:

let persons: Collection<Person> = agg_db.collection("persons");persons.delete_many(doc! {}).await?;let data = vec![    Person {        person_id: "6392529400".to_string(),        firstname: "Elise".to_string(),        lastname: "Smith".to_string(),        dateofbirth: DateTime::builder().year(1972).month(1).day(13).hour(9).minute(32).second(7).build().unwrap(),        vocation: "ENGINEER".to_string(),        address: Address {            number: 5625,            street: "Tipa Circle".to_string(),            city: "Wojzinmoj".to_string(),        },    },    Person {        person_id: "1723338115".to_string(),        firstname: "Olive".to_string(),        lastname: "Ranieri".to_string(),        gender: "FEMALE".to_string(),        dateofbirth: DateTime::builder().year(1985).month(5).day(12).hour(23).minute(14).second(30).build().unwrap(),        vocation: "ENGINEER".to_string(),        address: Address {            number: 9303,            street: "Mele Circle".to_string(),            city: "Tobihbo".to_string(),        },    },    Person {        person_id: "8732762874".to_string(),        firstname: "Toni".to_string(),        lastname: "Jones".to_string(),        dateofbirth: DateTime::builder().year(1991).month(11).day(23).hour(16).minute(53).second(56).build().unwrap(),        vocation: "POLITICIAN".to_string(),        address: Address {            number: 1,            street: "High Street".to_string(),            city: "Upper Abbeywoodington".to_string(),        },    },    Person {        person_id: "7363629563".to_string(),        firstname: "Bert".to_string(),        lastname: "Gooding".to_string(),        dateofbirth: DateTime::builder().year(1941).month(4).day(7).hour(22).minute(11).second(52).build().unwrap(),        vocation: "FLORIST".to_string(),        address: Address {            number: 13,            street: "Upper Bold Road".to_string(),            city: "Redringtonville".to_string(),        },    },    Person {        person_id: "1029648329".to_string(),        firstname: "Sophie".to_string(),        lastname: "Celements".to_string(),        dateofbirth: DateTime::builder().year(1959).month(7).day(6).hour(17).minute(35).second(45).build().unwrap(),        vocation: "ENGINEER".to_string(),        address: Address {            number: 5,            street: "Innings Close".to_string(),            city: "Basilbridge".to_string(),        },    },    Person {        person_id: "7363626383".to_string(),        firstname: "Carl".to_string(),        lastname: "Simmons".to_string(),        dateofbirth: DateTime::builder().year(1998).month(12).day(26).hour(13).minute(13).second(55).build().unwrap(),        vocation: "ENGINEER".to_string(),        address: Address {            number: 187,            street: "Hillside Road".to_string(),            city: "Kenningford".to_string(),        },    },];persons.insert_many(data).await?;

Before you begin following an aggregation tutorial, you must set up a new Scala app. You can use this app to connect to a MongoDB deployment, insert sample data into MongoDB, and run the aggregation pipeline.

After you install the driver, create a file called AggTutorial.scala. Paste the following code in this file to create an app template for the aggregation tutorials.

Important

In the following code, read the code comments to find the sections of the code that you must modify for the tutorial you are following.

If you attempt to run the code without making any changes, you will encounter a connection error.

package org.example;import org.mongodb.scala.MongoClientimport org.mongodb.scala.bson.Documentimport org.mongodb.scala.model.{Accumulators, Aggregates, Field, Filters, Variable}import java.text.SimpleDateFormatobject FilteredSubset {  def main(args: Array[String]): Unit = {        val uri = "<connection string>"    val mongoClient = MongoClient(uri)    Thread.sleep(1000)    val aggDB = mongoClient.getDatabase("agg_tutorials_db")                            val dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss")            Thread.sleep(1000)                    Thread.sleep(1000)    mongoClient.close()  }}

For every tutorial, you must replace the connection string placeholder with your deployment's connection string.

Tip

To learn how to locate your deployment's connection string, see the Create a Connection String step of the Scala Driver Get Started guide.

For example, if your connection string is "mongodb+srv://mongodb-example:27017", your connection string assignment resembles the following:

val uri = "mongodb+srv://mongodb-example:27017"

This example uses a persons collection, which contains documents describing each person's name, date of birth, vocation, and other details. The aggregation selects documents based on whether their field values match specified criteria.

To create the persons collection and insert the sample data, add the following code to your application:

val persons = aggDB.getCollection("persons")persons.deleteMany(Filters.empty()).subscribe(  _ => {},  e => println("Error: " + e.getMessage),)val dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss")persons.insertMany(Seq(  Document(    "person_id" -> "6392529400",    "firstname" -> "Elise",    "lastname" -> "Smith",    "dateofbirth" -> dateFormat.parse("1972-01-13T09:32:07"),    "vocation" -> "ENGINEER",    "address" -> Document(      "number" -> 5625,      "street" -> "Tipa Circle",      "city" -> "Wojzinmoj"    )  ),  Document(    "person_id" -> "1723338115",    "firstname" -> "Olive",    "lastname" -> "Ranieri",    "dateofbirth" -> dateFormat.parse("1985-05-12T23:14:30"),    "gender" -> "FEMALE",    "vocation" -> "ENGINEER",    "address" -> Document(      "number" -> 9303,      "street" -> "Mele Circle",      "city" -> "Tobihbo"    )  ),  Document(    "person_id" -> "8732762874",    "firstname" -> "Toni",    "lastname" -> "Jones",    "dateofbirth" -> dateFormat.parse("1991-11-23T16:53:56"),    "vocation" -> "POLITICIAN",    "address" -> Document(      "number" -> 1,      "street" -> "High Street",      "city" -> "Upper Abbeywoodington"    )  ),  Document(    "person_id" -> "7363629563",    "firstname" -> "Bert",    "lastname" -> "Gooding",    "dateofbirth" -> dateFormat.parse("1941-04-07T22:11:52"),    "vocation" -> "FLORIST",    "address" -> Document(      "number" -> 13,      "street" -> "Upper Bold Road",      "city" -> "Redringtonville"    )  ),  Document(    "person_id" -> "1029648329",    "firstname" -> "Sophie",    "lastname" -> "Celements",    "dateofbirth" -> dateFormat.parse("1959-07-06T17:35:45"),    "vocation" -> "ENGINEER",    "address" -> Document(      "number" -> 5,      "street" -> "Innings Close",      "city" -> "Basilbridge"    )  ),  Document(    "person_id" -> "7363626383",    "firstname" -> "Carl",    "lastname" -> "Simmons",    "dateofbirth" -> dateFormat.parse("1998-12-26T13:13:55"),    "vocation" -> "ENGINEER",    "address" -> Document(      "number" -> 187,      "street" -> "Hillside Road",      "city" -> "Kenningford"    )  ))).subscribe(  _ => {},  e => println("Error: " + e.getMessage),)

The following steps demonstrate how to create and run an aggregation pipeline to filter for a specific subset of documents.

db.persons.aggregate( [      { $match: { "vocation": "ENGINEER" } },      { $sort: { "dateofbirth": -1 } },      { $limit: 3 },      { $unset: [ "_id", "address"] }] )

The aggregated results contain three documents. The documents represent the three youngest people with the vocation of ENGINEER, ordered from youngest to oldest. The results omit the _id and address fields.

{   person_id: '7363626383',   firstname: 'Carl',   lastname: 'Simmons',   dateofbirth: ISODate("1998-12-26T13:13:55.000Z"),   vocation: 'ENGINEER'}{   person_id: '1723338115',   firstname: 'Olive',   lastname: 'Ranieri',   dateofbirth: ISODate("1985-05-12T23:14:30.000Z"),   gender: 'FEMALE',   vocation: 'ENGINEER'}{   person_id: '6392529400',   firstname: 'Elise',   lastname: 'Smith',   dateofbirth: ISODate("1972-01-13T09:32:07.000Z"),   vocation: 'ENGINEER'}

In your pipeline, create a $match stage that finds documents in which the value of the vocation field is "ENGINEER":

"{", "$match", "{", "vocation", BCON_UTF8("ENGINEER"), "}", "}",

Next, create a $sort stage that sorts the documents in descending order by the dateofbirth field to list the youngest people first:

"{", "$sort", "{", "dateofbirth", BCON_INT32(-1), "}", "}",

Next, create a $limit stage to the pipeline to output only the first three documents in the results.

"{", "$limit", BCON_INT32(3), "}",

Finally, create an $unset stage. The $unset stage removes unnecessary fields from the result documents:

"{", "$unset", "[", BCON_UTF8("_id"), BCON_UTF8("address"), "]", "}",
Tip

Use the $unset operator instead of $project to avoid modifying the aggregation pipeline if documents with different fields are added to the collection.

Add the following code to the end of your application to perform the aggregation on the persons collection:

mongoc_cursor_t *results =    mongoc_collection_aggregate(persons, MONGOC_QUERY_NONE, pipeline, NULL, NULL);bson_destroy(pipeline);

Ensure that you clean up the collection resources by adding the following line to your cleanup statements:

mongoc_collection_destroy(persons);

Finally, run the following commands in your shell to generate and run the executable:

gcc -o aggc agg-tutorial.c $(pkg-config --libs --cflags libmongoc-1.0)./aggc
Tip

If you encounter connection errors by running the preceding commands in one call, you can run them separately.

The aggregated result contains three documents. The documents represent the three youngest people with the vocation of "ENGINEER", ordered from youngest to oldest. The results omit the _id and address fields.

{ "person_id" : "7363626383", "firstname" : "Carl", "lastname" : "Simmons", "dateofbirth" : { "$date" : { "$numberLong" : "915250045000" } }, "vocation" : "ENGINEER" }{ "person_id" : "6392529400", "firstname" : "Elise", "lastname" : "Smith", "dateofbirth" : { "$date" : { "$numberLong" : "653616727000" } }, "vocation" : "ENGINEER" }{ "person_id" : "1723338115", "firstname" : "Olive", "lastname" : "Ranieri", "dateofbirth" : { "$date" : { "$numberLong" : "485113500000" } }, "gender" : "FEMALE", "vocation" : "ENGINEER" }

First, add a $match stage that finds documents in which the value of the vocation field is "ENGINEER":

pipeline.match(bsoncxx::from_json(R"({ "vocation": "ENGINEER"})"));

Next, add a $sort stage that sorts the documents in descending order by the dateofbirth field to list the youngest people first:

pipeline.sort(bsoncxx::from_json(R"({ "dateofbirth": -1})"));

Next, add a $limit stage to the pipeline to output only the first three documents in the results.

Finally, add an $unset stage. The $unset stage removes unnecessary fields from the result documents:

pipeline.append_stage(bsoncxx::from_json(R"({ "$unset": ["_id", "address"]})"));
Tip

Use the $unset operator instead of $project to avoid modifying the aggregation pipeline if documents with different fields are added to the collection.

Add the following code to the end of your application to perform the aggregation on the persons collection:

auto cursor = persons.aggregate(pipeline);

Finally, run the following commands in your shell to start your application:

c++ --std=c++17 agg-tutorial.cpp $(pkg-config --cflags --libs libmongocxx) -o ./app.out./app.out

The aggregated result contains three documents. The documents represent the three youngest people with the vocation of "ENGINEER", ordered from youngest to oldest. The results omit the _id and address fields.

{ "person_id" : "7363626383", "firstname" : "Carl", "lastname" : "Simmons", "dateofbirth" : { "$date" : "1999-01-01T00:00:35Z" }, "vocation" : "ENGINEER" }{ "person_id" : "1723338115", "firstname" : "Olive", "lastname" : "Ranieri", "dateofbirth" : { "$date" : "1985-05-21T13:14:30Z" }, "gender" : "FEMALE", "vocation" : "ENGINEER" }{ "person_id" : "6392529400", "firstname" : "Elise", "lastname" : "Smith", "dateofbirth" : { "$date" : "1970-01-08T04:29:07.927Z" }, "vocation" : "ENGINEER" }

First, start the aggregation on the persons collection and chain a $match stage that finds documents in which the value of the Vocation field is "ENGINEER":

var results = persons.Aggregate()    .Match(p => p.Vocation == "ENGINEER")

Next, add a $sort stage that sorts the documents in descending order by the DateOfBirth field to list the youngest people first:

.Sort(Builders<Person>.Sort.Descending(p => p.DateOfBirth))

Next, add a $limit stage to the pipeline to output only the first three documents in the results.

Finally, add a $project stage. The $project stage excludes unnecessary fields from the result documents:

.Project(Builders<Person>.Projection    .Exclude(p => p.Address)    .Exclude(p => p.Id));

Finally, run the application in your IDE and inspect the results.

The aggregated result contains three documents. The documents represent the three youngest people with the vocation of "ENGINEER", ordered from youngest to oldest. The results omit the _id and Address fields.

{ "PersonId" : "7363626383", "FirstName" : "Carl", "LastName" : "Simmons", "DateOfBirth" : { "$date" : "1998-12-26T13:13:55Z" }, "Vocation" : "ENGINEER" }{ "PersonId" : "1723338115", "FirstName" : "Olive", "LastName" : "Ranieri", "DateOfBirth" : { "$date" : "1985-05-12T23:14:30Z" }, "Gender" : "FEMALE", "Vocation" : "ENGINEER" }{ "PersonId" : "6392529400", "FirstName" : "Elise", "LastName" : "Smith", "DateOfBirth" : { "$date" : "1972-01-13T09:32:07Z" }, "Vocation" : "ENGINEER" }

First, add a $match stage that finds documents in which the value of the vocation field is "ENGINEER":

matchStage := bson.D{{Key: "$match", Value: bson.D{{Key: "vocation", Value: "ENGINEER"}}}}

Next, add a $sort stage that sorts the documents in descending order by the dateofbirth field to list the youngest people first:

sortStage := bson.D{{Key: "$sort", Value: bson.D{{Key: "dateofbirth", Value: -1}}}}

Next, add a $limit stage to the pipeline to output only the first three documents in the results.

limitStage := bson.D{{Key: "$limit", Value: 3}}

Finally, add an $unset stage. The $unset stage removes unnecessary fields from the result documents:

unsetStage := bson.D{{Key: "$unset", Value: bson.A{"_id", "address"}}}
Tip

Use the $unset operator instead of $project to avoid modifying the aggregation pipeline if documents with different fields are added to the collection.

Add the following code to the end of your application to perform the aggregation on the persons collection:

pipeline := mongo.Pipeline{matchStage, sortStage, limitStage, unsetStage}cursor, err := persons.Aggregate(context.TODO(), pipeline)

Finally, run the following command in your shell to start your application:

The aggregated result contains three documents. The documents represent the three youngest people with the vocation of "ENGINEER", ordered from youngest to oldest. The results omit the _id and address fields.

{"person_id":"7363626383","firstname":"Carl","lastname":"Simmons","dateofbirth":{"$date":"1998-12-26T13:13:55Z"},"vocation":"ENGINEER"}{"person_id":"1723338115","firstname":"Olive","lastname":"Ranieri","gender":"FEMALE","dateofbirth":{"$date":"1985-05-12T23:14:30Z"},"vocation":"ENGINEER"}{"person_id":"6392529400","firstname":"Elise","lastname":"Smith","dateofbirth":{"$date":"1972-01-13T09:32:07Z"},"vocation":"ENGINEER"}

First, add a $match stage that finds documents in which the value of the vocation field is "ENGINEER":

pipeline.add(Aggregates.match(Filters.eq("vocation", "ENGINEER")));

Next, add a $sort stage that sorts the documents in descending order by the dateofbirth field to list the youngest people first:

pipeline.add(Aggregates.sort(Sorts.descending("dateofbirth")));

Next, add a $limit stage to the pipeline to output only the first three documents in the results.

pipeline.add(Aggregates.limit(3));

Finally, add an $unset stage. The $unset stage removes unnecessary fields from the result documents:

pipeline.add(Aggregates.unset("_id", "address"));
Tip

Use the $unset operator instead of $project to avoid modifying the aggregation pipeline if documents with different fields are added to the collection.

Add the following code to the end of your application to perform the aggregation on the persons collection:

AggregateIterable<Document> aggregationResult = persons.aggregate(pipeline);

Finally, run the application in your IDE.

The aggregated result contains three documents. The documents represent the three youngest people with the vocation of "ENGINEER", ordered from youngest to oldest. The results omit the _id and address fields.

{"person_id": "7363626383", "firstname": "Carl", "lastname": "Simmons", "dateofbirth": {"$date": "1998-12-26T13:13:55Z"}, "vocation": "ENGINEER"}{"person_id": "1723338115", "firstname": "Olive", "lastname": "Ranieri", "dateofbirth": {"$date": "1985-05-12T23:14:30Z"}, "gender": "FEMALE", "vocation": "ENGINEER"}{"person_id": "6392529400", "firstname": "Elise", "lastname": "Smith", "dateofbirth": {"$date": "1972-01-13T09:32:07Z"}, "vocation": "ENGINEER"}

First, add a $match stage that finds documents in which the value of the vocation field is "ENGINEER":

pipeline.add(Aggregates.match(Filters.eq(Person::vocation.name, "ENGINEER")))

Next, add a $sort stage that sorts the documents in descending order by the dateOfBirth field to list the youngest people first:

pipeline.add(Aggregates.sort(Sorts.descending(Person::dateOfBirth.name)))

Next, add a $limit stage to the pipeline to output only the first three documents in the results.

pipeline.add(Aggregates.limit(3))

Finally, add an $unset stage. The $unset stage removes unnecessary fields from the result documents:

pipeline.add(Aggregates.unset("_id", Person::address.name))
Tip

Use the $unset operator instead of $project to avoid modifying the aggregation pipeline if documents with different fields are added to the collection.

Add the following code to the end of your application to perform the aggregation on the persons collection:

val aggregationResult = persons.aggregate<Document>(pipeline)

Finally, run the application in your IDE.

The aggregated result contains three documents. The documents represent the three youngest people with the vocation of "ENGINEER", ordered from youngest to oldest. The results omit the _id and address fields.

Document{{personID=7363626383, firstname=Carl, lastname=Simmons, dateOfBirth=Sat Dec 26 08:13:55 EST 1998, vocation=ENGINEER}}Document{{personID=1723338115, firstname=Olive, lastname=Ranieri, dateOfBirth=Sun May 12 19:14:30 EDT 1985, vocation=ENGINEER, gender=FEMALE}}Document{{personID=6392529400, firstname=Elise, lastname=Smith, dateOfBirth=Thu Jan 13 04:32:07 EST 1972, vocation=ENGINEER}}

First, add a $match stage that finds documents in which the value of the vocation field is "ENGINEER":

pipeline.push({  $match: {    vocation: 'ENGINEER',  },});

Next, add a $sort stage that sorts the documents in descending order by the dateofbirth field to list the youngest people first:

pipeline.push({  $sort: {    dateofbirth: -1,  },});

Next, add a $limit stage to the pipeline to output only the first three documents in the results.

pipeline.push({  $limit: 3,});

Finally, add an $unset stage. The $unset stage removes unnecessary fields from the result documents:

pipeline.push({  $unset: ['_id', 'address'],});
Tip

Use the $unset operator instead of $project to avoid modifying the aggregation pipeline if documents with different fields are added to the collection.

Add the following code to the end of your application to perform the aggregation on the persons collection:

const aggregationResult = await persons.aggregate(pipeline);

Finally, execute the code in the file using your IDE or the command line.

The aggregated result contains three documents. The documents represent the three youngest people with the vocation of "ENGINEER", ordered from youngest to oldest. The results omit the _id and address fields.

{  person_id: '7363626383',  firstname: 'Carl',  lastname: 'Simmons',  dateofbirth: 1998-12-26T13:13:55.000Z,  vocation: 'ENGINEER'}{  person_id: '1723338115',  firstname: 'Olive',  lastname: 'Ranieri',  dateofbirth: 1985-05-12T23:14:30.000Z,  gender: 'FEMALE',  vocation: 'ENGINEER'}{  person_id: '6392529400',  firstname: 'Elise',  lastname: 'Smith',  dateofbirth: 1972-01-13T09:32:07.000Z,  vocation: 'ENGINEER'}

In your Pipeline instance, create a $match stage that finds documents in which the value of the vocation field is "ENGINEER":

Stage::match(vocation: 'ENGINEER'),

Next, create a $sort stage that sorts the documents in descending order by the dateofbirth field to list the youngest people first:

Stage::sort(dateofbirth: Sort::Desc),

Next, create a $limit stage to the pipeline to output only the first three documents in the results.

Finally, create an $unset stage. The $unset stage removes unnecessary fields from the result documents:

Stage::unset('_id', 'address')
Tip

Use the $unset operator instead of $project to avoid modifying the aggregation pipeline if documents with different fields are added to the collection.

Add the following code to the end of your application to perform the aggregation on the persons collection:

$cursor = $persons->aggregate($pipeline);

Finally, run the following command in your shell to start your application:

The aggregated result contains three documents. The documents represent the three youngest people with the vocation of "ENGINEER", ordered from youngest to oldest. The results omit the _id and address fields.

{    "person_id": "7363626383",    "firstname": "Carl",    "lastname": "Simmons",    "dateofbirth": {        "$date": {            "$numberLong": "914678035000"        }    },    "vocation": "ENGINEER"}{    "person_id": "1723338115",    "firstname": "Olive",    "lastname": "Ranieri",    "gender": "FEMALE",    "dateofbirth": {        "$date": {            "$numberLong": "484787670000"        }    },    "vocation": "ENGINEER"}{    "person_id": "6392529400",    "firstname": "Elise",    "lastname": "Smith",    "dateofbirth": {        "$date": {            "$numberLong": "64143127000"        }    },    "vocation": "ENGINEER"}

In your Pipeline instance, create a $match stage that finds documents in which the value of the vocation field is "ENGINEER":

pipeline.append({"$match": {"vocation": "ENGINEER"}})

Next, create a $sort stage that sorts the documents in descending order by the dateofbirth field to list the youngest people first:

pipeline.append({"$sort": {"dateofbirth": -1}})

Next, create a $limit stage to the pipeline to output only the first three documents in the results.

pipeline.append({"$limit": 3})

Finally, create an $unset stage. The $unset stage removes unnecessary fields from the result documents:

pipeline.append({"$unset": ["_id", "address"]})
Tip

Use the $unset operator instead of $project to avoid modifying the aggregation pipeline if documents with different fields are added to the collection.

Add the following code to the end of your application to perform the aggregation on the persons collection:

aggregation_result = person_coll.aggregate(pipeline)

Finally, run the following command in your shell to start your application:

The aggregated result contains three documents. The documents represent the three youngest people with the vocation of "ENGINEER", ordered from youngest to oldest. The results omit the _id and address fields.

{'person_id': '7363626383', 'firstname': 'Carl', 'lastname': 'Simmons', 'dateofbirth': datetime.datetime(1998, 12, 26, 13, 13, 55), 'vocation': 'ENGINEER'}{'person_id': '1723338115', 'firstname': 'Olive', 'lastname': 'Ranieri', 'dateofbirth': datetime.datetime(1985, 5, 12, 23, 14, 30), 'gender': 'FEMALE', 'vocation': 'ENGINEER'}{'person_id': '6392529400', 'firstname': 'Elise', 'lastname': 'Smith', 'dateofbirth': datetime.datetime(1972, 1, 13, 9, 32, 7), 'vocation': 'ENGINEER'}

First, add a $match stage that finds documents in which the value of the vocation field is "ENGINEER":

{ "$match": { "vocation": "ENGINEER" } },

Next, add a $sort stage that sorts the documents in descending order by the dateofbirth field to list the youngest people first:

{ "$sort": { "dateofbirth": -1 } },

Next, add a $limit stage to the pipeline to output only the first three documents in the results.

Finally, add an $unset stage. The $unset stage removes unnecessary fields from the result documents:

{ "$unset": ["_id", "address"] },
Tip

Use the $unset operator instead of $project to avoid modifying the aggregation pipeline if documents with different fields are added to the collection.

Add the following code to the end of your application to perform the aggregation on the persons collection:

aggregation_result = persons.aggregate(pipeline)

Finally, run the following command in your shell to start your application:

The aggregated result contains three documents. The documents represent the three youngest people with the vocation of "ENGINEER", ordered from youngest to oldest. The results omit the _id and address fields.

{"person_id"=>"7363626383", "firstname"=>"Carl", "lastname"=>"Simmons", "dateofbirth"=>1998-12-26 13:13:55 UTC, "vocation"=>"ENGINEER"}{"person_id"=>"1723338115", "firstname"=>"Olive", "lastname"=>"Ranieri", "dateofbirth"=>1985-05-12 23:14:30 UTC, "gender"=>"FEMALE", "vocation"=>"ENGINEER"}{"person_id"=>"6392529400", "firstname"=>"Elise", "lastname"=>"Smith", "dateofbirth"=>1972-01-13 09:32:07 UTC, "vocation"=>"ENGINEER"}

First, add a $match stage that finds documents in which the value of the vocation field is "ENGINEER":

pipeline.push(doc! { "$match": { "vocation": "ENGINEER" } });

Next, add a $sort stage that sorts the documents in descending order by the dateofbirth field to list the youngest people first:

pipeline.push(doc! { "$sort": { "dateofbirth": -1 } });

Next, add a $limit stage to the pipeline to output only the first three documents in the results.

pipeline.push(doc! { "$limit": 3 });

Finally, add an $unset stage. The $unset stage removes unnecessary fields from the result documents:

pipeline.push(doc! { "$unset": ["_id", "address"] });
Tip

Use the $unset operator instead of $project to avoid modifying the aggregation pipeline if documents with different fields are added to the collection.

Add the following code to the end of your application to perform the aggregation on the persons collection:

let mut cursor = persons.aggregate(pipeline).await?;

Finally, run the following command in your shell to start your application:

The aggregated result contains three documents. The documents represent the three youngest people with the vocation of "ENGINEER", ordered from youngest to oldest. The results omit the _id and address fields.

Document({"person_id": String("7363626383"), "firstname": String("Carl"), "lastname": String("Simmons"), "dateofbirth": DateTime(1998-12-26 13:13:55.0 +00:00:00), "vocation": String("ENGINEER")})Document({"person_id": String("1723338115"), "firstname": String("Olive"), "lastname": String("Ranieri"), "gender": String("FEMALE"), "dateofbirth": DateTime(1985-05-12 23:14:30.0 +00:00:00), "vocation": String("ENGINEER")})Document({"person_id": String("6392529400"), "firstname": String("Elise"), "lastname": String("Smith"), "dateofbirth": DateTime(1972-01-13 9:32:07.0 +00:00:00), "vocation": String("ENGINEER")})

First, add a $match stage that finds documents in which the value of the vocation field is "ENGINEER":

Aggregates.filter(Filters.equal("vocation", "ENGINEER")),

Next, add a $sort stage that sorts the documents in descending order by the dateofbirth field to list the youngest people first:

Aggregates.sort(Sorts.descending("dateofbirth")),

Next, add a $limit stage to the pipeline to output only the first three documents in the results.

Finally, add an $unset stage. The $unset stage removes unnecessary fields from the result documents:

Aggregates.unset("_id", "address")
Tip

Use the $unset operator instead of $project to avoid modifying the aggregation pipeline if documents with different fields are added to the collection.

Add the following code to the end of your application to perform the aggregation on the persons collection:

persons.aggregate(pipeline)  .subscribe((doc: Document) => println(doc.toJson()),    (e: Throwable) => println(s"Error: $e"))

Finally, run the application in your IDE.

The aggregated result contains three documents. The documents represent the three youngest people with the vocation of "ENGINEER", ordered from youngest to oldest. The results omit the _id and address fields.

{"person_id": "7363626383", "firstname": "Carl", "lastname": "Simmons", "dateofbirth": {"$date": "1998-12-26T18:13:55Z"}, "vocation": "ENGINEER"}{"person_id": "1723338115", "firstname": "Olive", "lastname": "Ranieri", "dateofbirth": {"$date": "1985-05-13T03:14:30Z"}, "gender": "FEMALE", "vocation": "ENGINEER"}{"person_id": "6392529400", "firstname": "Elise", "lastname": "Smith", "dateofbirth": {"$date": "1972-01-13T14:32:07Z"}, "vocation": "ENGINEER"}

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4