本文整理汇总了C#中BsonObjectId类的典型用法代码示例。如果您正苦于以下问题:C# BsonObjectId类的具体用法?C# BsonObjectId怎么用?C# BsonObjectId使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
BsonObjectId类属于命名空间,在下文中一共展示了BsonObjectId类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的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);
}
示例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;
}
示例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));
}
示例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);
}
示例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);
}
示例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()));
}
示例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);
}
示例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
}
示例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);
}
}
示例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);
}
}
示例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));
}
示例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;
}
示例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);
}
示例14: TestClass
public TestClass(
BsonObjectId value
)
{
this.B = value;
this.V = value;
}
示例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);
}