ToBsonByteArray() public method

Renders the bytes required to create a document
public ToBsonByteArray ( ) : byte[]
return byte[]
Example #1
0
        /// <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());
        }
Example #2
0
        /// <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();
        }
Example #3
0
        /// <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());

            }
        }