Creates a BSON document where the field order is managed
Inheritance: BsonObject
コード例 #1
0
ファイル: MessageRequest.cs プロジェクト: hugoware/CSMongo
        /// <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());
        }
コード例 #2
0
 private BsonDocument Build()
 {
     var findAndModifyDoc = new BsonDocument();
     findAndModifyDoc["findandmodify"] = associatedTable;
     findAndModifyDoc["query"] = query;
     findAndModifyDoc["new"] = returnUpdatedValues;
     findAndModifyDoc["upsert"] = true;
     findAndModifyDoc["update"] = update;
     return findAndModifyDoc;
 }
コード例 #3
0
ファイル: BsonTranslator.cs プロジェクト: ritcoder/CSMongo
        /// <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();
        }
コード例 #4
0
ファイル: MongoQuery.cs プロジェクト: hugoware/CSMongo
        /// <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;
        }
コード例 #5
0
ファイル: BsonDocument.cs プロジェクト: hugoware/CSMongo
        /// <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;

            }
        }
コード例 #6
0
ファイル: MongoQuery.cs プロジェクト: hugoware/CSMongo
        //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);
        }
コード例 #7
0
ファイル: MongoQuery.cs プロジェクト: hugoware/CSMongo
        /// <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);
        }
コード例 #8
0
ファイル: MongoQuery.cs プロジェクト: hugoware/CSMongo
 /// <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);
 }
コード例 #9
0
ファイル: MongoQuery.cs プロジェクト: hugoware/CSMongo
 /// <summary>
 /// Creates a new query for this database
 /// </summary>
 public MongoQuery(MongoCollection collection)
     : base(collection)
 {
     this._Parameters = new BsonDocument();
 }
コード例 #10
0
ファイル: MongoQuery.cs プロジェクト: hugoware/CSMongo
        /// <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);
        }
コード例 #11
0
ファイル: MongoQuery.cs プロジェクト: hugoware/CSMongo
        /// <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);
        }
コード例 #12
0
ファイル: QueryRequest.cs プロジェクト: hugoware/CSMongo
        /// <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());

            }
        }
コード例 #13
0
 /// <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());
 }
コード例 #14
0
 /// <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;
 }
コード例 #15
0
 public FindAndModifyParameters()
 {
     query = new BsonDocument();
     update = new BsonDocument();
 }