当前位置: 首页>>代码示例>>C#>>正文


C# BSONDocument.WriteAsBSON方法代码示例

本文整理汇总了C#中BSONDocument.WriteAsBSON方法的典型用法代码示例。如果您正苦于以下问题:C# BSONDocument.WriteAsBSON方法的具体用法?C# BSONDocument.WriteAsBSON怎么用?C# BSONDocument.WriteAsBSON使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在BSONDocument的用法示例。


在下文中一共展示了BSONDocument.WriteAsBSON方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: WriteBigIntegers

 public void WriteBigIntegers(Stream stream)
 {
     stream.Position = 0;
       var root = new BSONDocument();
       root.Set(new BSONInt32Element("intMin", int.MinValue));
       root.Set(new BSONInt32Element("intMax", int.MaxValue));
       root.Set(new BSONInt64Element("longMin", long.MinValue));
       root.Set(new BSONInt64Element("longMax", long.MaxValue));
       root.WriteAsBSON(stream);
 }
开发者ID:vlapchenko,项目名称:nfx,代码行数:10,代码来源:BSONTestForm.cs

示例2: WriteInt32Array

 /// <summary>
 /// Array of int32
 /// { 'years': [1963, 1984, 2015] } --> { 'years': { '0': 1963, '1': 1984, '2': 2015 } }
 /// </summary>
 public void WriteInt32Array(Stream stream)
 {
     var root = new BSONDocument();
       var array = new[] { new BSONInt32Element(1963), new BSONInt32Element(1984), new BSONInt32Element(2015) };
       root.Set(new BSONArrayElement("years", array));
       root.WriteAsBSON(stream);
 }
开发者ID:vlapchenko,项目名称:nfx,代码行数:11,代码来源:BSONTestForm.cs

示例3: WriteStringInt32DoubleMixedArray

        public void WriteStringInt32DoubleMixedArray()
        {
            using (var stream = new MemoryStream())
              using (var reader = new BinaryReader(stream))
              {
            var root = new BSONDocument();
            var array = new BSONElement[] { new BSONStringElement("apple"), new BSONInt32Element(3), new BSONDoubleElement(2.14D) };
            root.Set(new BSONArrayElement("stuff", array));
            root.WriteAsBSON(stream);

            Assert.AreEqual(stream.Position, 48); // ensure document length is 48 bytes

            stream.Seek(0, SeekOrigin.Begin);

            CollectionAssert.AreEqual(reader.ReadBytes(4), BitConverter.GetBytes(48));       // document's content length is 48
            Assert.AreEqual(reader.ReadByte(), (byte)BSONElementType.Array);                 // element type is array 0x04
            CollectionAssert.AreEqual(reader.ReadBytes(5), Encoding.UTF8.GetBytes("stuff")); // element name is 'stuff'
            Assert.AreEqual(reader.ReadByte(), (byte)0x00);                                  // string name terminator 0x00 is present
            CollectionAssert.AreEqual(reader.ReadBytes(4), BitConverter.GetBytes(36));       // array's content length is 36

            Assert.AreEqual(reader.ReadByte(), (byte)BSONElementType.String);               // element type is string 0x02
            CollectionAssert.AreEqual(reader.ReadBytes(1), Encoding.UTF8.GetBytes("0"));    // element name is '0'
            Assert.AreEqual(reader.ReadByte(), (byte)0x00);                                 // last byte is terminator 0x00
            CollectionAssert.AreEqual(reader.ReadBytes(4), BitConverter.GetBytes(6));       // string content length is 6
            CollectionAssert.AreEqual(reader.ReadBytes(5),Encoding.UTF8.GetBytes("apple")); // string content length is 'apple'
            Assert.AreEqual(reader.ReadByte(), (byte)0x00);                                 // last byte is terminator 0x00

            Assert.AreEqual(reader.ReadByte(), (byte)BSONElementType.Int32);             // element type is int32 0x10
            CollectionAssert.AreEqual(reader.ReadBytes(1), Encoding.UTF8.GetBytes("1")); // element name is '1'
            Assert.AreEqual(reader.ReadByte(), (byte)0x00);                              // last byte is terminator 0x00
            CollectionAssert.AreEqual(reader.ReadBytes(4), BitConverter.GetBytes(3));    // value is 3

            Assert.AreEqual(reader.ReadByte(), (byte)BSONElementType.Double);             // element type is double 0x01
            CollectionAssert.AreEqual(reader.ReadBytes(1), Encoding.UTF8.GetBytes("2")); // element name is '2'
            Assert.AreEqual(reader.ReadByte(), (byte)0x00);                              // last byte is terminator 0x00
            CollectionAssert.AreEqual(reader.ReadBytes(8), BitConverter.GetBytes(2.14D)); // value is 2.14

            Assert.AreEqual(reader.ReadByte(), (byte)0x00); // ensure last byte is terminator 0x00
            Assert.AreEqual(reader.ReadByte(), (byte)0x00); // ensure last byte is terminator 0x00

            Assert.AreEqual(stream.Position, 48); // ensure whole document readed
              }
        }
