问题
I'm wondering what the most efficient way to manually abort a running query with the official node client is. In my current code I am creating a new DB connection for each remote client and then simply closing those connections if the client disconnects early, which works well but seems like it might be a wasteful way of doing things (edit: indeed it is; detailed here). I imagine if it's possible to directly access the mongodb client connection pool and close specific connections rather than a whole database this might be more performant?
The mongo server is querying several large geospatial collections concurrently, so releasing resources upon clients disconnecting is an important part of performance in this implementation. There will be quite some unnecessary requests coming in - people like to click around a lot on maps even when you are throttling them.
Contrived example of my current code:
var MongoClient = require('mongodb').MongoClient,
express = require("express"),
app = express();
app.get('/my-route', function(request, response) {
MongoClient.connect(connectionUri, function(err, db) {
if (err) throw err;
request.on("close", function() {
db.close(true, function(err, res) {
if (err) throw err;
});
});
// make async DB queries as normal and send response when done
// ...
});
});
来源:https://stackoverflow.com/questions/25734779/node-mongodb-native-manually-aborting-running-queries