我只是创建一个简单的用户登录,以便通过 Express 使用 MongoDB、Backbone 和 Node。
我一直在查询数据库中的用户凭据并可靠地识别它们是否存在。
我在这里尝试两件事:
// When user posts to the url
app.post('/save/user', function (req, res) {
// create user object and store email from form
var user = {
email : req.body.email
};
// in my collection users
db.collection('users', function (err, collection) {
// create a variable existence with the value, in lamens terms 'yes it does' or 'no it doesnt'
var existence = findOne(collection, {
$query : {email : user.email}
});
});
};
// findOne function passing the collection and the query object
function findOne(coll, query) {
var cursor = coll.find(query).limit(1);
return cursor.hasNext() ? cursor.next() : null;
}
所以这是我尝试的第一种方法。问题是,我不明白为什么光标没有 'next()、hasNext()、findOne()、forEach()' 和其他适用于 javascript 环境的方法,而仅适用于 mongo shell。
我的问题是,如何从 Node.js 应用程序中访问这些方法?
我尝试的第二种方法:
//当用户发布到 url 时 app.post('/save/user', function (req, res) {
// create user object and store email from form
var user = {
email : req.body.email
};
// in my collection users
db.collection('users', function (err, collection) {
// Have a look for the email entered
var query = collection.find({
$query : {email : user.email}
}).limit(1);
// If it's true, then send response
query.each( function (err, item) {
// If the item does exists, then send back message and don't insert. Otherwise, insert.
if(item !== null) {
console.log('Item does not equal null : ', item);
res.send('The email : ' + item.email + ' already exists');
} else {
console.log('Item does equal null : ', item);
collection.insert(user);
res.send('New user with email : ' + user.email + ' was saved');
}
});
});
};
这样做的问题是,它总是会在某个时候返回 null,因此我将警告用户“它已经存在”,然后下一次将返回 null,因此它将保存电子邮件。
我认为我错过了重点,所以方向正确的点会很棒。
非常感谢!
<小时/>好吧,我已经研究了解决方案,但仍然没有捕获重点。
我正在执行插入操作,使用 safe : true 传入我的用户对象,但尽管可以输入多个用户对象,但它仍然只查找相同的 ID;。我已经尝试使用 new ObjectID() 创建一个 id,但我仍然不明白如果用户输入他们的电子邮件地址,然后尝试使用相同的电子邮件创建一个新用户,它会为该条目创建一个新的 id。
通过执行findOne,我可以轻松地查看它是否存在,我不知道如何使用插入来完成它。
app.post('/register/user', function (req, res) {
// Store user details in object
var user = {
username : req.body.user,
email : req.body.email,
password : req.body.password
};
db.collection('users', function (err, collection) {
collection.insert(user, {safe : true}, function (err, doc) {
console.log('what is doc : ', doc);
if(!err) {
res.writeHead(200, {
"Content-Type" : "text/plain",
"Message" : "New user added",
"Access-Control-Allow-Origin" : "*"
});
} else {
console.log('Error is : ', err);
res.writeHead(200, {
"Content-Type" : "text/plain",
"Message" : "User already exists",
"Access-Control-Allow-Origin" : "*"
});
}
res.end();
});
});
});
最佳答案
如果您使用 Native Node.JS MongoDB driver ,有一个collection.findOne()方法和cursors确实有与 shell 方法 hasNext()
和 forEach()
等效的 nextObject()
和 each()
方法。 mongo shell 等效项的实现/命名可能因驱动程序而略有不同,但您应该能够将 mongo shell 示例转换为 Node.JS。
就保存新用户而言..而不是在插入之前查询用户,您最好在 email
字段和 insert into the collection 上添加唯一索引。与安全:true
。如果插入由于重复的电子邮件而失败,您可以处理 err
结果。如果您在插入之前执行查询,则存在潜在的竞争条件,即在您检查电子邮件时该电子邮件可能不存在,但在您执行插入时该电子邮件确实存在。
关于node.js - 使用javascript、游标方法进行mongodb查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11383116/