开发者ID:itadapter,项目名称:nfx,代码行数:43,代码来源:BSON.cs

示例4: WriteStringAndInt32Pair

        public void WriteStringAndInt32Pair()
        {
            using (var stream = new MemoryStream())
              using (var reader = new BinaryReader(stream))
              {
            var root = new BSONDocument();
            root.Set(new BSONStringElement("name", "Gagarin"));
            root.Set(new BSONInt32Element("birth", 1934));
            root.WriteAsBSON(stream);

            Assert.AreEqual(stream.Position, 34); // ensure document length is 38 bytes

            stream.Seek(0, SeekOrigin.Begin);

            CollectionAssert.AreEqual(reader.ReadBytes(sizeof(int)), BitConverter.GetBytes(34)); // ensure content length is 34
            Assert.AreEqual(reader.ReadByte(), (byte)BSONElementType.String); // ensure element type is string 0x02
            CollectionAssert.AreEqual(reader.ReadBytes(4), Encoding.UTF8.GetBytes("name")); // ensure element name is 'name'
            Assert.AreEqual(reader.ReadByte(), (byte)0x00); // ensure string name terminator 0x00 is present
            CollectionAssert.AreEqual(reader.ReadBytes(sizeof(int)), BitConverter.GetBytes(8)); // ensure string content length is 8
            CollectionAssert.AreEqual(reader.ReadBytes(7), Encoding.UTF8.GetBytes("Gagarin")); // ensure element value is 'Gagarin'
            Assert.AreEqual(reader.ReadByte(), (byte)0x00); // ensure string value terminator 0x00 is present

            Assert.AreEqual(reader.ReadByte(), (byte)BSONElementType.Int32); // ensure element type is int 0x10
            CollectionAssert.AreEqual(reader.ReadBytes(5), Encoding.UTF8.GetBytes("birth")); // ensure element name is 'birth'
            Assert.AreEqual(reader.ReadByte(), (byte)0x00); // ensure string name terminator 0x00 is present
            CollectionAssert.AreEqual(reader.ReadBytes(4), BitConverter.GetBytes(1934)); // ensure element value is int 1934
            Assert.AreEqual(reader.ReadByte(), (byte)0x00); // ensure last byte is terminator 0x00

            Assert.AreEqual(stream.Position, 34); // ensure whole document readed
              }
        }
开发者ID:itadapter,项目名称:nfx,代码行数:31,代码来源:BSON.cs

