我曾经比较像这样的列表,但它在测试中返回false:
Assert.IsTrue(expected.SequenceEquals(actual));
Run Code Online (Sandbox Code Playgroud)
并尝试转换为json,它工作:
Assert.AreEqual(expected.ToJson(), actual.ToJson());
Run Code Online (Sandbox Code Playgroud)
价值似乎是平等的,可能会有什么不同?如何找出列表中的不同之处?
更新:
我的课:
public class Department
{
[BsonId]
public ObjectId Id { get; set; }
public string Name { get; set; }
public override string ToString()
{
return Id.ToString();
}
}
Run Code Online (Sandbox Code Playgroud) 我的同事和我在辩论中处于僵局,其他人的意见将不胜感激.
我们利用服务定位器模式和通用接口来抽象我们的所有数据访问,以便随着需求的变化,我们可以轻松地在不同的数据源之间进行交换.我们的调用代码没有指示数据的存储位置或方式.它只是通过服务注册表提供的服务访问数据.
当我们在对象上有DateTime字段并将其存储到MongoDB数据源时,我们正在讨论的问题就出现了.
我注意到的是,当我们在C#中有一个带有DateTime的对象时,它显示为正确的时间.当我们使用MongoVUE登录我们的MongoDB服务器来检查对象时,它会显示正确的时间.但是当我们检索对象时,DateTime现在是UTC.当将内存中的DateTime与从MongoDB数据源检索到的对象中的DateTime进行比较时,这显然会产生问题.
我知道Mongo在内部将DateTime存储为UTC时间.我甚至理解为什么它会在你打电话时返回UTC.
这是辩论开始的地方.
有人建议这只是一个美容问题,只是显示日期时的问题.因此,我们应该简单地在界面层中调用.ToLocalTime.我不同意并声称这危险地打破了我们在实现服务定位器模式时创建的抽象层.它还提出了与那些日期时间的交互问题,因为它涉及触发其他事件.
我所读到的其他地方是我们应该将我们的时间存储为字符串,特别是作为UTC格式的一些标准.以这种方式,接口层不知道或不关心DateTime的存储方式,也不知道我们的对象,因为每个数据源都以相同的方式存储该字符串.
我使用ISO 1806格式取得了成功,但我的同事认为这是一个"hacky"修复,并且使用.toLocalTime是处理这种情况的适当方法.
我对其他人对这个话题的看法很感兴趣.
提前感谢您的意见.
使用10gen mondgo db c#driver.I有以下课程
[BsonId]
public ObjectId Id { get; set; }
public int AttemptId { get; set; }
public int UserId { get; set; }
public int QId { get; set; }
public string UserInput { get; set; }
public string Feedback{ get; set; }
Run Code Online (Sandbox Code Playgroud)
默认情况下,如果我没有发送UserInput或Feedback(任何字符串)的值,mongodb将它们作为null.string.empty在插入或获取数据时是否有任何方法可以覆盖它.尝试过设置,[BsonDefaultValue("")]但这也没有用.
我用MongoDb C#Driver 1.4更新了我的项目,我的一个Lambda表达式不再工作了.
在我使用MongoDb C#Driver 1.3.1与Fluent Mongo支持Linq之前.
这是我的方法:
IQueryable<T> IBackend<T>.Get(System.Linq.Expressions.Expression<Func<T, bool>> expression)
{
return collection.AsQueryable<T>().Where(expression);
}
Run Code Online (Sandbox Code Playgroud)
这个lambda表达式有效:
var addedCustomer = repo.Get(c => c.FirstName == "Elwood").SingleOrDefault();
Run Code Online (Sandbox Code Playgroud)
这个现在引发了一个例外:
var updatedCustomer = repo.Get(c => c.Id == customer.Id).SingleOrDefault();
Run Code Online (Sandbox Code Playgroud)
抛出的异常消息:
Object reference not set to an instance of an object.
Run Code Online (Sandbox Code Playgroud)
这里更新是我的堆栈跟踪:
MongoDB.Bson.dll!MongoDB.Bson.Serialization.BsonClassMapSerializer.GetMemberSerializationInfo(string memberName) Line 253 + 0x3 bytes C#
MongoDB.Driver.dll!MongoDB.Driver.Linq.SelectQuery.GetSerializationInfoMember(MongoDB.Bson.Serialization.IBsonSerializer serializer, System.Linq.Expressions.MemberExpression memberExpression) Line 962 + 0xc bytes C#
MongoDB.Driver.dll!MongoDB.Driver.Linq.SelectQuery.GetSerializationInfo(MongoDB.Bson.Serialization.IBsonSerializer serializer, System.Linq.Expressions.Expression expression) Line 888 + 0xf bytes C#
MongoDB.Driver.dll!MongoDB.Driver.Linq.SelectQuery.GetSerializationInfo(System.Linq.Expressions.Expression expression) Line 880 + 0xf …Run Code Online (Sandbox Code Playgroud) MongoDB 2.4增加了一个新的"更新后限制数组中的元素数量"功能.这是如何通过shell使用它:
db.students.update(
{ _id: 1 },
{ $push:
{ scores:
{ $each :
[
{ attempt: 3, score: 7 },
{ attempt: 4, score: 4 }
],
$sort: { score: 1 },
$slice: -3
}
}
}
)
Run Code Online (Sandbox Code Playgroud)
如何使用MongoDB的C#-driver完成这项工作?
我有许多不可变的值类型类,例如EmailAddress,它确保任何非null实例有效.我希望"123@abc.com"在使用MongoDB C#Driver持久化时,将这些类型的对象的序列化控制为标准字符串表示().
我尝试过实现IBsonSerilizer它,但它只允许根级别的对象或数组.我能够用Json.NET实现适当的Json Serilization,我应该采用不同的方法吗?
如何在MongoShell中实现以下SQL?
Select TableA.* from TableA where TableA.FieldB in (select TableB.FieldValue from TableB)
Run Code Online (Sandbox Code Playgroud)
Mongo doc给出了一些例子
db.inventory.find( { qty: { $in: [ 5, 15 ] } } )
Run Code Online (Sandbox Code Playgroud)
我希望该数组是从另一个查询动态的.可能吗?
扩展我的问题
我有一个bot名字的集合
机器人收藏
{
"_id" : ObjectId("53266697c294991f57c36e42"),
"name" : "teoma"
}
Run Code Online (Sandbox Code Playgroud)
我有一个用户流量的集合,在该流量集合中,我有一个字段 useragent
userTraffic集合
{
"_id" : ObjectId("5325ee6efb91c0161cbe7b2c"),
"hosttype" : "http",
"useragent" : "Mediapartners-Google",
"is_crawler" : false,
"City" : "Mountain View",
"State" : "CA",
"Country" : "United States"
}
Run Code Online (Sandbox Code Playgroud)
我想选择其中useragent包含任何bot集合名称的所有用户流量文档
这就是我想出的
var botArray = db.bots.find({},{name:1, _id:0}).toArray()
db.Sessions.find({ …Run Code Online (Sandbox Code Playgroud) 如何在C#中访问MongoCursor属性.
我有以下代码行:
MongoCursor results = collection.Find(searchQuery).SetLimit(10).SetFields(
Fields.Include("name1","name", "_id"));
Run Code Online (Sandbox Code Playgroud)
MongoDB返回一个数组,每个数组都有两个属性:name和name1.在调试器的结果视图中,我可以看到一个数组,数组中的每个项都包含一个MongoDB.Bson.BsonDocument.
我想要一个点表示法访问数组中每个BsonDocument的属性.我怎样才能做到这一点.
需要一些帮助来创建一个通用的方法来按名称选择字段.
这样的事情:
T GetDocField<T>(string doc_Id, string fieldName)
Run Code Online (Sandbox Code Playgroud)
我得到的最好的是使用投影,它给我的文档只有想要的字段:
public T GetDocField<T>(string Doc_Id, string fieldName)
{
var value = DocCollection.Find(d => d.Id == Doc_Id)
.Project<T>(Builders<Doc>.Projection
.Include(new StringFieldDefinition<Doc>
(fieldName))).FirstOrDefaultAsync().Result;
Run Code Online (Sandbox Code Playgroud)
注意: 我正在使用新的c#驱动程序(2.0)
谢谢!!
我需要通过Mongo C#Driver设置分析级别.
Client.GetDatabase("test")返回在运行时IMongoDatabase解析的接口MongoDB.Driver.MongoDatabaseImpl.
根据MongoDB .NET Driver API Documentation MongoDatase类有一个SetProfilingLevel我无法在运行时强制转换的方法.
BTW我还安装了旧版驱动程序版本2.0.1,因为文档中说SetProfilingLevel方法就在其中.
mongodb ×9
c# ×6
.net ×2
bson ×1
c#-4.0 ×1
interface ×1
mongo-shell ×1
mstest ×1
unit-testing ×1