帮助通用 LINQ 插入方法 (Norm/MongoDB)
我正在尝试编写一个通用方法来将对象作为嵌入文档插入。下面的代码已经可以工作,但我需要创建一个通用方法,可以将其放入存储库中并从控制器调用。我需要传入根 ID 和需要插入的子文档对象。
class Post
{
...
public IList<Comment> Comments { get; set; }
}
using (var db = Mongo.Create(session.ConnectionString()))
{
IMongoCollection<Post> _collection = db.GetCollection<Post>("Post");
var comment = new Comment();
comment.InjectFrom(commentViewModel.comment);
// Use below to add *NEW* embedded document
_collection.UpdateOne(
new { _id = commentViewModel.Id },
new { Comments = M.AddToSet(comment) }
);
}
举个例子,这就是我用来检索文档的方法,我正在寻找类似于 insert:
// Controller
var session = new MongoSession();
var post = session.Single<Post>(c => c.Id == id);
//Repository
public T Single<T>(System.Linq.Expressions.Expression<Func<T, bool>> expression) where T : class, new()
{
T retval = default(T);
using (var db = Mongo.Create(_connectionString))
{
retval = db.GetCollection<T>().AsQueryable()
.Where(expression).SingleOrDefault();
}
return retval;
}
UPDATE
的内容,请注意,调用:
_collection.UpdateOne(
new { _id = commentViewModel.Id },
new { Comments = M.AddToSet(comment) }
)
可以满足我的需要,我只是希望能够做到这一点对类似内容(工作版本除外)的通用调用(由于缺乏更好的词):
//Controller
var session = new MongoSession();
session.AddSubDocument<>(new Post { Id = commentViewModel.Id }, new { Comments = M.AddToSet(comment) });
// Repository ==> NOT WORKING SAMPLE
public void AddSubDocument<X, U>(X matchDocument, U valueDocument)
{
using (var db = Mongo.Create(_connectionString))
{
db.GetCollection<X>().UpdateOne(matchDocument, valueDocument);
}
}
希望这更有意义。
谢谢你, 麦克风
I'm trying to write a generic method to inset an object as an embedded document. The below code works already but I need to make a generic method that can be put in a repository and called from the controller. I'd need to pass in the root ID and the sub-document object that needs to be inserted.
class Post
{
...
public IList<Comment> Comments { get; set; }
}
using (var db = Mongo.Create(session.ConnectionString()))
{
IMongoCollection<Post> _collection = db.GetCollection<Post>("Post");
var comment = new Comment();
comment.InjectFrom(commentViewModel.comment);
// Use below to add *NEW* embedded document
_collection.UpdateOne(
new { _id = commentViewModel.Id },
new { Comments = M.AddToSet(comment) }
);
}
As an example, this is what I use to retrieve a document, I'm looking for something similar to insert:
// Controller
var session = new MongoSession();
var post = session.Single<Post>(c => c.Id == id);
//Repository
public T Single<T>(System.Linq.Expressions.Expression<Func<T, bool>> expression) where T : class, new()
{
T retval = default(T);
using (var db = Mongo.Create(_connectionString))
{
retval = db.GetCollection<T>().AsQueryable()
.Where(expression).SingleOrDefault();
}
return retval;
}
UPDATE
Note that calling:
_collection.UpdateOne(
new { _id = commentViewModel.Id },
new { Comments = M.AddToSet(comment) }
)
does what I need, I just want to be able to make it a generic call (for lack of a better word) to something like this (except a working version):
//Controller
var session = new MongoSession();
session.AddSubDocument<>(new Post { Id = commentViewModel.Id }, new { Comments = M.AddToSet(comment) });
// Repository ==> NOT WORKING SAMPLE
public void AddSubDocument<X, U>(X matchDocument, U valueDocument)
{
using (var db = Mongo.Create(_connectionString))
{
db.GetCollection<X>().UpdateOne(matchDocument, valueDocument);
}
}
Hope that makes more sense.
Thank you,
Mike
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这应该有效
This should work