示例5: WriteSingleString

        public void WriteSingleString()
        {
            using (var stream = new MemoryStream())
              using (var reader = new BinaryReader(stream))
              {
            var root = new BSONDocument();
            root.Set(new BSONStringElement("greetings", "Hello World!"));
            root.WriteAsBSON(stream);

            Assert.AreEqual(stream.Position, 33); // ensure document length is 33 bytes

            stream.Seek(0, SeekOrigin.Begin);

            CollectionAssert.AreEqual(reader.ReadBytes(sizeof(int)), BitConverter.GetBytes(33)); // ensure content length is 33
            Assert.AreEqual(reader.ReadByte(), (byte)BSONElementType.String); // ensure element type is string 0x02
            CollectionAssert.AreEqual(reader.ReadBytes(9), Encoding.UTF8.GetBytes("greetings")); // ensure element name is 'greetings'
            Assert.AreEqual(reader.ReadByte(), (byte)0x00); // ensure string name terminator 0x00 is present
            CollectionAssert.AreEqual(reader.ReadBytes(sizeof(int)), BitConverter.GetBytes(13)); // ensure string content length is 13
            CollectionAssert.AreEqual(reader.ReadBytes(12), Encoding.UTF8.GetBytes("Hello World!")); // ensure element value is 'Hello World!'
            Assert.AreEqual(reader.ReadByte(), (byte)0x00); // ensure string value terminator 0x00 is present
            Assert.AreEqual(reader.ReadByte(), (byte)0x00); // ensure last byte is terminator 0x00

            Assert.AreEqual(stream.Position, 33); // ensure whole document readed
              }
        }
开发者ID:itadapter,项目名称:nfx,代码行数:25,代码来源:BSON.cs

示例6: WriteSingleObjectId

        public void WriteSingleObjectId()
        {
            using (var stream = new MemoryStream())
              using (var reader = new BinaryReader(stream))
              {
            var hex = "507f1f77bcf86cd799439011";
            var data = Enumerable.Range(0, hex.Length)
                     .Where(x => x % 2 == 0)
                     .Select(x => Convert.ToByte(hex.Substring(x, 2), 16))
                     .ToArray();
            var root = new BSONDocument();
            var objectId = new BSONObjectID(data);
            root.Set(new BSONObjectIDElement("objectId", objectId));
            root.WriteAsBSON(stream);

            Assert.AreEqual(stream.Position, 27); // ensure document length is 27 bytes

            stream.Seek(0, SeekOrigin.Begin);

            CollectionAssert.AreEqual(reader.ReadBytes(4), BitConverter.GetBytes(27));          // content length is 27
            Assert.AreEqual(reader.ReadByte(), (byte)BSONElementType.ObjectID);                 // element type is objectID 0x07
            CollectionAssert.AreEqual(reader.ReadBytes(8), Encoding.UTF8.GetBytes("objectId")); // element name is 'objectId'
            Assert.AreEqual(reader.ReadByte(), (byte)0x00);                                     // string name terminator 0x00 is present
            CollectionAssert.AreEqual(reader.ReadBytes(12), data);                              // byte content is correct

            Assert.AreEqual(reader.ReadByte(), (byte)0x00); // ensure last byte is terminator 0x00

            Assert.AreEqual(stream.Position, 27); // ensure whole document readed
              }
        }
开发者ID:itadapter,项目名称:nfx,代码行数:30,代码来源:BSON.cs

示例7: WriteSingleMinKey

        public void WriteSingleMinKey()
        {
            using (var stream = new MemoryStream())
              using (var reader = new BinaryReader(stream))
              {
            var root = new BSONDocument();
            root.Set(new BSONMinKeyElement("minkey"));
            root.WriteAsBSON(stream);

            Assert.AreEqual(stream.Position, 13); // ensure document length is 13 bytes

            stream.Seek(0, SeekOrigin.Begin);

            CollectionAssert.AreEqual(reader.ReadBytes(4), BitConverter.GetBytes(13));        // ensure content length is 13
            Assert.AreEqual(reader.ReadByte(), (byte)BSONElementType.MinKey);                 // ensure element type is MinKey 0xff
            CollectionAssert.AreEqual(reader.ReadBytes(6), Encoding.UTF8.GetBytes("minkey")); // ensure element name is 'minkey'
            Assert.AreEqual(reader.ReadByte(), (byte)0x00);                                   // ensure string name terminator 0x00 is present
            Assert.AreEqual(reader.ReadByte(), (byte)0x00);                                   // ensure last byte is terminator 0x00

            Assert.AreEqual(stream.Position, 13); // ensure whole document readed
              }
        }
开发者ID:itadapter,项目名称:nfx,代码行数:22,代码来源:BSON.cs

