/// <summary> /// Prepares the body of the request to send to the server /// </summary> protected override void GenerateBody(DynamicStream stream) { //required ZERO int after header stream.Append(BsonTranslator.AsInt32(0)); //apply the collection and database stream.Append(BsonTranslator.AsString(this.GetDatabaseTarget())); //and generate each of the documents foreach (MongoDocument document in this.Documents) { stream.Append(document.ToBsonByteArray()); } }
/// <summary> /// Generates the body for this request /// </summary> protected override void GenerateBody(DynamicStream stream) { //required ZERO byte stream.Append(BsonTranslator.AsInt32(0)); //the name of the collection stream.Append(BsonTranslator.AsString(Cursor.Query.GetDatabaseTarget())); //the total records to select stream.Append(BsonTranslator.AsInt32(Count)); //required ZERO byte stream.Append(BsonTranslator.AsInt64(Cursor.Cursor)); }
/// <summary> /// Prepares the body of the request to send to the server /// </summary> protected override void GenerateBody(DynamicStream stream) { //required ZERO byte after header stream.Append(BsonTranslator.AsInt32(0)); //apply the collection and database stream.Append(BsonTranslator.AsString(this.GetDatabaseTarget())); //required ZERO byte before selector stream.Append(BsonTranslator.AsInt32(0)); //then the actual selection stream.Append(this.Parameters.ToBsonByteArray()); }
/// <summary> /// Writes a binary object to the Mongo database /// </summary> public static byte[] AsBinary(byte[] value) { var stream = new DynamicStream(4); //write the kinds of stream this is //for now default to User-Defined stream.Append((byte)MongoBinaryTypes.UserDefined); //write the bytes and update the length stream.Append(value); stream.WriteAt(0, BitConverter.GetBytes(value.Length)); //and return the final bytes to use return stream.ToArray(); }
/// <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()); }
/// <summary> /// Writes a the bytes for a CS string in BSON format /// </summary> public static byte[] AsCString(string value) { var stream = new DynamicStream(); stream.Append(AsString(value)); stream.InsertAt(0, BitConverter.GetBytes(stream.Length)); return stream.ToArray(); }
/// <summary> /// Generates the command to send to the server /// </summary> protected override void GenerateBody(DynamicStream stream) { //default 'none' for the query options stream.Append(BsonTranslator.AsInt32(0)); //apply the collection and database which should //be the database.$target format stream.Append(BsonTranslator.AsString(this.GetDatabaseTarget())); //skip 0 and take 1 means to do a 'FindOne' command //which actually executes our command as code stream.Append(BsonTranslator.AsInt32(0)); stream.Append(BsonTranslator.AsInt32(1)); //insert the command value at the front of the request stream.Append(this.Arguments.ToBsonByteArray()); }
/// <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> /// Creates the bytes for a regular string in BSON format /// </summary> public static byte[] AsString(string value) { var stream = new DynamicStream(); stream.Append(Encoding.UTF8.GetBytes(value)); stream.Append(0); return stream.ToArray(); }
/// <summary> /// Writes a regex in BSON format /// </summary> /// <param name="value">The regex instance</param> /// <returns>regex as bson byte arrray</returns> public static byte[] AsRegex(Regex value) { var optionsStr = ""; var flags = value.Options; if ((flags & RegexOptions.IgnoreCase) == RegexOptions.IgnoreCase) optionsStr += "i"; if ((flags & RegexOptions.CultureInvariant) == RegexOptions.CultureInvariant) optionsStr += "l"; if ((flags & RegexOptions.Multiline) == RegexOptions.Multiline) optionsStr += "m"; if ((flags & RegexOptions.Singleline) == RegexOptions.Singleline) optionsStr += "s"; if ((flags & RegexOptions.IgnorePatternWhitespace) == RegexOptions.IgnorePatternWhitespace) optionsStr += "x"; var stream = new DynamicStream(); stream.Append(AsString(value.ToString())); stream.Append(AsString(optionsStr)); return stream.ToArray(); }