• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

C# BsonObjectId类代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了C#中BsonObjectId的典型用法代码示例。如果您正苦于以下问题:C# BsonObjectId类的具体用法?C# BsonObjectId怎么用?C# BsonObjectId使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。



BsonObjectId类属于命名空间,在下文中一共展示了BsonObjectId类的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的C#代码示例。

示例1: TestDoesNotThrowStackOverflowExceptionWhenConvertingToSelfType

        public void TestDoesNotThrowStackOverflowExceptionWhenConvertingToSelfType()
        {
            var id1 = new BsonObjectId(ObjectId.GenerateNewId());
            var id2 = (BsonObjectId)((IConvertible)id1).ToType(typeof(BsonObjectId), null);

            Assert.Equal(id1, id2);
        }
开发者ID:RavenZZ,项目名称:MDRelation,代码行数:7,代码来源:CSharp595Tests.cs


示例2: FindOneByClientId

        public static Client FindOneByClientId(MongoDatabase mongoDb, BsonObjectId clientId)
        {
            if (mongoDb == null ||
                clientId == null)
            {
                return null;
            }

            if (mongoDb.Server == null)
                mongoDb = Helper.MongoDb.GetDatabase();

            Client client = null;

            try
            {
                var clientCol = mongoDb.GetCollection<Client>("Client");
                var clientQuery = Query.EQ("_id", clientId);
                client = clientCol.FindOne(clientQuery);
            }
            catch (Exception ex)
            {
                if (log.IsDebugEnabled) { log.Error("FindOneByClientId.Client." + (client == null ? "null" : client.ToJsonString()), ex); }

                throw ex;
                return null; // "Error: unable to Client.FindOneByClientId for " + ClientId;
            }

            if (log.IsDebugEnabled) { log.Debug("FindOneByClientId.Client." + (client == null ? "null" : client.ToJsonString())); }

            return client;
        }
开发者ID:travisrussi,项目名称:KissCaller,代码行数:31,代码来源:Client.cs


示例3: BsonObjectId

            public void BsonObjectId()
            {
                const string id = "52ddb25d606fdc049c7aaf7e";
                BsonObjectId bsonId = new BsonObjectId(new ObjectId(id));
                string expected = string.Format("{0}", bsonId.Value.Increment);

                Assert.AreEqual(expected, MongoDataAccessExtensions.GenerateDisplayId(id));
            }
开发者ID:cgavieta,项目名称:WORKPAC2016-poc,代码行数:8,代码来源:MongoDataAccessExtensionsTests.cs


示例4: TestCompareLargerIncrement

 public void TestCompareLargerIncrement()
 {
     var objectId1 = new BsonObjectId("0102030405060708090a0b0d");
     var objectId2 = new BsonObjectId("0102030405060708090a0b0c");
     Assert.IsFalse(objectId1 < objectId2);
     Assert.IsFalse(objectId1 <= objectId2);
     Assert.IsTrue(objectId1 != objectId2);
     Assert.IsFalse(objectId1 == objectId2);
     Assert.IsTrue(objectId1 > objectId2);
     Assert.IsTrue(objectId1 >= objectId2);
 }
开发者ID:modesto,项目名称:mongo-csharp-driver,代码行数:11,代码来源:BsonObjectIdTests.cs


示例5: TestDoesNotThrowStackOverflowExceptionWhenConvertingToBsonString

        public void TestDoesNotThrowStackOverflowExceptionWhenConvertingToBsonString()
        {
            BsonObjectId id1 = new BsonObjectId(ObjectId.GenerateNewId());
            BsonString id2 = null;
            Assert.DoesNotThrow(() =>
            {
                id2 = (BsonString)((IConvertible)id1).ToType(typeof(BsonString), null);
            });

            Assert.AreEqual(id1.ToString(), id2.AsString);
        }
开发者ID:robinNode,项目名称:mongo-csharp-driver,代码行数:11,代码来源:CSharp595Tests.cs


