I’m using mongodb go driver for my application and I’m confused on querying and returning meta with the results:
1st option
collection.countDocuments(ctx, filter)
// add more filter
collection.Find(ctx, filter, limit(200))
vs
2nd option
collection.Aggregate(
mongo.Pipeline{
// filter first
// then sort
// do a facet:
// 1st pipeline: count
// 2nd pipeline: apply added filter, limit
}
)
I have attempted to translate it into the mongodb commands and run them on my shell
db.collection.explain().aggregate(...)
but I’m still confused:
- There’s a lot of slow query message on my mongodb log, does that certainly mean my commands are slow? I have applied index and it shows IXSCAN
{"t":{"$date":"2021-02-18T14:03:17.180+00:00"},"s":"I", "c":"COMMAND", "id":51803, "ctx":"conn3","msg":"Slow query","attr":{"type":"command","ns":"TaskManagement.task","appName":"MongoDB Shell","command":{"aggregate":"task","pipeline":[{"$match":{"$or":[{"client_time_utc":{"$lt":{"$date":"2020-08-17T19:20:30.450Z"}}},{"$and":[{"client_time_utc":{"$date":"2020-08-17T19:20:30.450Z"}},{"_id":{"$lt":"1beecc71-c550-4ff8-8644-891e61e4e8a2"}}]}],"org_id":"2323"}},{"$facet":{"count":[{"$count":"value"}],"data":[{"$sort":{"client_time_utc":-1.0,"_id":-1.0}},{"$limit":5.0}]}}],"explain":true,"cursor":{},"lsid":{"id":{"$uuid":"3ec30c03-1de1-47aa-b912-d47878c32e88"}},"$db":"TaskManagement"},"planSummary":"IXSCAN { _id: 1 }, IXSCAN { client_time_utc: -1, _id: -1 }","numYields":0,"reslen":2714,"locks":{"ParallelBatchWriterMode":{"acquireCount":{"r":1}},"ReplicationStateTransition":{"acquireCount":{"w":2}},"Global":{"acquireCount":{"r":2}},"Database":{"acquireCount":{"r":2}},"Collection":{"acquireCount":{"r":2}},"Mutex":{"acquireCount":{"r":2}}},"storage":{},"protocol":"op_msg","durationMillis":1}}
-
I’m not sure what to make use of rejectedPlan (explain output). Does that mean I have to modify my query?
-
How do I know which one is faster for
count and then find
vsaggregate
?
Thank you