MongoDB Udate 查询在 C# 中使用类对象?

发布于 2024-12-22 14:20:56 字数 556 浏览 5 评论 0原文

我将类对象存储在 mongodb 中,如下所示,

try
{
    Sample risk = new Sample();
    risk.Name = "ABC";
    risk.Enable = true;
    risk.Sender = "IBM";
    risk.Target = "CITI";
    MongoServer server = MongoServer.Create("mongodb://localhost");
    MongoDatabase db = server.GetDatabase("DATABASE");
    db.GetCollection<StockQuote>("SMAPLETABLE").Insert(risk);
}
catch (Exception e)
{
    MessageBox.Show("Error");
}

现在我想更新相同的类实例,例如 db.GetCollection("SMAPLETABLE").Insert(risk); 我该怎么做一。

I am storing class object in mongodb like below,

try
{
    Sample risk = new Sample();
    risk.Name = "ABC";
    risk.Enable = true;
    risk.Sender = "IBM";
    risk.Target = "CITI";
    MongoServer server = MongoServer.Create("mongodb://localhost");
    MongoDatabase db = server.GetDatabase("DATABASE");
    db.GetCollection<StockQuote>("SMAPLETABLE").Insert(risk);
}
catch (Exception e)
{
    MessageBox.Show("Error");
}

now i want to to update that same class instance like db.GetCollection<StockQuote>("SMAPLETABLE").Insert(risk); how can i do this one.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

风吹过旳痕迹 2024-12-29 14:20:56

更新文档有两种方法:

1.通过 Save 方法更新整个文档

db.GetCollection<StockQuote>("SMAPLETABLE").Save(risk);

2.通过 原子更新:更新部分文档。例如,如果您需要使用 NameABC”更新文档的 Enable 字段:

db.GetCollection<StockQuote>("SMAPLETABLE").Update(
                                        Query.EQ("Name", "ABC"),
                                        Update.Set("Enable", false));

一些注意事项:

  1. 通过原子更新,您可以避免并发问题。原子更新就像一个文档中的事务。

There is two approaches two update document:

1.Via Save method and update etire document

db.GetCollection<StockQuote>("SMAPLETABLE").Save(risk);

2.Via atomic update: update part of document. For example if you need update Enable field of document with Name "ABC":

db.GetCollection<StockQuote>("SMAPLETABLE").Update(
                                        Query.EQ("Name", "ABC"),
                                        Update.Set("Enable", false));

Some notes:

  1. With atomic updates you can avoid concurrency issues. Atomic update it is like transaction within one document.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文