示例8: WriteSingleJavaScript

        public void WriteSingleJavaScript()
        {
            using (var stream = new MemoryStream())
              using (var reader = new BinaryReader(stream))
              {
            var root = new BSONDocument();
            var code = "function(){var x=1;var y='abc';return 1;};";
            root.Set(new BSONJavaScriptElement("code", code));
            root.WriteAsBSON(stream);

            Assert.AreEqual(stream.Position, 58); // ensure document length is 58 bytes

            stream.Seek(0, SeekOrigin.Begin);

            CollectionAssert.AreEqual(reader.ReadBytes(4), BitConverter.GetBytes(58));      // content length is 58
            Assert.AreEqual(reader.ReadByte(), (byte)BSONElementType.JavaScript);           // element type is JavaScript 0x0d
            CollectionAssert.AreEqual(reader.ReadBytes(4), Encoding.UTF8.GetBytes("code")); // element name is 'code'
            Assert.AreEqual(reader.ReadByte(), (byte)0x00);                                 // string name terminator 0x00 is present

            CollectionAssert.AreEqual(reader.ReadBytes(4), BitConverter.GetBytes(43));     // js code content length is 43
            CollectionAssert.AreEqual(reader.ReadBytes(42), Encoding.UTF8.GetBytes(code)); // element value is code
            Assert.AreEqual(reader.ReadByte(), (byte)0x00);                                // string value terminator 0x00 is present
            Assert.AreEqual(reader.ReadByte(), (byte)0x00);                                // last byte is terminator 0x00

            Assert.AreEqual(stream.Position, 58); // ensure whole document readed
              }
        }
开发者ID:itadapter,项目名称:nfx,代码行数:27,代码来源:BSON.cs

示例9: WriteInt32Array

        public void WriteInt32Array()
        {
            using (var stream = new MemoryStream())
              using (var reader = new BinaryReader(stream))
              {
            var root = new BSONDocument();
            var array = new[] { new BSONInt32Element(1963), new BSONInt32Element(1984), new BSONInt32Element(2015) };
            root.Set(new BSONArrayElement("years", array));
            root.WriteAsBSON(stream);

            Assert.AreEqual(stream.Position, 38); // ensure document length is 38 bytes

            stream.Seek(0, SeekOrigin.Begin);

            CollectionAssert.AreEqual(reader.ReadBytes(4), BitConverter.GetBytes(38));       // document's content length is 38
            Assert.AreEqual(reader.ReadByte(), (byte)BSONElementType.Array);                 // element type is array 0x04
            CollectionAssert.AreEqual(reader.ReadBytes(5), Encoding.UTF8.GetBytes("years")); // element name is 'years'
            Assert.AreEqual(reader.ReadByte(), (byte)0x00);                                  // string name terminator 0x00 is present
            CollectionAssert.AreEqual(reader.ReadBytes(4), BitConverter.GetBytes(26));       // array's content length is 26

            Assert.AreEqual(reader.ReadByte(), (byte)BSONElementType.Int32);             // element type is int32 0x10
            CollectionAssert.AreEqual(reader.ReadBytes(1), Encoding.UTF8.GetBytes("0")); // element name is '0'
            Assert.AreEqual(reader.ReadByte(), (byte)0x00);                              // last byte is terminator 0x00
            CollectionAssert.AreEqual(reader.ReadBytes(4), BitConverter.GetBytes(1963)); // value is 1963

            Assert.AreEqual(reader.ReadByte(), (byte)BSONElementType.Int32);             // element type is int32 0x10
            CollectionAssert.AreEqual(reader.ReadBytes(1), Encoding.UTF8.GetBytes("1")); // element name is '1'
            Assert.AreEqual(reader.ReadByte(), (byte)0x00);                              // last byte is terminator 0x00
            CollectionAssert.AreEqual(reader.ReadBytes(4), BitConverter.GetBytes(1984)); // value is 1984

            Assert.AreEqual(reader.ReadByte(), (byte)BSONElementType.Int32);             // element type is int32 0x10
            CollectionAssert.AreEqual(reader.ReadBytes(1), Encoding.UTF8.GetBytes("2")); // element name is '2'
            Assert.AreEqual(reader.ReadByte(), (byte)0x00);                              // last byte is terminator 0x00
            CollectionAssert.AreEqual(reader.ReadBytes(4), BitConverter.GetBytes(2015)); // value is 2015

            Assert.AreEqual(reader.ReadByte(), (byte)0x00); // ensure last byte is terminator 0x00
            Assert.AreEqual(reader.ReadByte(), (byte)0x00); // ensure last byte is terminator 0x00

            Assert.AreEqual(stream.Position, 38); // ensure whole document readed
              }
        }