示例6: TestByteArrayConstructor

 public void TestByteArrayConstructor()
 {
     byte[] bytes = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 };
     var objectId = new BsonObjectId(bytes);
     Assert.AreEqual(0x01020304, objectId.Timestamp);
     Assert.AreEqual(0x050607, objectId.Machine);
     Assert.AreEqual(0x0809, objectId.Pid);
     Assert.AreEqual(0x0a0b0c, objectId.Increment);
     Assert.AreEqual(0x05060708090a0b0c, objectId.MachinePidIncrement);
     Assert.AreEqual(Bson.UnixEpoch.AddSeconds(0x01020304), objectId.CreationTime);
     Assert.AreEqual("0102030405060708090a0b0c", objectId.ToString());
     Assert.IsTrue(bytes.SequenceEqual(objectId.ToByteArray()));
 }
开发者ID:abolibibelot,项目名称:mongo-csharp-driver,代码行数:13,代码来源:BsonObjectIdTests.cs


示例7: GenerateDisplayId

        /// <summary>
        /// Generates a display ID for an application ID <paramref name="applicationId"/>.
        /// This is a human-readable, shortened version of the application ID,
        /// used for easy reference.
        /// </summary>
        /// <param name="applicationId">The application identifier.</param>
        /// <returns>A human-readable version of the application ID <paramref name="applicationId"/>.</returns>
        public static string GenerateDisplayId(string applicationId)
        {
            if (string.IsNullOrEmpty(applicationId))
            {
                return null;
            }

            if (applicationId.Length != 24)
            {
                return applicationId;
            }

            BsonObjectId bsonId = new BsonObjectId(new ObjectId(applicationId));
            return string.Format("{0}", bsonId.Value.Increment);
        }
开发者ID:cgavieta,项目名称:WORKPAC2016-poc,代码行数:22,代码来源:MongoDataAccessExtensions.cs


示例8: TestIntIntShortIntConstructor

        public void TestIntIntShortIntConstructor()
        {
#pragma warning disable 618
            byte[] bytes = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 };
            var objectId = new BsonObjectId(0x01020304, 0x050607, 0x0809, 0x0a0b0c);
            Assert.AreEqual(0x01020304, objectId.Timestamp);
            Assert.AreEqual(0x050607, objectId.Machine);
            Assert.AreEqual(0x0809, objectId.Pid);
            Assert.AreEqual(0x0a0b0c, objectId.Increment);
            Assert.AreEqual(0x050607, objectId.Machine);
            Assert.AreEqual(0x0809, objectId.Pid);
            Assert.AreEqual(0x0a0b0c, objectId.Increment);
            Assert.AreEqual(BsonConstants.UnixEpoch.AddSeconds(0x01020304), objectId.CreationTime);
            Assert.AreEqual("0102030405060708090a0b0c", objectId.ToString());
            Assert.IsTrue(bytes.SequenceEqual(objectId.ToByteArray()));
#pragma warning restore
        }
开发者ID:CloudMetal,项目名称:mongo-csharp-driver,代码行数:17,代码来源:BsonObjectIdTests.cs


