【发布时间】:2015-11-14 19:22:20
【问题描述】:
当我运行代码时
var collection = db.get('categories');
console.log(collection.find().limit(1).sort( { _id : -1 } ));
在 nodejs 上使用 mongodb 我收到错误 Object # has no method 'limit' 。我是 node 的初学者,真的卡在 node 的这一部分
这里是获取最后一个插入文档的完整代码。
router.post('/addcategory', function(req, res) {
// Set our internal DB variable
var db = req.db;
// Get our form values. These rely on the "name" attributes
var name = req.body.name;
var description = req.body.description;
// Set our collection
var collection = db.get('categories');
// Submit to the DB
collection.insert({
"name" : name,
"description" : description,
}, function (err, doc) {
if (err) {
// If it failed, return error
res.send("There was a problem adding the information to the database.");
}
else {
// And forward to success page
/******************/
console.log(collection.find().limit(1).sort( { _id : -1 } ));
/*************/
}
});
});
【问题讨论】:
-
你用的是什么版本的mongodb驱动?把
var collection = db.get('categories');改成var collection = db.collection('categories');有帮助吗? -
mongodb 使用什么模块?
-
@JoGoFo 当我这样做时,我收到此错误
Object #<Manager> has no method 'collection' -
@mscdex 我正在使用 mongo 和和尚,但我不确定它是否正确
var mongo = require('mongodb'); var monk = require('monk'); var db = monk('localhost:27017/glass')
标签: node.js mongodb mongodb-query