开发者ID:itadapter,项目名称:nfx,代码行数:41,代码来源:BSON.cs

示例10: WriteEmptyDocument

        public void WriteEmptyDocument()
        {
            using (var stream = new MemoryStream())
              using (var reader = new BinaryReader(stream))
              {
            var root = new BSONDocument();
            root.WriteAsBSON(stream);

            Assert.AreEqual(stream.Position, 5); // ensure document length is 5 bytes

            stream.Seek(0, SeekOrigin.Begin);

            CollectionAssert.AreEqual(reader.ReadBytes(sizeof(int)), BitConverter.GetBytes(5)); // ensure content length is 1
            Assert.AreEqual(reader.ReadByte(), (byte)0x00); // ensure last byte is terminator 0x00

            Assert.AreEqual(stream.Position, 5); // ensure whole document readed
              }
        }
开发者ID:itadapter,项目名称:nfx,代码行数:18,代码来源:BSON.cs

示例11: WriteBigIntegers

        public void WriteBigIntegers()
        {
            using (var stream = new MemoryStream())
              using (var reader = new BinaryReader(stream))
              {
            var root = new BSONDocument();
            root.Set(new BSONInt32Element("intMin", int.MinValue));
            root.Set(new BSONInt32Element("intMax", int.MaxValue));
            root.Set(new BSONInt64Element("longMin", long.MinValue));
            root.Set(new BSONInt64Element("longMax", long.MaxValue));
            root.WriteAsBSON(stream);

            Assert.AreEqual(stream.Position, 63); // ensure document length is 63 bytes

            stream.Seek(0, SeekOrigin.Begin);

            CollectionAssert.AreEqual(reader.ReadBytes(4), BitConverter.GetBytes(63));           // content length is 63

            Assert.AreEqual(reader.ReadByte(), (byte)BSONElementType.Int32);                     // element type is int32 0x10
            CollectionAssert.AreEqual(reader.ReadBytes(6), Encoding.UTF8.GetBytes("intMin"));    // element name is 'intMin'
            Assert.AreEqual(reader.ReadByte(), (byte)0x00);                                      // string name terminator 0x00 is present
            CollectionAssert.AreEqual(reader.ReadBytes(4), BitConverter.GetBytes(int.MinValue)); // element value is int.MinValue

            Assert.AreEqual(reader.ReadByte(), (byte)BSONElementType.Int32);                     // element type is int32 0x10
            CollectionAssert.AreEqual(reader.ReadBytes(6), Encoding.UTF8.GetBytes("intMax"));    // element name is 'intMax'
            Assert.AreEqual(reader.ReadByte(), (byte)0x00);                                      // string name terminator 0x00 is present
            CollectionAssert.AreEqual(reader.ReadBytes(4), BitConverter.GetBytes(int.MaxValue)); // element value is int.MaxValue

            Assert.AreEqual(reader.ReadByte(), (byte)BSONElementType.Int64);                      // element type is int64 0x12
            CollectionAssert.AreEqual(reader.ReadBytes(7), Encoding.UTF8.GetBytes("longMin"));    // element name is 'longMin'
            Assert.AreEqual(reader.ReadByte(), (byte)0x00);                                       // string name terminator 0x00 is present
            CollectionAssert.AreEqual(reader.ReadBytes(8), BitConverter.GetBytes(long.MinValue)); // element value is long.MinValue

            Assert.AreEqual(reader.ReadByte(), (byte)BSONElementType.Int64);                      // element type is int64 0x12
            CollectionAssert.AreEqual(reader.ReadBytes(7), Encoding.UTF8.GetBytes("longMax"));    // element name is 'longMax'
            Assert.AreEqual(reader.ReadByte(), (byte)0x00);                                       // string name terminator 0x00 is present
            CollectionAssert.AreEqual(reader.ReadBytes(8), BitConverter.GetBytes(long.MaxValue)); // element value is long.MaxValue

            Assert.AreEqual(reader.ReadByte(), (byte)0x00); // ensure last byte is terminator 0x00

            Assert.AreEqual(stream.Position, 63); // ensure whole document readed
              }
        }
