/** * Creates an update that adds each of the given values to the array value of the field with the given name. * * @param fieldName the non-null field name * @param values the values * @param <TItem> the value type * @return the update * @mongodb.driver.manual reference/operator/update/push/ $push */ public static <TItem> Bson pushEach(final String fieldName, final List<TItem> values) { return new PushUpdate<TItem>(fieldName, values, new PushOptions()); }
@Override protected <TDocument> void writeAdditionalFields(final BsonDocumentWriter writer, final Class<TDocument> tDocumentClass, final CodecRegistry codecRegistry) { Integer position = options.getPosition(); if (position != null) { writer.writeInt32("$position", position); } Integer slice = options.getSlice(); if (slice != null) { writer.writeInt32("$slice", slice); } Integer sort = options.getSort(); if (sort != null) { writer.writeInt32("$sort", sort); } else { Bson sortDocument = options.getSortDocument(); if (sortDocument != null) { writer.writeName("$sort"); encodeValue(writer, sortDocument, codecRegistry); } } }
@Override public <V> void addAllValues(ManyFeatureBean feature, List<? extends V> collection) { checkNotNull(feature, "feature"); checkNotNull(collection, "collection"); checkNotContainsNull(collection, "collection"); if (collection.isEmpty()) { return; } final String ownerId = idConverter.convert(feature.owner()); final String featureId = Integer.toString(feature.id()); final int size = sizeOfValue(feature.withoutPosition()).orElse(0); if (feature.position() > size) { throw new IndexOutOfBoundsException(); } List<String> newValues = collection.stream().map(this::serializeValue).collect(Collectors.toList()); final String fieldName = concat(ModelDocument.F_MANY_FEATURE, featureId); final Bson filter = eq(ModelDocument.F_ID, ownerId); final Bson update = pushEach(fieldName, newValues, new PushOptions().position(feature.position())); documents.updateOne(filter, update); }
@Override protected <TDocument> void writeAdditionalFields(final BsonDocumentWriter writer, final Class<TDocument> tDocumentClass, final CodecRegistry codecRegistry) { Integer position = options.getPosition(); if (position != null) { writer.writeInt32("$position", position); } Integer slice = options.getSlice(); if (slice != null) { writer.writeInt32("$slice", slice); } Integer sort = options.getSort(); if (sort != null) { writer.writeInt32("$sort", sort); } else { Bson sortDocument = options.getSortDocument(); if (sortDocument != null) { writer.writeName("$sort"); encodeValue(writer, sortDocument, codecRegistry); } } }
/** * Creates an update that adds each of the given values to the array value of the field with the given name. * * @param fieldName the non-null field name * @param values the values * @param <TItem> the value type * @return the update * @mongodb.driver.manual reference/operator/update/push/ $push */ public static <TItem> Bson pushEach(final String fieldName, final List<TItem> values) { return new PushUpdate<TItem>(fieldName, values, new PushOptions()); }