Пример #1
0
        /// <summary>
        /// Handles reading back the content for this query
        /// </summary>
        protected override void ParseStream(Stream stream)
        {
            var reader = new BinaryReader(stream);

            /* Message Format (Excluding Header)
             * Flag (int32) - normally zero, non-zero on query failure
             * CursorId (int64) - id of the cursor created for this query response
             * StartingFrom (int32) - indicates where in this content is starting from
             * TotalReturned (int32) - number of documents in the reply
             * Documents (BSON array) - The documents (parsed separately)
             */

            //get the flag first
            Flag = reader.ReadInt32();

            //second is the cursor value
            CursorId = reader.ReadInt64();

            //and the skipped and returned counts
            StartingFrom = reader.ReadInt32();
            TotalReturned = reader.ReadInt32();

            //next, read and parse the records
            Documents = new List<MongoDocument>();
            for (var i = 0; i < TotalReturned; i++) {

                //convert to a MongoDocument
                var parsed = BsonDocument.FromStream(stream);
                var document = new MongoDocument();
                document.Merge(parsed);
                //when the parsed doc has a field _id which is not MongOid, it ends up being replaced
                //as a fix, rename it as id
                if (parsed.Has(Mongo.DocumentIdKey) && parsed[Mongo.DocumentIdKey].GetType() != typeof(MongoOid))
                {
                    document["id"] = parsed[Mongo.DocumentIdKey];
                }

                //and add it to the list
                Documents.Add(document);
            }

            //check for server exceptions
            CheckForExceptions();
        }
Пример #2
0
        /// <summary>
        /// Handles reading back the content for this query
        /// </summary>
        protected override void ParseStream(Stream stream)
        {
            BinaryReader reader = new BinaryReader(stream);

            /* Message Format (Excluding Header)
             * Flag (int32) - normally zero, non-zero on query failure
             * CursorId (int64) - id of the cursor created for this query response
             * StartingFrom (int32) - indicates where in this content is starting from
             * TotalReturned (int32) - number of documents in the reply
             * Documents (BSON array) - The documents (parsed separately)
             */

            //get the flag first
            this.Flag = reader.ReadInt32();

            //second is the cursor value
            this.CursorId = reader.ReadInt64();

            //and the skipped and returned counts
            this.StartingFrom = reader.ReadInt32();
            this.TotalReturned = reader.ReadInt32();

            //next, read and parse the records
            this.Documents = new List<MongoDocument>();
            for (int i = 0; i < this.TotalReturned; i++) {

                //convert to a MongoDocument
                BsonDocument parsed = BsonDocument.FromStream(stream);
                MongoDocument document = new MongoDocument();
                document.Merge(parsed);

                //and add it to the list
                this.Documents.Add(document);
            }

            //check for server exceptions
            this.CheckForExceptions();
        }