我目前正在将我的代码升级到MongoDB C#驱动程序2.0,并且我在升级代码以更新文档时遇到问题.
使用旧版本,我能够做到这样的事情:
MyType myObject; // passed in
var collection = _database.GetCollection<MyType>("myTypes");
var result = collection.Save(myObject);
Run Code Online (Sandbox Code Playgroud)
我很难在新版本中找到一种方法.我找到了一些更新单个字段的例子
var filter = Builders<MyType>.Filter.Eq(s => s.Id, id);
var update = Builders<MyType>.Update.Set(s => s.Description, description);
var result = await collection.UpdateOneAsync(filter, update);
Run Code Online (Sandbox Code Playgroud)
我想用旧方法更新所有字段,方法是Save.
有任何想法吗 ?
非常感谢
MongoDB的界面与前一个界面完全不同. 在这里,您可以看到官方文档,其中包含有关如何搜索,插入和更新的一些示例,但有关upserts的内容呢?
meta的想法:我试图在谷歌和SO上搜索但很多资源都是指旧界面.也许创建一个MongoLegacy标签会很好.
我正在构建的页面在很大程度上取决于AJAX.基本上,只有一个"页面",每个数据传输都通过AJAX处理.由于浏览器端的过度优化缓存导致奇怪的问题(数据未重新加载),我必须使用POST执行所有请求(也读取) - 强制重新加载.
现在我想阻止页面反对CSRF.使用表单提交,使用Html.AntiForgeryToken()工作整齐,但在AJAX请求中,我想我将不得不手动附加令牌?有没有开箱即用的东西?
我目前的尝试看起来像这样:
我很想重复使用现有的魔法.但是,HtmlHelper.GetAntiForgeryTokenAndSetCookie是私有的,我不想在MVC中乱搞.另一种选择是写一个类似的扩展名
public static string PlainAntiForgeryToken(this HtmlHelper helper)
{
// extract the actual field value from the hidden input
return helper.AntiForgeryToken().DoSomeHackyStringActions();
}
Run Code Online (Sandbox Code Playgroud)
这有点hacky并留下未解决的更大问题:如何验证该令牌?默认验证实现是内部的,并且使用表单字段进行硬编码.我尝试写一个稍微修改过ValidateAntiForgeryTokenAttribute,但它使用的AntiForgeryDataSerializer是私有的,我真的不想复制它.
在这一点上,似乎更容易想出一个自己开发的解决方案,但这确实是重复的代码.
有什么建议如何以聪明的方式做到这一点?我错过了一些完全明显的东西吗
我正在尝试从Mongo 2.0驱动程序初始化MongoClient,如下所示:
MongoClientSettings settings = new MongoClientSettings();
settings.WaitQueueSize = int.MaxValue;
settings.WaitQueueuTimeout = new TimeSpan(0,2,0);
settings.MinConnectionPoolSize = 1;
settings.MaxConnectionPoolSize = 25;
settings.Server = new MongoServerAddress("mongodb://localhost");
client = new MongoClient(settings)
Run Code Online (Sandbox Code Playgroud)
但是,当我现在尝试使用此代码插入文档时:
db = client.GetDatabase("local");
col = db.GetCollection<BsonDocument>(collectionName);
col.InsertOneAsync(new BsonDocument().Add(new BsonElement("id",BsonValue.Create(1)))).Wait();
Run Code Online (Sandbox Code Playgroud)
它没有做任何事情.它没有插入,也没有错误消息(虽然一段时间后输出中出现System.Timeout的第一次机会异常).如果我初始化客户端
client = new MongoClient("mongodb://localhost")
Run Code Online (Sandbox Code Playgroud)
它确实有效,并按预期上传文档.
我希望客户端能够处理非常高的写入吞吐量,所以我首先尝试了这些设置.我是否设置了一些错误的设置或是否存在其他问题?
编辑:经过一些更多的测试,它确实是我得到的System.Timeout异常.
我有一个存储以下数据的类:
public class User
{
public ObjectId _id { get; set; }
public string Name { get; set; }
public string Pass { get; set; }
public Dictionary<string, Tuple<string, string>> Quests { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
新用户的实例化如下:
await collection.InsertOneAsync(new User { Name = u, Pass = p,
Quests = new Dictionary<string,Tuple<string,string>>() });
Run Code Online (Sandbox Code Playgroud)
我知道如何从创建的文档中查找和提取信息,但不知道如何推送和保存对文档的更改。大多数在线答案都是针对旧的 Mongodb C# 驱动程序,因此诸如Query或 之类的东西.save()不存在,或者我没有将正确的包包含到我的程序中。
我希望能够向词典添加和删除条目,然后保存对文档的更改。有什么建议么?
将Mongodb 3.0.1集群到两台机器上之后.在从属模式下,当我运行此命令"show dbs"时,它显示"not master"错误.
这是错误:
E QUERY Error: listDatabases failed:
{ "note" : "from execCommand", "ok" : 0, "errmsg" : "not master" }
at Error (<anonymous>)
Run Code Online (Sandbox Code Playgroud)
请告诉我,我怎样才能克服这个问题.
通过 terraform 创建的 Kubernetes pod 没有/var/run/secrets文件夹,但根据hello-minikube教程创建的 pod 有-为什么会这样,我该如何解决?
动机:我需要 traefik 才能与 k8s 集群通信。
我已经设置了一个带有 minikube 的本地 Kubernetes 集群,并设置了 terraform 以使用该集群。
要设置traefik,你需要创建一个Ingress和Deployment它们尚未被terraform支持。根据该问题中发布的解决方法,我使用一个更简单的模块通过 terraform 执行 yaml 文件:
# A tf-module that can create Kubernetes resources from YAML file descriptions.
variable "name" {}
variable "file_name" { }
resource "null_resource" "kubernetes_resource" {
triggers {
configuration = "${var.file_name}"
}
provisioner "local-exec" {
command = "kubectl apply -f ${var.file_name}"
}
}
Run Code Online (Sandbox Code Playgroud)
以这种方式创建的资源在 …
我需要从“followingList.username”中获取所有用户名并与帖子的用户名进行比较,如果有任何匹配需要将该用户名添加到数组中。
Person Model
{
"_id" : ObjectId("554f20f5c90d3c7ed42303e1"),
"username" : "fatihyildizhan",
"followingList" : [
{
"_id" : ObjectId("55505b6ca515860cbcf7901d"),
"username" : "gumusluk",
"avatar" : "avatar.png"
},
{
"_id" : ObjectId("58505b6ca515860cbcf7901d"),
"username" : "yalikavak",
"avatar" : "avatar.png"
},
{
"_id" : ObjectId("58305b6ca515860cbcf7901d"),
"username" : "gumbet",
"avatar" : "avatar.png"
}
]
}
Post Model
{
"_id" : ObjectId("554f2df2a388R4b425b89833"),
"username" : "yalikavak",
"category" : "Summer",
"text" : "blue voyage with yacht"
},
{
"_id" : ObjectId("554f2df2a388P4b425b89833"),
"username" : "yalikavak",
"category" : "Winter",
"text" : "is …Run Code Online (Sandbox Code Playgroud) mongodb mongodb-query mongodb-csharp-2.0 mongodb-.net-driver
我想查询所有用户组的 ID,其中管理员的 ID="25160228446835585906563830293" 或用户的 ID ="25160228446835585906563830293"。
这是 java obj 中的 hashmap 键值对 hashmap<String,Date>
"25160228446835585906563830293" : ISODate("2013-03-26T04:51:36.731Z")
{ "_id" : ObjectId("51512958849ca4748271c640"),
"_class" : "com.pcd.app.model.UserGroup",
"groupName" : "sdfsadfsad",
"privacyType" : "PRIVACY_OPEN",
"approvalType" : "MEMBER_APPROVAL",
"groupDescription" : "test",
"admins" : {
"25160228446835585906563830293" : ISODate("2013-03-26T04:51:36.731Z"),
"25160228446835585906563830294" : ISODate("2013-03-26T04:51:36.731Z"),
"25160228446835585906563830295" : ISODate("2013-03-26T04:51:36.731Z")
},
"users" : {
"25160228446835585906563830296" : ISODate("2013-03-26T04:51:36.731Z")
}
}
Run Code Online (Sandbox Code Playgroud)