project in mongodb code example

Example 1: mongodb project

{ $project: { "contact.address.country": 1, contact: 1 } }

Example 2: mongodb project all fields

db.collection.aggregate([
    { "$addFields": { "custom_field": "$obj.obj_field1" } }
])

Example 3: project mongodb c#

int count = 1;
await collection.Find(FilterDefinition<Student>.Empty)
    .Project(x => new {x.FirstName, x.LastName})
    .ForEachAsync(
        student =>
        {
            Console.WriteLine($"{count}. \t FirstName: {student.FirstName} - LastName {student.LastName}");
            count++;
        });

Console.WriteLine();

Example 4: how to project specific field mongodb nodejs

db.inventory.find( { status: "A" }, { item: 1, status: 1 } )
//1 to show and 0 to hide
//_id always included need to be set to 0 if not needed

Tags:

Go Example