MongoDB + NodeJs 简单查询不工作
MongoDB + NodeJs simple query not working
我正在尝试学习 MEAN 堆栈,现在我正在使用 NodeJS + MongoDB。
在我正在制作的这个 helloworld 项目中,我有以下要求:
"dependencies": {
"async": "0.9.0",
"mongodb": "2.0.27"
}
这个项目包括在数据库中进行简单的插入和查询。在这种情况下,我想通过导演姓名查询:
/*
* Finds all documents in the "movies" collection
* whose "director" field equals the given director,
* ordered by the movie's "title" field. See
* http://mongodb.github.io/node-mongodb-native/2.0/api/Cursor.html#sort
*/
exports.byDirector = function(db, director, callback) {
db.collection('movies').find({director: director}).toarray(function(error, docs){
callback(error, docs);
});
};
但是,在教程的测试中,我失败了,出现以下错误:
[14:27:33] Starting 'test'...
[14:27:33] Finished 'test' after 1.1 ms
1) dbInterface can query data by director:
TypeError: db.collection(...).find(...).sort(...).toarray is not a function
at Object.exports.byDirector
Tests failed!
我想我遇到了某种语法错误,但老实说我无法找出是什么。
我错过了什么?
我觉得你打错了,
toarray 不应该是 toArray 吗?
exports.byDirector = function(db, director, callback) {
db.collection('movies').find({director: director}).toArray(function(error, docs){
callback(error, docs);
});
};
我正在尝试学习 MEAN 堆栈,现在我正在使用 NodeJS + MongoDB。
在我正在制作的这个 helloworld 项目中,我有以下要求:
"dependencies": {
"async": "0.9.0",
"mongodb": "2.0.27"
}
这个项目包括在数据库中进行简单的插入和查询。在这种情况下,我想通过导演姓名查询:
/*
* Finds all documents in the "movies" collection
* whose "director" field equals the given director,
* ordered by the movie's "title" field. See
* http://mongodb.github.io/node-mongodb-native/2.0/api/Cursor.html#sort
*/
exports.byDirector = function(db, director, callback) {
db.collection('movies').find({director: director}).toarray(function(error, docs){
callback(error, docs);
});
};
但是,在教程的测试中,我失败了,出现以下错误:
[14:27:33] Starting 'test'...
[14:27:33] Finished 'test' after 1.1 ms
1) dbInterface can query data by director:
TypeError: db.collection(...).find(...).sort(...).toarray is not a function
at Object.exports.byDirector
Tests failed!
我想我遇到了某种语法错误,但老实说我无法找出是什么。
我错过了什么?
我觉得你打错了,
toarray 不应该是 toArray 吗?
exports.byDirector = function(db, director, callback) {
db.collection('movies').find({director: director}).toArray(function(error, docs){
callback(error, docs);
});
};