示例9: TestConvertMultipleToObjectViaJson

        public void TestConvertMultipleToObjectViaJson()
        {
            BsonDocument person1 = new BsonDocument();
            BsonDocument person2 = new BsonDocument();
            List<BsonDocument> people = new List<BsonDocument>();

            const string id1 = "51ca5da43d1b18cc15000000";
            const string name1 = "Luke Skywalker";
            const string gender1 = "Male";
            DateTime dateOfBirth1 = new DateTime(1977, 5, 25);

            person1["_id"] = new BsonObjectId(new ObjectId(id1));
            person1["Name"] = new BsonString(name1);
            person1["Gender"] = new BsonString(gender1);
            person1["DateOfBirth"] = new BsonDateTime(dateOfBirth1);
            people.Add(person1);

            const string id2 = "52cb5da43d1f17cc15000000";
            const string name2 = "Princess Leia";
            const string gender2 = "Female";
            DateTime dateOfBirth2 = new DateTime(1973, 5, 25);

            person2["_id"] = new BsonObjectId(new ObjectId(id2));
            person2["Name"] = new BsonString(name2);
            person2["Gender"] = new BsonString(gender2);
            person2["DateOfBirth"] = new BsonDateTime(dateOfBirth2);
            people.Add(person2);

            List<Person> personList = BsonConverter.ConvertToObjectViaJson<List<Person>>(people);
            for (var i = 0; i < personList.Count; i++)
            {
                var person = personList[i];
                var personToUse = i == 0 ? person1 : person2;
                Assert.AreEqual(person.Id, personToUse["_id"].ToString());
                Assert.AreEqual(person.Name, personToUse["Name"]);
                Assert.AreEqual(person.Gender, personToUse["Gender"]);
                Assert.AreEqual(person.DateOfBirth.ToUniversalTime(), personToUse["DateOfBirth"].ToUniversalTime());
                Assert.IsNull(person.DateOfDeath);
            }
        }
开发者ID:cgavieta,项目名称:WORKPAC2016-poc,代码行数:40,代码来源:BsonConverterTests.cs


示例10: ReadValue


//.........这里部分代码省略.........

                case BsonType.Double:
                    var bsonDouble = new BsonDouble(_wrappedReader.ReadDouble());
                    switch (FloatParseHandling)
                    {
                        case Newtonsoft.Json.FloatParseHandling.Decimal:
                            jsonDotNetValue = Convert.ToDecimal(bsonDouble);
                            break;

                        case Newtonsoft.Json.FloatParseHandling.Double:
                            jsonDotNetValue = bsonDouble.Value;
                            break;

                        default:
                            throw new NotSupportedException(string.Format("Unexpected FloatParseHandling value: {0}.", FloatParseHandling));
                    }
                    SetCurrentToken(Newtonsoft.Json.JsonToken.Float, jsonDotNetValue, bsonDouble);
                    return;

                case BsonType.Int32:
                    var bsonInt32 = (BsonInt32)_wrappedReader.ReadInt32();
                    SetCurrentToken(Newtonsoft.Json.JsonToken.Integer, (long)bsonInt32.Value, bsonInt32);
                    return;

                case BsonType.Int64:
                    var bsonInt64 = (BsonInt64)_wrappedReader.ReadInt64();
                    SetCurrentToken(Newtonsoft.Json.JsonToken.Integer, bsonInt64.Value, bsonInt64);
                    return;

                case BsonType.JavaScript:
                    {
                        var code = _wrappedReader.ReadJavaScript();
                        var bsonJavaScript = new BsonJavaScript(code);
                        SetCurrentToken(Newtonsoft.Json.JsonToken.String, code, bsonJavaScript);
                    }
                    return;

                case BsonType.JavaScriptWithScope:
                    {
                        var code = _wrappedReader.ReadJavaScriptWithScope();
                        var context = BsonDeserializationContext.CreateRoot(_wrappedReader);
                        var scope = BsonDocumentSerializer.Instance.Deserialize<BsonDocument>(context);
                        var bsonJavaScriptWithScope = new BsonJavaScriptWithScope(code, scope);
                        SetCurrentToken(Newtonsoft.Json.JsonToken.String, code, bsonJavaScriptWithScope);
                    }
                    return;

                case BsonType.MaxKey:
                    _wrappedReader.ReadMaxKey();
                    SetCurrentToken(Newtonsoft.Json.JsonToken.Undefined, null, BsonMaxKey.Value);
                    return;

                case BsonType.MinKey:
                    _wrappedReader.ReadMinKey();
                    SetCurrentToken(Newtonsoft.Json.JsonToken.Undefined, null, BsonMinKey.Value);
                    return;

                case BsonType.Null:
                    _wrappedReader.ReadNull();
                    SetCurrentToken(Newtonsoft.Json.JsonToken.Null, null, BsonNull.Value);
                    return;

                case BsonType.ObjectId:
                    var bsonObjectId = new BsonObjectId(_wrappedReader.ReadObjectId());
                    SetCurrentToken(Newtonsoft.Json.JsonToken.Bytes, bsonObjectId.Value.ToByteArray(), bsonObjectId);
                    return;

                case BsonType.RegularExpression:
                    var bsonRegularExpression = _wrappedReader.ReadRegularExpression();
                    var pattern = bsonRegularExpression.Pattern;
                    var options = bsonRegularExpression.Options;
                    jsonDotNetValue = "/" + pattern.Replace("/", "\\/") + "/" + options;
                    SetCurrentToken(Newtonsoft.Json.JsonToken.String, jsonDotNetValue, bsonRegularExpression);
                    return;

                case BsonType.String:
                    var stringValue = _wrappedReader.ReadString();
                    SetCurrentToken(Newtonsoft.Json.JsonToken.String, stringValue, (BsonString)stringValue);
                    return;

                case BsonType.Symbol:
                    var bsonSymbol = BsonSymbolTable.Lookup(_wrappedReader.ReadSymbol());
                    SetCurrentToken(Newtonsoft.Json.JsonToken.String, bsonSymbol.Name, bsonSymbol);
                    return;

                case BsonType.Timestamp:
                    var bsonTimestamp = new BsonTimestamp(_wrappedReader.ReadTimestamp());
                    SetCurrentToken(Newtonsoft.Json.JsonToken.Integer, bsonTimestamp.Value, bsonTimestamp);
                    return;

                case BsonType.Undefined:
                    _wrappedReader.ReadUndefined();
                    SetCurrentToken(Newtonsoft.Json.JsonToken.Undefined, null, BsonUndefined.Value);
                    return;

                default:
                    var message = string.Format("Unexpected BsonType: {0}.", _wrappedReader.GetCurrentBsonType());
                    throw new Newtonsoft.Json.JsonReaderException(message);
            }
        }
