How to solve mongoDB related issue efficiently? [closed]
There is a lot of questions in monoDB tag about different problems, and I see that there is also some bunch of comments requesting similar data.
So provides How To Ask a Good Question but this is not related to MongoDB. Is there any good document which allow me to ask good and valuable question?
Solution 1:
There is a few rules that will help here to get good and valuable answer for MongoDB related question.
Please see below some common categories and steps that will help with gathering data which could help you to find a good answer faster.
Please attach all documents in text format as screenshot cannot be pasted into editor :-)
Basics - as mongoDB evolves some cool functions are available in higher version - to avoid confusion please give your current mongo version and let us know if this is standalone system, replica set or sharded environment
-
Questions about performance:
- please provide execution stats output - for queries:
db.collection.find({query}).explain("executionStats")
- that will give some statistics about query, indexes, for aggregation framework:db.collection.aggregate([{pieplineDatausedToExecuteAggregation},{explain:true}])
- hardware specs like ssd, ram size, cpus no and even clock speed if known
- please provide execution stats output - for queries:
Data manipulation - as queries are based on document structure please provide valid document dump (or even more than one) and ensure that
mocked
fields are reflecting fields in query, sometimes when trying to build query, we are unable to insert example documents as they structure isn't valid. Also if you are expecting certain result at the ned of process p - please attach expected example.Replica set/sharding issues - please add
rs.config()
/sh.status()
and remove host data (if sensitive)If you have driver/framework specific question - please display what was done and where are you have problem. Sometimes it is very hard to translate query from mongo shell syntax to driver/framework syntax - so if you could try to build that query in mongoDB shell - and having running example - please add it to question.
Examples:
RE:1
Using mongo 2.6 on windows laptop I am unable to have collection greater than 2GB, why?
RE:2
My query db.collection.find({isValid:true})
takes more than 30 seconds, please see explain output:
{
"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
I'm having trouble to get last 3 array elements from every record in my aggregation pipeline, mongo 3.2.3
my query: db.collection.aggregate([{aggregation pipeline}])
document schema:
{
"_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
I have issues with my replica set, data is not replicated to other server using mongo 3.2, below rs.config dump:
{
"_id" : "rs0",
"version" : 1,
"members" : [
{
"_id" : 1,
"host" : "mongodb0.example.net:27017"
}
]
}
RE:5
I have aggregation query in mongo and having trouble to get typed result from 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]
}
}
}
])
my c# code
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();
}
}