在mongodb的官方文档中,他们提到了upserts,所以编写一个upsert命令而不是:
if (_campaignRepo.Exists(camp))
{
_campaignRepo.DeleteByIdAndSystemId(camp);
}
_campaignRepo.Save(camp);
Run Code Online (Sandbox Code Playgroud)
如果可能的话,可以在db级别实现该逻辑的东西.那么如果有一个upsert的方法是什么?
我知道MongoDB不应该支持工作单元等.但我认为实现只存储意图(类似于标准)然后将它们提交给数据库的存储库会很好.否则,在存储库中的每个方法中,您都必须创建与DB的连接,然后将其关闭.如果我们将连接放置在某个BaseRepository类中的DB,那么我们将我们的存储库绑定到具体的数据库,并且测试存储库非常困难,以测试解决存储库的IoC.
在MongoDB中创建会话是个坏主意吗?有没有办法将连接逻辑与存储库分开?
以下是Rob Conery的一些代码.在每次请求时总是连接到您的数据库是一个好主意吗?什么是最佳做法?
还有一件事.想象一下,我想为集合提供索引.以前我在构造函数中做过,但是使用Rob的方法,这似乎不符合逻辑.
using Norm;
using Norm.Responses;
using Norm.Collections;
using Norm.Linq;
public class MongoSession {
private string _connectionString;
public MongoSession() {
//set this connection as you need. This is left here as an example, but you could, if you wanted,
_connectionString = "mongodb://127.0.0.1/MyDatabase?strict=false";
}
public void Delete<T>(System.Linq.Expressions.Expression<Func<T, bool>> expression) where T : class, new() {
//not efficient, NoRM should do this in a way that sends a single command to MongoDB.
var items = All<T>().Where(expression);
foreach (T item …Run Code Online (Sandbox Code Playgroud) 在阅读Microsoft文档时,我偶然发现了这样一个有趣的代码示例:
interface ISomeInterface
{...}
class SomeClass
{...}
class MyClass<T>
{
void SomeMethod(T t)
{
ISomeInterface obj1 = (ISomeInterface)t;//Compiles
SomeClass obj2 = (SomeClass)t; //Does not compile
}
}
Run Code Online (Sandbox Code Playgroud)
这意味着除非您有约束,否则可以显式地将通用转换为接口而不是类.好吧,我仍然无法理解决策背后的逻辑,因为接口和类类型转换都抛出异常,那么为什么只能防止这些异常中的一个呢?
BTW-有一种解决编译错误的方法,但这并没有消除我头脑中的逻辑混乱:
class MyOtherClass
{...}
class MyClass<T>
{
void SomeMethod(T t)
{
object temp = t;
MyOtherClass obj = (MyOtherClass)temp;
}
}
Run Code Online (Sandbox Code Playgroud) 使用官方C#驱动程序向mongo db上传时遇到问题.
public abstract class AggregateRoot
{
/// <summary>
/// All mongoDb documents must have an id, we specify it here
/// </summary>
protected AggregateRoot()
{
Id = ObjectId.GenerateNewId();
}
[BsonId]
public ObjectId Id { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
我的实体已经拥有了id-s但我必须创建mongo特定的ID才能工作,因为集合中的所有文档都应该有一个.现在我在我的系统中收到一个新的实体,生成一个新的Mongo Id,我得到的mongo无法更改文件旧异常的_id.有一些解决方法吗?
让我来描述一下设计.将作为文档存储的所有实体都继承自AggregateRoot,其中包含id生成.每个子文档都自动生成了id,我对此没有任何问题.引入AggregateRoot中的id是为了在从MongoCollection到List检索数据时纠正问题,并且引入了生成,因此id-s不同.现在我们可以移动id生成来保存方法,因为更新的新实体有一个新的id生成.但这意味着团队中的每个开发人员都不能忘记在存储库中生成id-s,这是有风险的.如果可能的话,忽略id而不是从mongo映射会更好,而根本就没有AggregateRoot类
我正在研究一个包含一些重要数据的项目.这意味着如果灯或服务器发生故障,我们不能丢失任何一个.我们正在使用MongoDB作为数据库.如果没有插入一个元素,我想确保在插入和回滚整个批处理之后我的数据在数据库中.我知道Mongo背后的哲学是我们不需要交易但是如何确保我的数据在插入后真的安全存储而不是发送到某个"黑洞".
我应该搜索一下吗?
我应该使用一些特定的mongoDB命令吗?
我是否应该使用分片,即使一台服务器足以满足
速度,如果灯光
熄灭,它也不能保证任何东西?
什么是最好的解决方案?
让我们假设我想在dateTime上查询mongo.我有两个C#变量代表开始和结束日期.
1){20.10.2011 00:00:00}
2){22.10.2011 00:00:00}
现在,BsonDateTime.Create(dateTime)也将它们转换为BSON DateTime:
1)2011-10-20T00:00:00 MongoDB.Bson.BsonDateTime
2)2011-10-22T00:00:00 MongoDB.Bson.BsonDateTime
这是创建dateTimes的代码(_value是一个字符串):
DateTime dateTime;
bool parsed = DateTime.TryParse(_value, out dateTime);
if (!parsed)
throw new FormatException("Wrong format for a query param");
return BsonDateTime.Create(dateTime);
Run Code Online (Sandbox Code Playgroud)
然后下一个代码构建查询:
private QueryComplete MakeQuery(string key, BsonValue value)
{
if (_separatorType == ">=")
return Query.GTE(key, value);
if (_separatorType == "<=")
return Query.LTE(key, value);
return Query.EQ(key, value);
}
Run Code Online (Sandbox Code Playgroud)
我确实在查询中得到了这么奇怪的值:
"Sessions.End" : { "$gte" : ISODate("2011-10-19T21:00:00Z"), "$lte" : ISODate("2011-10-21T21:00:00Z") },
Run Code Online (Sandbox Code Playgroud)
好吧,我谷歌ISODate但没有找到任何理由为什么它应该被转移.可以吗?
似乎有两种方法来实例化此处描述的WCF服务代理.我的问题是为什么要使用ChannelFactory来实例化WCF代理,这种方法有什么好处?
我遇到了对第二种选择有强烈意见的人,但我无法理解他们的明确论证
我希望能够在控制器上标记一个动作,从ajax调用和RenderAction调用.问题是这两个属性都衍生或实现了不同的抽象.一个出路是下一个:
[AjaxOnly]
PartialViewResult GetViewAjax(int foo) { return GetView(foo); }
[ChildActionOnly]
PartialViewResult GetView(int foo) { ... }
Run Code Online (Sandbox Code Playgroud)
但这根本不是很好.
我正在谈论的AjaxOnly属性是:
public sealed class AjaxOnlyAttribute : ActionFilterAttribute
{
#region Public members
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
if (filterContext == null)
throw new ArgumentNullException("filterContext");
if (filterContext.HttpContext.Request.Headers["X-Requested-With"] != "XMLHttpRequest")
filterContext.Result = new HttpNotFoundResult();
}
#endregion
}
Run Code Online (Sandbox Code Playgroud)
这种方法取自MVC3期货.一个重要的评论为什么条件不是filterContext.HttpContext.Request.IsAjaxRequest()由开发团队做出并说明如下:
// Dev10 #939671 - If this attribute is going to say AJAX *only*, then we need to check the header
// specifically, as otherwise clients …Run Code Online (Sandbox Code Playgroud) 假设您有一个id-s列表,其中最多可包含数千个id-s,用于数据库中的文档.获取这些文件的最佳方法是什么?
我应该为每个查询调用一个查询,还是应该指定一个巨大的OR查询?可能有一些我不知道的更好的方法
让我们说我们有一个关键值,它们的意义上是多态的.考虑下一个示例项目:
public class ToBeSerialized
{
[BsonId]
public ObjectId MongoId;
public IDictionary<string, BaseType> Dictionary;
}
public abstract class BaseType
{
}
public class Type1 : BaseType
{
public string Value1;
}
public class Type2:BaseType
{
public string Value1;
public string Value2;
}
internal class Program
{
public static void Main()
{
var objectToSave = new ToBeSerialized
{
MongoId = ObjectId.GenerateNewId(),
Dictionary = new Dictionary<string, BaseType>
{
{"OdEd1", new Type1 {Value1="value1"}},
{
"OdEd2",
new Type1 {Value1="value1"}
}
}
};
string connectionString = …Run Code Online (Sandbox Code Playgroud) c# ×9
mongodb ×7
asp.net-ajax ×1
asp.net-mvc ×1
batch-file ×1
datetime ×1
generics ×1
safe-mode ×1
wcf ×1