开发者ID:rstam,项目名称:mongo-csharp-driver-jsondotnet-original,代码行数:101,代码来源:JsonReaderAdapter.cs


示例11: TestBsonObjectId

 public void TestBsonObjectId()
 {
     var value = new BsonObjectId(ObjectId.Parse("0102030405060708090a0b0c"));
     Assert.AreSame(value, ((IConvertible)value).ToType(typeof(object), null));
     Assert.Throws<InvalidCastException>(() => Convert.ToBoolean(value));
     Assert.Throws<InvalidCastException>(() => Convert.ToByte(value));
     Assert.Throws<InvalidCastException>(() => Convert.ToChar(value));
     Assert.Throws<InvalidCastException>(() => Convert.ToDateTime(value));
     Assert.Throws<InvalidCastException>(() => Convert.ToDecimal(value));
     Assert.Throws<InvalidCastException>(() => Convert.ToDouble(value));
     Assert.Throws<InvalidCastException>(() => Convert.ToInt16(value));
     Assert.Throws<InvalidCastException>(() => Convert.ToInt32(value));
     Assert.Throws<InvalidCastException>(() => Convert.ToInt64(value));
     Assert.Throws<InvalidCastException>(() => Convert.ToSByte(value));
     Assert.Throws<InvalidCastException>(() => Convert.ToSingle(value));
     Assert.AreEqual("0102030405060708090a0b0c", Convert.ToString(value));
     Assert.AreEqual("0102030405060708090a0b0c", ((IConvertible)value).ToType(typeof(string), null));
     Assert.Throws<InvalidCastException>(() => Convert.ToUInt16(value));
     Assert.Throws<InvalidCastException>(() => Convert.ToUInt32(value));
     Assert.Throws<InvalidCastException>(() => Convert.ToUInt64(value));
 }
开发者ID:niemyjski,项目名称:mongo-csharp-driver,代码行数:21,代码来源:BsonValueIConvertibleTests.cs


