Skip to main content

MongoProjections

Definition of the projections in Bson that are used in MongoDB collection queries to select specific fields.


computed


computed(fieldName: string, expression: org.bson.conversions.Bson) : org.bson.conversions.Bson

Description

Creates a projection that adds a computed field to the result.

How To Use
_mongo.projections().computed('fullName', expression);
Attributes
NAMETYPEDESCRIPTION
fieldNamestringThe output field name.
expressionorg.bson.conversions.BsonThe computed field expression.
Return

( org.bson.conversions.Bson )

The projection.


elemMatch


elemMatch(fieldName: string) : org.bson.conversions.Bson

Description

Creates a projection that includes for the given field only the first element of an array that matches the query filter.

How To Use
const docs = collection.find(_mongo.filters().gt('array', 7)).projection(_mongo.projections().elemMatch('array')).all();
Attributes
NAMETYPEDESCRIPTION
fieldNamestringThe field name whose value is the array.
Return

( org.bson.conversions.Bson )

The projection.


elemMatch(fieldName: string, filter: org.bson.conversions.Bson) : org.bson.conversions.Bson

Description

Creates a projection that includes for the given field only the first element of the array value of that field that matches the given query filter.

How To Use
const docs = collection.find().projection(
_mongo.projections().elemMatch(
"orders",
_mongo.filters().eq("status", "pending")
)
).all();

Attributes
NAMETYPEDESCRIPTION
fieldNamestringThe field name.
filterorg.bson.conversions.BsonThe filter to apply.
Return

( org.bson.conversions.Bson )

The projection.


exclude


exclude(fieldNames: string[]) : org.bson.conversions.Bson

Description

Creates a projection that excludes all of the given fields.

How To Use
const docs = collection.find().projection(_mongo.projections().exclude('internalField')).all();
Attributes
NAMETYPEDESCRIPTION
fieldNamesstring[]The field names.
Return

( org.bson.conversions.Bson )

The projection.


exclude(fieldNames: java.util.List<string>) : org.bson.conversions.Bson

Description

Creates a projection that excludes all fields from a list.

Attributes
NAMETYPEDESCRIPTION
fieldNamesjava.util.ListThe list of field names.
Return

( org.bson.conversions.Bson )

The projection.


excludeId


excludeId() : org.bson.conversions.Bson

Description

Creates a projection that excludes the _id field.

How To Use
const docs = collection.find().projection(_mongo.projections().excludeId()).all();
Return

( org.bson.conversions.Bson )

The projection.


fields


fields(projections: org.bson.conversions.Bson[]) : org.bson.conversions.Bson

Description

Creates a projection that combines the list of projections into a single one. If there are duplicate keys, the last one takes precedence.

How To Use
const docs = collection.find().projection( _mongo.projections().fields(
_mongo.projections().include("name", "quantity"),
_mongo.projections().excludeId()
)
).all();


Attributes
NAMETYPEDESCRIPTION
projectionsorg.bson.conversions.Bson[]The list of projections to combine.
Return

( org.bson.conversions.Bson )

The combined projection.


fields(projections: java.util.List<?>) : org.bson.conversions.Bson

Description

Creates a projection that combines the list of projections from a list.

Attributes
NAMETYPEDESCRIPTION
projectionsjava.util.ListThe list of projections to combine.
Return

( org.bson.conversions.Bson )

The combined projection.


include


include(fieldNames: string[]) : org.bson.conversions.Bson

Description

Creates a projection that includes all of the given fields.

How To Use
const docs = collection.find().projection(_mongo.projections().include('name', 'quantity')).all();
Attributes
NAMETYPEDESCRIPTION
fieldNamesstring[]The field names.
Return

( org.bson.conversions.Bson )

The projection.


include(fieldNames: java.util.List<string>) : org.bson.conversions.Bson

Description

Creates a projection that includes all fields from a list.

Attributes
NAMETYPEDESCRIPTION
fieldNamesjava.util.ListThe list of field names.
Return

( org.bson.conversions.Bson )

The projection.


slice


slice(fieldName: string, limit: int) : org.bson.conversions.Bson

Description

Creates a projection that returns only the first N elements of an array.

How To Use
const docs = collection.find().projection(_mongo.projections().slice('tags', 5)).all();
Attributes
NAMETYPEDESCRIPTION
fieldNamestringThe field name whose value is the array.
limitintThe number of elements to return.
Return

( org.bson.conversions.Bson )

The projection.


slice(fieldName: string, skip: int, limit: int) : org.bson.conversions.Bson

Description

Creates a projection that skips N elements and returns the next M elements of an array.

How To Use
const docs = collection.find().projection(_mongo.projections().slice('tags', 2, 5)).all();
Attributes
NAMETYPEDESCRIPTION
fieldNamestringThe field name whose value is the array.
skipintThe number of elements to skip.
limitintThe number of elements to return.
Return

( org.bson.conversions.Bson )

The projection.