All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.mongodb.client.model.Projections Maven / Gradle / Ivy

Go to download

The MongoDB Java Driver uber-artifact, containing mongodb-driver, mongodb-driver-core, and bson

The newest version!
/*
 * Copyright 2015 MongoDB, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONObjectITIONS OF ANY KINObject, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.mongodb.client.model;

import org.bson.BsonArray;
import org.bson.BsonDocument;
import org.bson.BsonInt32;
import org.bson.BsonString;
import org.bson.BsonValue;
import org.bson.codecs.configuration.CodecRegistry;
import org.bson.conversions.Bson;

import java.util.List;

import static com.mongodb.assertions.Assertions.notNull;
import static java.util.Arrays.asList;

/**
 * A factory for projections.   A convenient way to use this class is to statically import all of its methods, which allows usage like:
 *
 * 
 *    collection.find().projection(fields(include("x", "y"), excludeId()))
 * 
* * @mongodb.driver.manual tutorial/project-fields-from-query-results/#limit-fields-to-return-from-a-query Projection * @since 3.0 */ public final class Projections { private Projections() { } /** * Creates a projection of a field whose value is computed from the given expression. Projection with an expression is only supported * using the $project aggregation pipeline stage. * * @param fieldName the field name * @param expression the expression * @param the expression type * @return the projection * @see Aggregates#project(Bson) */ public static Bson computed(final String fieldName, final TExpression expression) { return new SimpleExpression(fieldName, expression); } /** * Creates a projection that includes all of the given fields. * * @param fieldNames the field names * @return the projection */ public static Bson include(final String... fieldNames) { return include(asList(fieldNames)); } /** * Creates a projection that includes all of the given fields. * * @param fieldNames the field names * @return the projection */ public static Bson include(final List fieldNames) { return combine(fieldNames, new BsonInt32(1)); } /** * Creates a projection that excludes all of the given fields. * * @param fieldNames the field names * @return the projection */ public static Bson exclude(final String... fieldNames) { return exclude(asList(fieldNames)); } /** * Creates a projection that excludes all of the given fields. * * @param fieldNames the field names * @return the projection */ public static Bson exclude(final List fieldNames) { return combine(fieldNames, new BsonInt32(0)); } /** * Creates a projection that excludes the _id field. This suppresses the automatic inclusion of _id that is the default, even when * other fields are explicitly included. * * @return the projection */ public static Bson excludeId() { return new BsonDocument("_id", new BsonInt32(0)); } /** * Creates a projection that includes for the given field only the first element of an array that matches the query filter. This is * referred to as the positional $ operator. * * @param fieldName the field name whose value is the array * @return the projection * @mongodb.driver.manual reference/operator/projection/positional/#projection Project the first matching element ($ operator) */ public static Bson elemMatch(final String fieldName) { return new BsonDocument(fieldName + ".$", new BsonInt32(1)); } /** * 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. * * @param fieldName the field name * @param filter the filter to apply * @return the projection * @mongodb.driver.manual reference/operator/projection/elemMatch elemMatch */ public static Bson elemMatch(final String fieldName, final Bson filter) { return new Bson() { @Override public BsonDocument toBsonDocument(final Class documentClass, final CodecRegistry codecRegistry) { return new BsonDocument(fieldName, new BsonDocument("$elemMatch", filter.toBsonDocument(documentClass, codecRegistry))); } }; } /** * Creates a projection to the given field name of the textScore, for use with text queries. * * @param fieldName the field name * @return the projection * @mongodb.driver.manual reference/operator/projection/meta/#projection textScore */ public static Bson metaTextScore(final String fieldName) { return new BsonDocument(fieldName, new BsonDocument("$meta", new BsonString("textScore"))); } /** * Creates a projection to the given field name of a slice of the array value of that field. * * @param fieldName the field name * @param limit the number of elements to project. * @return the projection * @mongodb.driver.manual reference/operator/projection/slice Slice */ public static Bson slice(final String fieldName, final int limit) { return new BsonDocument(fieldName, new BsonDocument("$slice", new BsonInt32(limit))); } /** * Creates a projection to the given field name of a slice of the array value of that field. * * @param fieldName the field name * @param skip the number of elements to skip before applying the limit * @param limit the number of elements to project * @return the projection * @mongodb.driver.manual reference/operator/projection/slice Slice */ public static Bson slice(final String fieldName, final int skip, final int limit) { return new BsonDocument(fieldName, new BsonDocument("$slice", new BsonArray(asList(new BsonInt32(skip), new BsonInt32(limit))))); } /** * Creates a projection that combines the list of projections into a single one. If there are duplicate keys, the last one takes * precedence. * * @param projections the list of projections to combine * @return the combined projection */ public static Bson fields(final Bson... projections) { return fields(asList(projections)); } /** * Creates a projection that combines the list of projections into a single one. If there are duplicate keys, the last one takes * precedence. * * @param projections the list of projections to combine * @return the combined projection * @mongodb.driver.manual */ public static Bson fields(final List projections) { notNull("sorts", projections); return new Bson() { @Override public BsonDocument toBsonDocument(final Class documentClass, final CodecRegistry codecRegistry) { BsonDocument combinedDocument = new BsonDocument(); for (Bson sort : projections) { BsonDocument sortDocument = sort.toBsonDocument(documentClass, codecRegistry); for (String key : sortDocument.keySet()) { combinedDocument.remove(key); combinedDocument.append(key, sortDocument.get(key)); } } return combinedDocument; } }; } private static Bson combine(final List fieldNames, final BsonValue value) { BsonDocument document = new BsonDocument(); for (String fieldName : fieldNames) { document.remove(fieldName); document.append(fieldName, value); } return document; } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy