我有一本字典,我想用它来更新 mongodb 记录。我正在使用一个简单的 foreach 来迭代字典并构造一个 UpdateDefinition 对象。问题是我无法初始化一个空的 UpdateDefinition 对象,因此我被迫使用现有的键值初始化 UpdateDefinition:
IDictionary<string, object> document = GetDocument();
string firstKey = document.Keys.First();
var update = Builders<BsonDocument>.Update.Set(firstKey, document[firstKey]);
foreach (var key in document.Keys)
{
update = update.Set(key, document[key]);
}
Run Code Online (Sandbox Code Playgroud)
这太可怕了。FilterDefinition 有一个空的 Filter 非常适合此目的。是否有类似的构建迭代 UpdateDefinitions 的方法?
我有以下应用程序:
A - 应用程序 A 是托管在 IIS 7.5 中的 .net wcf 服务,使用在 .net 4.5 中编译的 c# mongodriver 2.2.4
B - 应用程序 B 是使用 mongodriver 1.11 在 .net 3.5 中编译的 Windows 服务应用程序
这两种服务是相似的,服务 B 是为遗留系统维护的,服务 A 正在演变。
这两个应用程序都托管在相同的服务器中。(Windows Standard 2008 R2) 这个应用程序已经完美运行了 1 年多,但自 2016 年 6 月 24 日起,应用程序 A (WCF) 在打开与 Mongo Server 的新连接时开始出现奇怪的行为:
Run Code Online (Sandbox Code Playgroud)> System.TimeoutException: A timeout occured after 30000ms selecting a > server using CompositeServerSelector{ Selectors = > ReadPreferenceServerSelector{ ReadPreference = { Mode = Primary, > …
我想在 MongoDb 查询中获得第一个前 10 个结果
为此我尝试了这样的事情
return _users.Collection.FindAll().Limit(10).ToList();
Run Code Online (Sandbox Code Playgroud)
但这不起作用,然后我根据大多数资源尝试了以下操作,但这也不起作用
return _users.Collection.Aggregate([{ "$limit": 10 }]).ToList();
Run Code Online (Sandbox Code Playgroud) 我在 mongodb 中有大量文档集合,只想获取 _id 列表。Mongodb 查询是db.getCollection('Documents').find({},{_id : 0, _id: 1}). 但是在 C# 查询中
IMongoCollection<T> Collection { get; set; }
...
List<BsonDocument> mongoResult = this.Collection.FindAsync(FilterDefinition<T>.Empty, new FindOptions<T, BsonDocument>() { Projection = "{ _id: 0, _id: 1 }" }).Result.ToList();
Run Code Online (Sandbox Code Playgroud)
扔exeptionInvalidOperationException: Duplicate element name '_id'.
我想只有_id列表,不再需要其他的Fileds。文件可能有不同的结构,并排除所有其他手动困难的文件。
什么 C# 查询对应于指定的 mongodb 查询db.getCollection('Documents').find({},{_id : 0, _id: 1}?
更新:不提供与从服务器查询大量数据相关的解决方案,例如
this.Collection.Find(d => true).Project(d => d.Id).ToListAsync().Result;
Run Code Online (Sandbox Code Playgroud) Using MongoDB C# Driver 2.3, what's the strongly typed version of the following code?
database.GetCollection<GamePlay>()
.Indexes
.CreateOne("{ \"PartiesInGame.Username\": 1 }");
Run Code Online (Sandbox Code Playgroud)
GamePlay有一个IEnumerable<PartyInGame>,其中PartyInGame有一个public string Username { get; set; }
我对 Mongodb 和 C# 驱动程序完全陌生。
开发是在 Ubuntu 14.04 上使用 Monodevelop 完成的,Mongodb 的版本是 3.2.10 :
目前我的代码有一个 POCO 如下:
public class User
{
public String Name { get; set;}
public DateTime LastModifiedAt { get; set;}
public DateTime LastSyncedAt { get; set;}
public User ()
{
}
}
Run Code Online (Sandbox Code Playgroud)
已经能够创建集合并添加用户。
如何查找 LastModifiedAt 时间戳大于 LastSyncedAt 时间戳的用户?进行了一些搜索,但未能找到答案。
任何建议都会有很大的帮助
谢谢
我是 MongoDb 的新手,我正在尝试将嵌套在父文档中的文档数组序列化为特定的类对象。
我的文档对象如下所示:
Project
{
"_id" : ObjectId("589da20997b2ae4608c99173"),
// ... additional fields
"Products" : [
{
"_id" : ObjectId("56971340f3a4990d2c199853"),
"ProductId" : null,
"Name" : null,
"Description" : null,
// ... quite a few more properties
"Properties" : null
}
],
"CreatorUserId" : LUUID("af955555-5555-5555-5555-f1fc4bb7cfae")
}
Run Code Online (Sandbox Code Playgroud)
所以基本上它是一个名为Project包含嵌套文档数组的文档Product。
以下是当前课程的样子:
public class Project : BaseClasses {
public ObjectId ParentCreatorUserIdProject { get; set; }
// ... additional Fields
public IEnumerable<IProjectItem> ProjectItems { get; set; }
//public IEnumerable<IProductDetails> Products { get; …Run Code Online (Sandbox Code Playgroud) 我需要使用 IBsonSerializer 实现自定义序列化器。
这就是我所做的:
internal class MyCustomDateTimeSerializer : IBsonSerializer
{
public object Deserialize(BsonDeserializationContext context,
BsonDeserializationArgs args)
{
// Deserialization logic
}
public void Serialize(BsonSerializationContext context,
BsonSerializationArgs args, object value)
{
// Serialization logic
}
public Type ValueType => typeof(DateTime);
}
Run Code Online (Sandbox Code Playgroud)
然后在 BsonSerializerAttribute 中使用它:
[BsonSerializer(typeof(MyCustomDateTimeSerializer))]
Run Code Online (Sandbox Code Playgroud)
我的问题是我想序列化/反序列化 DateTime 和 Nullable DateTime。
我的 CustomSerializer 的 ValueType 设置为 typeof(DateTime),因此我得到如下异常:
序列化程序的值类型为 System.DateTime 且与成员类型 System.Nullable`1[[System.DateTime..
我没有找到这个问题的任何解决方案。当然,我可以为 Nullable DateTime 和 DateTime 创建两个不同的类,但也许还有另一种选择?
我正在尝试连接到 MongoDb 数据库。
通过连接字符串进行身份验证正在工作:
public MongoDbContext(AppSettings appSettings)
{
var connectionString = "mongodb://myUsername:myPassword@myDomain.com:27017/myDatabaseName";
_client = new MongoClient(connectionString);
// ...
}
Run Code Online (Sandbox Code Playgroud)
MongoClientSettings 的身份验证不起作用:
public MongoDbContext(AppSettings appSettings)
{
var credentials = MongoCredential.CreateMongoCRCredential(databaseName: "myDatabaseName", username: "myUsername", password: "myPassword");
var server = new MongoServerAddress(host: "myDomain.com", port: 27017);
var mongoClientSettings = new MongoClientSettings
{
Credential = credentials,
Server = server,
ConnectionMode = ConnectionMode.Standalone,
ServerSelectionTimeout = TimeSpan.FromSeconds(3)
}; …Run Code Online (Sandbox Code Playgroud) 由于工作中的环境限制,我正在 MongoDB 中实现一个粗略的事件源存储。我正在尝试从 Mongo 获取列表,IClientEvents如下所示:
var events = await _db.GetCollection<IClientEvent>("ClientEvents").FindAsync(c => c.ClientId == clientId);
Run Code Online (Sandbox Code Playgroud)
当我运行上述存储库方法时,出现以下异常:
Message: System.InvalidOperationException : {document}.ClientId is not supported.
Run Code Online (Sandbox Code Playgroud)
接口IClientEvent定义为:
public interface IClientEvent
{
Guid Id { get; set; }
long TimeStamp { get; set; }
Guid ClientId { get; set; }
}
public class ClientChangedEvent : IClientEvent
{
public Guid Id { get; set; }
public long TimeStamp { get; set; }
public Guid ClientId { get; set; }
public IEnumerable<Change> Changes; …Run Code Online (Sandbox Code Playgroud)