/// <summary> /// Writes the bytes for an array of values /// </summary> public static byte[] AsArray(IEnumerable<object> array) { array = array ?? new object[] { }; //simply create a Document with each index as //the value of the index of the item var result = new BsonDocument(); for(var i = 0; i < array.Count(); i++) { result.Set(i.ToString(), array.ElementAt(i)); } //then generate the bytes return result.ToBsonByteArray(); }
/// <summary> /// Removes all matching fields from each document in the query /// </summary> public void Unset(params string[] fields) { //mark the fields to be removed BsonDocument remove = new BsonDocument(); foreach (string field in fields) { remove.Set<int>(field, 1); } //send the command this._SendUpdate("$unset", UpdateOptionTypes.MultiUpdate, remove); }
/// <summary> /// Increments each of the fields by the number provides - first /// converting the value to an integer /// </summary> public void Increment(BsonDocument document) { //recast each to an integer value - I'm not sure //if any numeric type can be used in this instance foreach (var item in document.GetValues()) { document.Set<int>(item.Key, document.Get<int>(item.Key, 1)); } //send the update request this._SendUpdate("$inc", UpdateOptionTypes.MultiUpdate, document); }
/// <summary> /// Increments each of the fields provided by one /// </summary> public void Increment(params string[] fields) { //create the document BsonDocument document = new BsonDocument(); foreach (string field in fields) { document.Set<int>(field, 1); } //send the command this.Increment(document); }
/// <summary> /// Creates the body of the request to send /// </summary> protected override void GenerateBody(DynamicStream stream) { //determine the correct options to use stream.Append(BsonTranslator.AsInt32((int)this.Options)); //apply the collection and database stream.Append(BsonTranslator.AsString(this.GetDatabaseTarget())); //update the range information for this request stream.Append(BsonTranslator.AsInt32(this.Skip)); stream.Append(BsonTranslator.AsInt32(this.Take)); //generate the query stream.Append(this.Parameters.ToBsonByteArray()); //generate the field selectors if there are any if (this.Fields.Count > 0) { //create the selector document BsonDocument select = new BsonDocument(); for (int i = 0; i < this.Fields.Count; i++) { select.Set(this.Fields[i], i + 1); } //append the bytes stream.Append(select.ToBsonByteArray()); } }