开发者ID:itadapter,项目名称:nfx,代码行数:43,代码来源:BSON.cs

示例12: WriteAndReadSingleDateTime

        public void WriteAndReadSingleDateTime()
        {
            using (var stream = new MemoryStream())
                  {
                    var now = new DateTime(2010, 10, 12,  11, 20, 12, DateTimeKind.Local);
                    var root = new BSONDocument();
                    root.Set(new BSONDateTimeElement("mydate", now));
                    root.WriteAsBSON(stream);

                    Assert.AreEqual(stream.Position, 21); // ensure document length is 21 bytes

                    stream.Seek(0, SeekOrigin.Begin);

                    var root2 = new BSONDocument(stream);
                    Assert.AreEqual(stream.Position, 21); // ensure whole document read

                    Assert.AreEqual(1, root2.Count); // ensure whole document read
                    Assert.AreEqual(now.ToUniversalTime(), ((BSONDateTimeElement)root2["mydate"]).Value);
                  }
        }
开发者ID:itadapter,项目名称:nfx,代码行数:20,代码来源:BSON.cs

示例13: fullCopy

 private BSONDocument fullCopy(BSONDocument original)
 {
   //note: doc.DeepClone does not work as expected (copies references for byte[])
   using (var ms = new MemoryStream())
   {
     #pragma warning disable 0618
       original.WriteAsBSON(ms);
       ms.Position = 0;
       return new BSONDocument(ms);
     #pragma warning restore 0618
   }
 } 
开发者ID:huoxudong125,项目名称:nfx,代码行数:12,代码来源:RowConverterTest.cs

示例14: Write_QUERY

        public static Int32 Write_QUERY(Stream stream,
            Int32 requestID,
            Database db,
            Collection collection, //may be null for $CMD
            QueryFlags flags,
            Int32 numberToSkip,
            Int32 numberToReturn,
            BSONDocument query,
            BSONDocument selector//may be null
            )
        {
            stream.Position = STD_HDR_LEN;//skip the header

                          BinUtils.WriteInt32(stream, (Int32)flags);

                          //if collection==null then query the $CMD collection
                          var fullNameBuffer = collection!=null ? collection.m_FullNameCStringBuffer : db.m_CMD_NameCStringBuffer;
                          stream.Write(fullNameBuffer, 0, fullNameBuffer.Length);

                          BinUtils.WriteInt32(stream, numberToSkip);
                          BinUtils.WriteInt32(stream, numberToReturn);

                          query.WriteAsBSON(stream);

                          if (selector!=null)
                           selector.WriteAsBSON(stream);

                          var total = (Int32)stream.Position;
                          stream.Position = 0;
                          writeStandardHeader(stream, total, requestID, 0, OP_QUERY);
                          return total;
        }
开发者ID:itadapter,项目名称:nfx,代码行数:32,代码来源:Protocol.cs

示例15: WriteInt32Pair

 /// <summary>
 /// { name: "Gagarin", birth: 1934 }
 /// </summary>
 public void WriteInt32Pair(Stream stream)
 {
     var root = new BSONDocument();
       root.Set(new BSONInt32Element("name", 404));
       root.Set(new BSONInt32Element("birth", 1934));
       root.WriteAsBSON(stream);
 }
开发者ID:vlapchenko,项目名称:nfx,代码行数:10,代码来源:BSONTestForm.cs


注:本文中的BSONDocument.WriteAsBSON方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。