现在,我正在为每个请求打开一个集合:
即:
app.get('/route', function (req, res) {
db.collection('user', function (err, collection) {
collection.find(blah) // do something
app.get('/route2', function (req, res) {
db.collection('user', function (err, collection) {
collection.find(foo) // do something
app.get('/route3', function (req, res) {
db.collection('user', function (err, collection) {
collection.find(bar) // do something这是不正确的吗?我想我应该把'user‘集合保存到一个变量中,而不是每次请求都得到它。
谢谢。
发布于 2011-03-28 04:13:28
您可以拥有一个变量collection并使用它:
db.collection('user', function (err, collection) {
app.get('/route', function (req, res) {
collection.find(blah) // do something
}
app.get('/route2', function (req, res) {
collection.find(foo) // do something
}
app.get('/route3', function (req, res) {
collection.find(bar) // do something
}
}或者您可以使用一些模块来简化这些操作(Mongoose、Mongoose……)
https://stackoverflow.com/questions/5447702
复制相似问题