/// <summary> /// Generates the message to send /// </summary> protected override void GenerateBody(DynamicStream stream) { BsonDocument document = new BsonDocument(); document["db.users.remove()"] = 1.0; stream.Append(document.ToBsonByteArray()); }
private BsonDocument Build() { var findAndModifyDoc = new BsonDocument(); findAndModifyDoc["findandmodify"] = associatedTable; findAndModifyDoc["query"] = query; findAndModifyDoc["new"] = returnUpdatedValues; findAndModifyDoc["upsert"] = true; findAndModifyDoc["update"] = update; return findAndModifyDoc; }
/// <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> /// Allows you to append a query option using the Mongo syntax /// </summary> public MongoQuery AppendParameter(string field, string modifier, object value) { //if using a modifier, set this as a document if (modifier is string) { BsonDocument parameters = new BsonDocument(); parameters[modifier] = value; this._Parameters[field] = parameters; } //otherwise, just assign the value else { this._Parameters[field] = value; } //return the query to use return this; }
/// <summary> /// Reads an incoming BSON document from a stream /// </summary> public static BsonDocument FromStream(Stream stream) { //read the first byte to determine the type BinaryReader reader = new BinaryReader(stream); //get the length of this document and the amount //that should be read into the parsing stream. //removes 4 bytes to account for the length value //and an additional 1 byte to account for the //terminator value int length = reader.ReadInt32(); int read = length - (DOCUMENT_LENGTH_COUNT + DOCUMENT_TERMINATOR_COUNT); //read out the bytes to use and the terminator byte[] bytes = reader.ReadBytes(read); reader.ReadByte(); //use the bytes to generate the document using (MemoryStream content = new MemoryStream(bytes)) { //read the content Dictionary<string, object> values = BsonTranslator.FromStream(content); //fill and return the object BsonDocument document = new BsonDocument(); document.Merge(values); return document; } }
//prepares the actual update to send private void _SendUpdate(string type, UpdateOptionTypes options, BsonDocument changes) { //update the changes to actually make BsonDocument document = new BsonDocument(); document[type] = changes; //create the request to use UpdateRequest request = new UpdateRequest(this.Collection); request.Modifications = document; request.Parameters = this._Parameters; request.Options = options; //make sure something is found to change if (request.Modifications.FieldCount == 0) { return; } //send the request and get the response this.Collection.Database.Connection.SendRequest(request); }
/// <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> /// Updates all matching records with the values on the provided BsonDocument /// or adds the new item to object entirely /// </summary> public void Set(BsonDocument document) { this._SendUpdate("$set", UpdateOptionTypes.MultiUpdate, document); }
/// <summary> /// Creates a new query for this database /// </summary> public MongoQuery(MongoCollection collection) : base(collection) { this._Parameters = new BsonDocument(); }
/// <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()); } }
/// <summary> /// Returns the count of records from the specified collection /// that meet the criteria for the query /// </summary> public static CollectionCountResult CollectionCount(MongoDatabase database, string collection, BsonDocument query) { CommandResponse result = MongoDatabaseCommands.RunCommand(database, new { count = collection, query = query }); return new CollectionCountResult(result.GetDefaultResponse()); }
/// <summary> /// Creates a document for the command execution /// </summary> /// <returns></returns> public BsonDocument Build() { var doc = new BsonDocument{IgnoreNulls = true}; var output = @out; @out = null; doc += this; if (wrapFunctions) { doc["map"] = string.Format("function(){{ {0}; }}", map); doc["reduce"] = string.Format("function(k,vs){{ {0}; return ret; }}", reduce); if (!string.IsNullOrEmpty(finalize)) doc["finalize"] = string.Format("function(k,v){{ {0}; return v; }}", finalize); } var outDoc = new BsonDocument {IgnoreNulls = true}; outDoc += output; doc["out"] = outDoc; @out = output; return doc; }
public FindAndModifyParameters() { query = new BsonDocument(); update = new BsonDocument(); }