有一些规则可以帮助您获得 MongoDB 相关问题的良好且有价值的答案。
请参阅下面的一些常见类别和步骤,这些类别和步骤将有助于收集数据,从而帮助您更快地找到好的答案。
请以文本格式附上所有文档,因为屏幕截图无法粘贴到编辑器中:-)
基础知识 - 随着 mongoDB 的发展,一些很酷的功能在更高版本中可用 - 为了避免混淆,请提供您当前的 mongo 版本,并让我们知道这是独立系统、副本集还是分片环境
-
关于性能的问题:
- 请提供执行统计输出 - 用于查询:
db.collection.find({query}).explain("executionStats")
- 这将为聚合框架提供一些有关查询、索引的统计信息:db.collection.aggregate([{pieplineDatausedToExecuteAggregation},{explain:true}])
- 硬件规格,如 SSD、RAM 大小、CPU 编号,甚至时钟速度(如果已知)
数据操作 - 由于查询基于文档结构,请提供有效的文档转储(甚至多个)并确保mocked
字段反映查询中的字段,有时在尝试构建查询时,我们无法插入示例文档,因为它们的结构无效。另外,如果您期望在流程 p 的 ned 中得到某些结果 - 请附上预期的示例。
副本集/分片问题 - 请添加rs.config()
/ sh.status()
并删除主机数据(如果敏感)
如果您有驱动程序/框架的特定问题 - 请显示已完成的操作以及问题出在哪里。有时,将查询从 mongo shell 语法转换为驱动程序/框架语法非常困难 - 因此,如果您可以尝试在 mongoDB shell 中构建该查询 - 并且有正在运行的示例 - 请将其添加到问题中。
例子:
RE:1
在 Windows 笔记本电脑上使用 mongo 2.6 我无法收集大于 2GB 的数据,为什么?
RE:2
我的查询db.collection.find({isValid:true})
需要超过 30 秒,请参阅解释输出:
{
"queryPlanner" : {
"plannerVersion" : 1,
"namespace" : "test.collectionName",
"indexFilterSet" : false,
"parsedQuery" : {},
"winningPlan" : {
"stage" : "COLLSCAN",
"direction" : "forward"
},
"rejectedPlans" : []
},
"executionStats" : {
"executionSuccess" : true,
"nReturned" : 6,
"executionTimeMillis" : 0,
"totalKeysExamined" : 0,
"totalDocsExamined" : 6,
"executionStages" : {
"stage" : "COLLSCAN",
"nReturned" : 6,
"executionTimeMillisEstimate" : 0,
"works" : 8,
"advanced" : 6,
"needTime" : 1,
"needYield" : 0,
"saveState" : 0,
"restoreState" : 0,
"isEOF" : 1,
"invalidates" : 0,
"direction" : "forward",
"docsExamined" : 6
}
},
"serverInfo" : {
"host" : "greg",
"port" : 27017,
"version" : "3.3.6-229-ge533634",
"gitVersion" : "e533634d86aae9385d9bdd94e15d992c4c8de622"
},
"ok" : 1.0
}
RE:3
我无法从聚合管道 mongo 3.2.3 中的每条记录中获取最后 3 个数组元素
我的查询:db.collection.aggregate([{aggregation pipeline}])
文档架构:
{
"_id" : "john",
"items" : [{
"name" : "John",
"items" : [{
"school" : ObjectId("56de35ab520fc05b2fa3d5e4"),
"grad" : true
}
]
}, {
"name" : "John",
"items" : [{
"school" : ObjectId("56de35ab520fc05b2fa3d5e5"),
"grad" : true
}
]
}, {
"name" : "John",
"items" : [{
"school" : ObjectId("56de35ab520fc05b2fa3d5e6"),
"grad" : true
}
]
}, {
"name" : "John",
"items" : [{
"school" : ObjectId("56de35ab520fc05b2fa3d5e7"),
"grad" : true
}
]
}, {
"name" : "John",
"items" : [{
"school" : ObjectId("56de35ab520fc05b2fa3d5e8"),
"grad" : true
}
]
}
]
}
//expected result
{
"_id" : "john",
"items" : [{
"name" : "John",
"items" : [{
"school" : ObjectId("56de35ab520fc05b2fa3d5e4"),
"grad" : true
}
]
}, {
"name" : "John",
"items" : [{
"school" : ObjectId("56de35ab520fc05b2fa3d5e5"),
"grad" : true
}
]
}, {
"name" : "John",
"items" : [{
"school" : ObjectId("56de35ab520fc05b2fa3d5e6"),
"grad" : true
}
]
}
]
}
RE:4
我的副本集有问题,数据未使用 mongo 3.2 复制到其他服务器,位于 rs.config 转储下方:
{
"_id" : "rs0",
"version" : 1,
"members" : [
{
"_id" : 1,
"host" : "mongodb0.example.net:27017"
}
]
}
RE:5
我在 mongo 中有聚合查询,并且无法从中获取键入的结果c#
driver
startDate = new Date() // Current date
startDate.setDate(startDate.getDate() - 7) // Subtract 7 days
db.collection.aggregate([{
$match : {
LastUpdate : {
$gte : startDate
}
}
}, {
$sort : {
LastUpdate : -1
}
}, //sort data
{
$group : {
_id : "$Emp_ID",
documents : {
$push : "$$ROOT"
}
}
}, {
$project : {
_id : 1,
documents : {
$slice : ["$documents", 3]
}
}
}
])
我的 c# 代码
public static void Main()
{
var client = new MongoClient("mongodb://localhost:27017");
var database = client.GetDatabase("test");
var collection = database.GetCollection<InnerDocument>("irpunch");
var aggregationDocument = collection.Aggregate()
.Match(x=>x.LastUpdate> DateTime.Now.AddDays(-40))
.SortByDescending(x => x.LastUpdate)
.Group(BsonDocument.Parse("{ _id:'$Emp_ID', documents:{ '$push':'$$ROOT'}}"))
// how to get projection result as typed object ??
.Project(BsonDocument.Parse("{ _id:1, documents:{ $slice:['$documents', 3]}}")).ToList();
}
}
快乐提问!