示例12: getUserById

 public User getUserById(string id)
 {
     // Get an Oid from the ID string
     var oid = new BsonObjectId( new ObjectId(id));
     // Create a document with the ID we want to find
     var queryDoc = new QueryDocument { { "_id", oid } };
     // Query the db for a document with the required ID
     var user2 = mdb.GetCollection("userDatas").FindOne(queryDoc);
     var profileDic = getAllProfilesDictionary();
     User wuser = mapMongoUser(user2, profileDic);
     return wuser;
 }
开发者ID:pabloelustondo,项目名称:WellCastServer,代码行数:12,代码来源:WellCastServerEngine.cs


示例13: TestBulkWriteCountsWithUpsert

        public void TestBulkWriteCountsWithUpsert()
        {
            _collection.Drop();
            var id = new BsonObjectId(ObjectId.GenerateNewId());

            var bulk = _collection.InitializeOrderedBulkOperation();
            bulk.Find(Query.EQ("_id", id)).Upsert().UpdateOne(Update.Set("x", 2));
            bulk.Find(Query.EQ("_id", id)).Upsert().UpdateOne(Update.Set("x", 2));
            bulk.Find(Query.EQ("_id", id)).UpdateOne(Update.Set("x", 3));
            var result = bulk.Execute();

            Assert.AreEqual(0, result.DeletedCount);
            Assert.AreEqual(0, result.InsertedCount);
            if (_primary.Supports(FeatureId.WriteCommands))
            {
                Assert.AreEqual(true, result.IsModifiedCountAvailable);
                Assert.AreEqual(1, result.ModifiedCount);
            }
            else
            {
                Assert.AreEqual(false, result.IsModifiedCountAvailable);
                Assert.Throws<NotSupportedException>(() => { var _ = result.ModifiedCount; });
            }
            Assert.AreEqual(3, result.RequestCount);
            Assert.AreEqual(2, result.MatchedCount);
            Assert.AreEqual(1, result.Upserts.Count);
            Assert.AreEqual(0, result.Upserts.First().Index);
            Assert.AreEqual(id, result.Upserts.First().Id);
        }
开发者ID:p3p3pp3,项目名称:mongo-csharp-driver-for-tokumx,代码行数:29,代码来源:MongoCollectionTests.cs


示例14: TestClass

 public TestClass(
     BsonObjectId value
 )
 {
     this.B = value;
     this.V = value;
 }
开发者ID:vshlos,项目名称:mongo-csharp-driver,代码行数:7,代码来源:BsonValueSerializerTests.cs


示例15: DeserializeProperty

 public void DeserializeProperty(
     BsonReader bsonReader,
     object obj,
     BsonPropertyMap propertyMap
 )
 {
     var bsonType = bsonReader.PeekBsonType();
     BsonObjectId value;
     if (bsonType == BsonType.Null) {
         bsonReader.ReadNull(propertyMap.ElementName);
         value = null;
     } else {
         int timestamp;
         long machinePidIncrement;
         bsonReader.ReadObjectId(propertyMap.ElementName, out timestamp, out machinePidIncrement);
         value = new BsonObjectId(timestamp, machinePidIncrement);
     }
     propertyMap.Setter(obj, value);
 }
开发者ID:abolibibelot,项目名称:mongo-csharp-driver,代码行数:19,代码来源:BsonValuePropertySerializers.cs


示例16: TestConvertToObjectViaJson

        public void TestConvertToObjectViaJson()
        {
            const string id = "51ca5da43d1b18cc15000000";
            const string name = "Luke Skywalker";
            const string gender = "Male";
            DateTime dateOfBirth = new DateTime(1977, 5, 25);

            BsonDocument document = new BsonDocument();
            document["_id"] = new BsonObjectId(new ObjectId(id));
            document["Name"] = new BsonString(name);
            document["Gender"] = new BsonString(gender);
            document["DateOfBirth"] = new BsonDateTime(dateOfBirth);
            ////document["DateOfDeath"] = Deliberately excluded

            Person person = BsonConverter.ConvertToObjectViaJson<Person>(document);
            Assert.AreEqual(person.Id, id);
            Assert.AreEqual(person.Name, name);
            Assert.AreEqual(person.Gender, gender);
            Assert.AreEqual(person.DateOfBirth.ToUniversalTime(), dateOfBirth.ToUniversalTime());
            Assert.IsNull(person.DateOfDeath);
        }
