我正在将我们的消息传递系统移动到MongoDB,并且很好奇对于各种统计数据采取什么方法,例如每个用户的消息数量等.在MS SQL数据库中,我有一个表,其中每个用户有不同的计数,并且它们通过触发器更新在相应的表上,所以我可以知道UserA有多少未读消息而没有调用昂贵的SELECT Count(*)操作.
是countMongoDB中功能还贵?我开始阅读有关map/reduce的内容,但我的网站负载很高,所以统计数据必须实时更新,我的理解是map/reduce是耗时的操作.
收集MongoDB中各种聚合计数的最佳(性能方面)方法是什么?
我已经读过这个问题并且不明白.是否有能力通过C#驱动程序执行任意mongodb shell脚本?
让我们假设我想在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但没有找到任何理由为什么它应该被转移.可以吗?
我正在尝试查询我的收藏品,但我不确定如何对其进行"追加" Query.And()
这是我创建Item文档的域模型:
public class Item
{
public ObjectId Id { get; set; }
public string ItemTypeTemplate { get; set; }
public string UsernameOwner { get; set; }
public IList<ItemAttribute> Attributes { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
将IList<ItemAttribute>根据收集的变化ItemTypeTemplate(某种查找关键项目的属性预先确定的名单)
以下是Item文档示例:
{
"_id" : ObjectId("5130f9a677e23b11503fee72"),
"ItemTypeTemplate" : "Tablet Screens",
//can be other types like "Batteries", etc.
//which would change the attributes list and values
"UsernameOwner" : "user032186511",
"Attributes" : [{
"AttributeName" : "Screen Size",
"AttributeValue" …Run Code Online (Sandbox Code Playgroud) 在我的MongoDB集合中,我有一个带有数组条目的文档.如何在C#中将这些数组值作为字符串数组获取?我可以让文档本身恢复正常,但我似乎无法获得数组值.这是我要做的事情:
QueryDocument findUser = new QueryDocument("_id" , id);
BsonDocument user = bsonCollection.FindOne(findUser);
Run Code Online (Sandbox Code Playgroud)
所以在这个user文档中,有一个我想要获取并解析为字符串数组的数组.该文档看起来像这样:
{
"firstname" : "jon",
"secondname" : "smith",
"loves" : ["this","that","other stuff"]
}
Run Code Online (Sandbox Code Playgroud) `
public class Student
{
public long StudentId {get; set;}
public string Fname {get; set;}
public string Lname {get; set;}
public List<ObjectId> CoursesList {get; set;}
public int IQ {get;set;}
}
public class Courses
{
[BsonId]
public ObjectId Id { get; set; }
public string CourseNumber{get; set;}
public string CourseName{get; set;}
}
Run Code Online (Sandbox Code Playgroud)
`
如何在Student对象的课程列表(第一次可能为null)中添加/附加courser Id
PS:我知道如何使用下面的命令设置字段.我希望它与上述问题类似
await StudentCollection.UpdateOneAsync(a => a.StudentId == studentId, Builders<Student>.Update.Set( a => a.IQ,90));
Run Code Online (Sandbox Code Playgroud) 我们最近将我们的Web应用程序升级到MongoDB C#Driver 2.0并部署到生产中.低于一定负载,应用程序运行正常.一旦生产服务器上的负载超过某个限制,应用程序的CPU立即降至0,大约30秒后,将多次记录此异常:
System.TimeoutException message: A timeout occured after 30000ms selecting a server using CompositeServerSelector{ Selectors = ReadPreferenceServerSelector{ ReadPreference = { Mode = Primary, TagSets = System.Collections.Generic.List`1[MongoDB.Driver.TagSet] } }, LatencyLimitingServerSelector{ AllowedLatencyRange = 00:00:00.0150000 } }. Client view of cluster state is { ClusterId : "1", Type : "Standalone", State : "Disconnected", Servers : [{ ServerId: "{ ClusterId : 1, EndPoint : "Unspecified/10.4.0.113:27017" }", EndPoint: "Unspecified/10.4.0.113:27017", State: "Disconnected", Type: "Unknown" }] }.
stack trace:
at MongoDB.Driver.Core.Clusters.Cluster.ThrowTimeoutException(IServerSelector selector, ClusterDescription description)
at MongoDB.Driver.Core.Clusters.Cluster.<WaitForDescriptionChangedAsync>d__18.MoveNext() …Run Code Online (Sandbox Code Playgroud) 我正在尝试从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 Foo
{
public string Id { get; set; }
public ICollection<FooBar> Bars { get; set; }
// ...
}
public class FooBar
{
public string BarId { get; set; }
// ...
}
Run Code Online (Sandbox Code Playgroud)
使用带司机LINQ风格的查询,我可以Find全部Foo包含一个FooBar BarId这样的:
var foos = await m_fooCollection.Find( f => f.Bars.Any( fb => fb.BarId == "123") ).ToListAsync();
Run Code Online (Sandbox Code Playgroud)
如何使用FilterDefinitionBuilder而不是内联LINQ来实现相同的查询Find?
c# mongodb mongodb-query mongodb-csharp-2.0 mongodb-.net-driver
是否有直接的方法来更新MongoDB中嵌套的实体数组.我MongoDB C# Driver用于从应用程序进行数据库调用.下面是一个例子:说我有一个Student集合,其中每个文档都有一个嵌套的数组,Course其中填充了一些必要的字段Course,它本身就是一个单独的集合,如:
{
"_id": "234dssfcv456",
"Name": "Jean Douglas",
"Age": 32,
"Courses":
[
{
"_id": "1234",
"Name": "Computer Science",
"Level": "Basic"
},
{
"_id": "3456",
"Name": "Bio Science",
"Level": "Intermediate"
}
]
}
Run Code Online (Sandbox Code Playgroud)
我知道我可以通过索引更新嵌套实体,但我不知道索引,而只知道嵌套Course对象Id.
db.College.Student.update(
{"Student._id": "234dssfcv456"},
{$set: {
"Student.$.Courses.1.Level": "Basic"
}}
Run Code Online (Sandbox Code Playgroud)
现在,我正在阅读整个嵌套的课程数组 - >在应用程序端进行修改 - >然后将整个数组传递给更新"Courses",并使用将要传递的现有数组替换现有数组.
但是在想,有没有办法可以用Id可用的方式更新数组中的一个实体.请建议.
***在相关问题部分的右侧,所有显示使用对象项的索引更新嵌套的对象数组,这对我来说是不可能的.