/* Copyright 2010-present 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 CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using MongoDB.Bson;
using MongoDB.Bson.IO;
using MongoDB.Bson.Serialization;
using MongoDB.Driver.Core.Misc;
namespace MongoDB.Driver
{
///
/// Extension methods for UpdateDefinition.
///
public static class UpdateDefinitionExtensions
{
///
/// Combines an existing update with an add to set operator.
///
/// The type of the document.
/// The type of the item.
/// The update.
/// The field.
/// The value.
///
/// A combined update.
///
public static UpdateDefinition AddToSet(this UpdateDefinition update, FieldDefinition field, TItem value)
{
var builder = Builders.Update;
return builder.Combine(update, builder.AddToSet(field, value));
}
///
/// Combines an existing update with an add to set operator.
///
/// The type of the document.
/// The type of the item.
/// The update.
/// The field.
/// The value.
///
/// A combined update.
///
public static UpdateDefinition AddToSet(this UpdateDefinition update, Expression>> field, TItem value)
{
var builder = Builders.Update;
return builder.Combine(update, builder.AddToSet(field, value));
}
///
/// Combines an existing update with an add to set operator.
///
/// The type of the document.
/// The type of the item.
/// The update.
/// The field.
/// The values.
///
/// A combined update.
///
public static UpdateDefinition AddToSetEach(this UpdateDefinition update, FieldDefinition field, IEnumerable values)
{
var builder = Builders.Update;
return builder.Combine(update, builder.AddToSetEach(field, values));
}
///
/// Combines an existing update with an add to set operator.
///
/// The type of the document.
/// The type of the item.
/// The update.
/// The field.
/// The values.
///
/// A combined update.
///
public static UpdateDefinition AddToSetEach(this UpdateDefinition update, Expression>> field, IEnumerable values)
{
var builder = Builders.Update;
return builder.Combine(update, builder.AddToSetEach(field, values));
}
///
/// Combines an existing update with a bitwise and operator.
///
/// The type of the document.
/// The type of the field.
/// The update.
/// The field.
/// The value.
///
/// A combined update.
///
public static UpdateDefinition BitwiseAnd(this UpdateDefinition update, FieldDefinition field, TField value)
{
var builder = Builders.Update;
return builder.Combine(update, builder.BitwiseAnd(field, value));
}
///
/// Combines an existing update with a bitwise and operator.
///
/// The type of the document.
/// The type of the field.
/// The update.
/// The field.
/// The value.
///
/// A combined update.
///
public static UpdateDefinition BitwiseAnd(this UpdateDefinition update, Expression> field, TField value)
{
var builder = Builders.Update;
return builder.Combine(update, builder.BitwiseAnd(field, value));
}
///
/// Combines an existing update with a bitwise or operator.
///
/// The type of the document.
/// The type of the field.
/// The update.
/// The field.
/// The value.
///
/// A combined update.
///
public static UpdateDefinition BitwiseOr(this UpdateDefinition update, FieldDefinition field, TField value)
{
var builder = Builders.Update;
return builder.Combine(update, builder.BitwiseOr(field, value));
}
///
/// Combines an existing update with a bitwise or operator.
///
/// The type of the document.
/// The type of the field.
/// The update.
/// The field.
/// The value.
///
/// A combined update.
///
public static UpdateDefinition BitwiseOr(this UpdateDefinition update, Expression> field, TField value)
{
var builder = Builders.Update;
return builder.Combine(update, builder.BitwiseOr(field, value));
}
///
/// Combines an existing update with a bitwise xor operator.
///
/// The type of the document.
/// The type of the field.
/// The update.
/// The field.
/// The value.
///
/// A combined update.
///
public static UpdateDefinition BitwiseXor(this UpdateDefinition update, FieldDefinition field, TField value)
{
var builder = Builders.Update;
return builder.Combine(update, builder.BitwiseXor(field, value));
}
///
/// Combines an existing update with a bitwise xor operator.
///
/// The type of the document.
/// The type of the field.
/// The update.
/// The field.
/// The value.
///
/// A combined update.
///
public static UpdateDefinition BitwiseXor(this UpdateDefinition update, Expression> field, TField value)
{
var builder = Builders.Update;
return builder.Combine(update, builder.BitwiseXor(field, value));
}
///
/// Combines an existing update with a current date operator.
///
/// The type of the document.
/// The update.
/// The field.
/// The type.
///
/// A combined update.
///
public static UpdateDefinition CurrentDate(this UpdateDefinition update, FieldDefinition field, UpdateDefinitionCurrentDateType? type = null)
{
var builder = Builders.Update;
return builder.Combine(update, builder.CurrentDate(field, type));
}
///
/// Combines an existing update with a current date operator.
///
/// The type of the document.
/// The update.
/// The field.
/// The type.
///
/// A combined update.
///
public static UpdateDefinition CurrentDate(this UpdateDefinition update, Expression> field, UpdateDefinitionCurrentDateType? type = null)
{
var builder = Builders.Update;
return builder.Combine(update, builder.CurrentDate(field, type));
}
///
/// Combines an existing update with an increment operator.
///
/// The type of the document.
/// The type of the field.
/// The update.
/// The field.
/// The value.
///
/// A combined update.
///
public static UpdateDefinition Inc(this UpdateDefinition update, FieldDefinition field, TField value)
{
var builder = Builders.Update;
return builder.Combine(update, builder.Inc(field, value));
}
///
/// Combines an existing update with an increment operator.
///
/// The type of the document.
/// The type of the field.
/// The update.
/// The field.
/// The value.
///
/// A combined update.
///
public static UpdateDefinition Inc(this UpdateDefinition update, Expression> field, TField value)
{
var builder = Builders.Update;
return builder.Combine(update, builder.Inc(field, value));
}
///
/// Combines an existing update with a max operator.
///
/// The type of the document.
/// The type of the field.
/// The update.
/// The field.
/// The value.
///
/// A combined update.
///
public static UpdateDefinition Max(this UpdateDefinition update, FieldDefinition field, TField value)
{
var builder = Builders.Update;
return builder.Combine(update, builder.Max(field, value));
}
///
/// Combines an existing update with a max operator.
///
/// The type of the document.
/// The type of the field.
/// The update.
/// The field.
/// The value.
///
/// A combined update.
///
public static UpdateDefinition Max(this UpdateDefinition update, Expression> field, TField value)
{
var builder = Builders.Update;
return builder.Combine(update, builder.Max(field, value));
}
///
/// Combines an existing update with a min operator.
///
/// The type of the document.
/// The type of the field.
/// The update.
/// The field.
/// The value.
///
/// A combined update.
///
public static UpdateDefinition Min(this UpdateDefinition update, FieldDefinition field, TField value)
{
var builder = Builders.Update;
return builder.Combine(update, builder.Min(field, value));
}
///
/// Combines an existing update with a min operator.
///
/// The type of the document.
/// The type of the field.
/// The update.
/// The field.
/// The value.
///
/// A combined update.
///
public static UpdateDefinition Min(this UpdateDefinition update, Expression> field, TField value)
{
var builder = Builders.Update;
return builder.Combine(update, builder.Min(field, value));
}
///
/// Combines an existing update with a multiply operator.
///
/// The type of the document.
/// The type of the field.
/// The update.
/// The field.
/// The value.
///
/// A combined update.
///
public static UpdateDefinition Mul(this UpdateDefinition update, FieldDefinition field, TField value)
{
var builder = Builders.Update;
return builder.Combine(update, builder.Mul(field, value));
}
///
/// Combines an existing update with a multiply operator.
///
/// The type of the document.
/// The type of the field.
/// The update.
/// The field.
/// The value.
///
/// A combined update.
///
public static UpdateDefinition Mul(this UpdateDefinition update, Expression> field, TField value)
{
var builder = Builders.Update;
return builder.Combine(update, builder.Mul(field, value));
}
///
/// Combines an existing update with a pop operator.
///
/// The type of the document.
/// The update.
/// The field.
///
/// A combined update.
///
public static UpdateDefinition PopFirst(this UpdateDefinition update, FieldDefinition field)
{
var builder = Builders.Update;
return builder.Combine(update, builder.PopFirst(field));
}
///
/// Combines an existing update with a pop operator.
///
/// The type of the document.
/// The update.
/// The field.
///
/// A combined update.
///
public static UpdateDefinition PopFirst(this UpdateDefinition update, Expression> field)
{
var builder = Builders.Update;
return builder.Combine(update, builder.PopFirst(field));
}
///
/// Combines an existing update with a pop operator.
///
/// The type of the document.
/// The update.
/// The field.
///
/// A combined update.
///
public static UpdateDefinition PopLast(this UpdateDefinition update, FieldDefinition field)
{
var builder = Builders.Update;
return builder.Combine(update, builder.PopLast(field));
}
///
/// Combines an existing update with a pop operator.
///
/// The type of the document.
/// The update.
/// The field.
///
/// A combined update.
///
public static UpdateDefinition PopLast(this UpdateDefinition update, Expression> field)
{
var builder = Builders.Update;
return builder.Combine(update, builder.PopLast(field));
}
///
/// Combines an existing update with a pull operator.
///
/// The type of the document.
/// The type of the item.
/// The update.
/// The field.
/// The value.
///
/// A combined update.
///
public static UpdateDefinition Pull(this UpdateDefinition update, FieldDefinition field, TItem value)
{
var builder = Builders.Update;
return builder.Combine(update, builder.Pull(field, value));
}
///
/// Combines an existing update with a pull operator.
///
/// The type of the document.
/// The type of the item.
/// The update.
/// The field.
/// The value.
///
/// A combined update.
///
public static UpdateDefinition Pull(this UpdateDefinition update, Expression>> field, TItem value)
{
var builder = Builders.Update;
return builder.Combine(update, builder.Pull(field, value));
}
///
/// Combines an existing update with a pull operator.
///
/// The type of the document.
/// The type of the item.
/// The update.
/// The field.
/// The values.
///
/// A combined update.
///
public static UpdateDefinition PullAll(this UpdateDefinition update, FieldDefinition field, IEnumerable values)
{
var builder = Builders.Update;
return builder.Combine(update, builder.PullAll(field, values));
}
///
/// Combines an existing update with a pull operator.
///
/// The type of the document.
/// The type of the item.
/// The update.
/// The field.
/// The values.
///
/// A combined update.
///
public static UpdateDefinition PullAll(this UpdateDefinition update, Expression>> field, IEnumerable values)
{
var builder = Builders.Update;
return builder.Combine(update, builder.PullAll(field, values));
}
///
/// Combines an existing update with a pull operator.
///
/// The type of the document.
/// The type of the item.
/// The update.
/// The field.
/// The filter.
///
/// A combined update.
///
public static UpdateDefinition PullFilter(this UpdateDefinition update, FieldDefinition field, FilterDefinition filter)
{
var builder = Builders.Update;
return builder.Combine(update, builder.PullFilter(field, filter));
}
///
/// Combines an existing update with a pull operator.
///
/// The type of the document.
/// The type of the item.
/// The update.
/// The field.
/// The filter.
///
/// A combined update.
///
public static UpdateDefinition PullFilter(this UpdateDefinition update, Expression>> field, FilterDefinition filter)
{
var builder = Builders.Update;
return builder.Combine(update, builder.PullFilter(field, filter));
}
///
/// Combines an existing update with a pull operator.
///
/// The type of the document.
/// The type of the item.
/// The update.
/// The field.
/// The filter.
///
/// A combined update.
///
public static UpdateDefinition PullFilter(this UpdateDefinition update, Expression>> field, Expression> filter)
{
var builder = Builders.Update;
return builder.Combine(update, builder.PullFilter(field, filter));
}
///
/// Combines an existing update with a push operator.
///
/// The type of the document.
/// The type of the item.
/// The update.
/// The field.
/// The value.
///
/// A combined update.
///
public static UpdateDefinition Push(this UpdateDefinition update, FieldDefinition field, TItem value)
{
var builder = Builders.Update;
return builder.Combine(update, builder.Push(field, value));
}
///
/// Combines an existing update with a push operator.
///
/// The type of the document.
/// The type of the item.
/// The update.
/// The field.
/// The value.
///
/// A combined update.
///
public static UpdateDefinition Push(this UpdateDefinition update, Expression>> field, TItem value)
{
var builder = Builders.Update;
return builder.Combine(update, builder.Push(field, value));
}
///
/// Combines an existing update with a push operator.
///
/// The type of the document.
/// The type of the item.
/// The update.
/// The field.
/// The values.
/// The slice.
/// The position.
/// The sort.
///
/// A combined update.
///
public static UpdateDefinition PushEach(this UpdateDefinition update, FieldDefinition field, IEnumerable values, int? slice = null, int? position = null, SortDefinition sort = null)
{
var builder = Builders.Update;
return builder.Combine(update, builder.PushEach(field, values, slice, position, sort));
}
///
/// Combines an existing update with a push operator.
///
/// The type of the document.
/// The type of the item.
/// The update.
/// The field.
/// The values.
/// The slice.
/// The position.
/// The sort.
///
/// A combined update.
///
public static UpdateDefinition PushEach(this UpdateDefinition update, Expression>> field, IEnumerable values, int? slice = null, int? position = null, SortDefinition sort = null)
{
var builder = Builders.Update;
return builder.Combine(update, builder.PushEach(field, values, slice, position, sort));
}
///
/// Combines an existing update with a field renaming operator.
///
/// The type of the document.
/// The update.
/// The field.
/// The new name.
///
/// A combined update.
///
public static UpdateDefinition Rename(this UpdateDefinition update, FieldDefinition field, string newName)
{
var builder = Builders.Update;
return builder.Combine(update, builder.Rename(field, newName));
}
///
/// Combines an existing update with a field renaming operator.
///
/// The type of the document.
/// The update.
/// The field.
/// The new name.
///
/// A combined update.
///
public static UpdateDefinition Rename(this UpdateDefinition update, Expression> field, string newName)
{
var builder = Builders.Update;
return builder.Combine(update, builder.Rename(field, newName));
}
///
/// Combines an existing update with a set operator.
///
/// The type of the document.
/// The type of the field.
/// The update.
/// The field.
/// The value.
///
/// A combined update.
///
public static UpdateDefinition Set(this UpdateDefinition update, FieldDefinition field, TField value)
{
var builder = Builders.Update;
return builder.Combine(update, builder.Set(field, value));
}
///
/// Combines an existing update with a set operator.
///
/// The type of the document.
/// The type of the field.
/// The update.
/// The field.
/// The value.
///
/// A combined update.
///
public static UpdateDefinition Set(this UpdateDefinition update, Expression> field, TField value)
{
var builder = Builders.Update;
return builder.Combine(update, builder.Set(field, value));
}
///
/// Combines an existing update with a set on insert operator.
///
/// The type of the document.
/// The type of the field.
/// The update.
/// The field.
/// The value.
///
/// A combined update.
///
public static UpdateDefinition SetOnInsert(this UpdateDefinition update, FieldDefinition field, TField value)
{
var builder = Builders.Update;
return builder.Combine(update, builder.SetOnInsert(field, value));
}
///
/// Combines an existing update with a set on insert operator.
///
/// The type of the document.
/// The type of the field.
/// The update.
/// The field.
/// The value.
///
/// A combined update.
///
public static UpdateDefinition SetOnInsert(this UpdateDefinition update, Expression> field, TField value)
{
var builder = Builders.Update;
return builder.Combine(update, builder.SetOnInsert(field, value));
}
///
/// Combines an existing update with an unset operator.
///
/// The type of the document.
/// The update.
/// The field.
///
/// A combined update.
///
public static UpdateDefinition Unset(this UpdateDefinition update, FieldDefinition field)
{
var builder = Builders.Update;
return builder.Combine(update, builder.Unset(field));
}
///
/// Combines an existing update with an unset operator.
///
/// The type of the document.
/// The update.
/// The field.
///
/// A combined update.
///
public static UpdateDefinition Unset(this UpdateDefinition update, Expression> field)
{
var builder = Builders.Update;
return builder.Combine(update, builder.Unset(field));
}
}
///
/// The type to use for a $currentDate operator.
///
public enum UpdateDefinitionCurrentDateType
{
///
/// A date.
///
Date,
///
/// A timestamp.
///
Timestamp
}
///
/// A builder for an .
///
/// The type of the document.
public sealed class UpdateDefinitionBuilder
{
///
/// Creates an add to set operator.
///
/// The type of the item.
/// The field.
/// The value.
/// An add to set operator.
public UpdateDefinition AddToSet(FieldDefinition field, TItem value)
{
return new AddToSetUpdateDefinition(
field,
new[] { value });
}
///
/// Creates an add to set operator.
///
/// The type of the item.
/// The field.
/// The value.
/// An add to set operator.
public UpdateDefinition AddToSet(Expression>> field, TItem value)
{
return AddToSet(new ExpressionFieldDefinition(field), value);
}
///
/// Creates an add to set operator.
///
/// The type of the item.
/// The field.
/// The values.
/// An add to set operator.
public UpdateDefinition AddToSetEach(FieldDefinition field, IEnumerable values)
{
return new AddToSetUpdateDefinition(field, values);
}
///
/// Creates an add to set operator.
///
/// The type of the item.
/// The field.
/// The values.
/// An add to set operator.
public UpdateDefinition AddToSetEach(Expression>> field, IEnumerable values)
{
return AddToSetEach(new ExpressionFieldDefinition(field), values);
}
///
/// Creates a bitwise and operator.
///
/// The type of the field.
/// The field.
/// The value.
/// A bitwise and operator.
public UpdateDefinition BitwiseAnd(FieldDefinition field, TField value)
{
return new BitwiseOperatorUpdateDefinition("and", field, value);
}
///