开发者ID:cgavieta,项目名称:WORKPAC2016-poc,代码行数:21,代码来源:BsonConverterTests.cs


示例17: EquivalentToObjectIdNotMatch

 public void EquivalentToObjectIdNotMatch()
 {
     BsonValue val1 = new BsonObjectId(new ObjectId("5212e9f1d7eaf01e8c6b8b40"));
     BsonValue val2 = new BsonObjectId(new ObjectId("5212e9f1d7eaf01e8c6b8b41"));
     Assert.IsFalse(val1.EquivalentTo(val2));
 }
开发者ID:cgavieta,项目名称:WORKPAC2016-poc,代码行数:6,代码来源:BsonValueExtensionsTests.cs


示例18: TestBulkWriteCountsWithUpsert

        public void TestBulkWriteCountsWithUpsert()
        {
            using (_server.RequestStart(null, ReadPreference.Primary))
            {
                var serverInstance = _server.RequestConnection.ServerInstance;

                _collection.Drop();
                var id = new BsonObjectId(ObjectId.GenerateNewId());
                var result = _collection.BulkWrite(
                    new BulkWriteArgs { IsOrdered = true, WriteConcern = WriteConcern.Acknowledged },
                    new UpdateRequest(Query.EQ("_id", id), Update.Set("x", 2)) { IsUpsert = true },
                    new UpdateRequest(Query.EQ("_id", id), Update.Set("x", 2)) { IsUpsert = true },
                    new UpdateRequest(Query.EQ("_id", id), Update.Set("x", 3)));

                var expectedModifiedCount = 1;
                if (serverInstance.BuildInfo.Version < new Version(2, 5, 5))
                {
                    expectedModifiedCount = 2;
                }

                Assert.AreEqual(0, result.DeletedCount);
                Assert.AreEqual(0, result.InsertedCount);
                Assert.AreEqual(expectedModifiedCount, result.ModifiedCount);
                Assert.AreEqual(3, result.RequestCount);
                Assert.AreEqual(2, result.UpdatedCount);
                Assert.AreEqual(1, result.Upserts.Count);
                Assert.AreEqual(0, result.Upserts.First().Index);
                Assert.AreEqual(id, result.Upserts.First().Id);
            }
        }
开发者ID:KeithLee208,项目名称:mongo-csharp-driver,代码行数:30,代码来源:MongoCollectionTests.cs


示例19: RemoveById

 public void RemoveById(BsonObjectId userId)
 {
     users.Remove(Query.EQ("_id", userId));
 }
开发者ID:tkellogg,项目名称:TweetDigest,代码行数:4,代码来源:UserRepository.cs


示例20: TestUpsertInsert

        public void TestUpsertInsert()
        {
            _collection.Drop();
            var id = new BsonObjectId(ObjectId.GenerateNewId());
            var result = _collection.Update(Query.EQ("_id", id), Update.Set("x", 2), UpdateFlags.Upsert);

            Assert.AreEqual(true, result.Ok);
            Assert.AreEqual(null, result.Code);
            Assert.AreEqual(null, result.LastErrorMessage);
            Assert.AreEqual(1, result.DocumentsAffected);
            Assert.AreEqual(false, result.UpdatedExisting);
            Assert.AreEqual(id, result.Upserted);

            var document = _collection.FindOne();
            Assert.AreEqual(2, document["x"].AsInt32);
            Assert.AreEqual(1, _collection.Count());
        }
开发者ID:KeithLee208,项目名称:mongo-csharp-driver,代码行数:17,代码来源:MongoCollectionTests.cs



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


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
C# BsonReader类代码示例发布时间:2022-05-24
下一篇:
C# BsonJavaScript类代码示例发布时间:2022-05-24
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap