Node.js 如何从mongodb检索特定数据

Node.js 如何从mongodb检索特定数据,node.js,mongodb,express,mean-stack,Node.js,Mongodb,Express,Mean Stack,这里我使用的是平均堆栈。这是我的数据库模型 var datas = mongoose.model('datas',{ username: string, password: string, email: string }); 这是我的检索代码 app.use('/api/datas',function(req,res) { console.log('fetching reviews'); }); 如何仅检索给定用户名字段的用户名和密码 如果我理解你的问

这里我使用的是平均堆栈。这是我的数据库模型

    var datas = mongoose.model('datas',{
    username: string,
    password: string,
    email: string
});
这是我的检索代码

app.use('/api/datas',function(req,res) {
    console.log('fetching reviews');
});

如何仅检索给定用户名字段的用户名和密码

如果我理解你的问题,你想在数据库中找到一个用户名和密码由客户端发送的用户吗

您可以通过以下方式完成此操作:

app.use('/api/datas',function(req,res) {
    var model = mongoose.model('datas');
    model.find(
        {username: req.body.username, password: req.body.password}, // Search request
        {username:1, password:1, email:0}, // You only want to get in return username and password, no email
        function(err, reponse) {
            if (err) {
                console.log(err); // On error
            }
            else {
                console.log(response); // Data found
            }
        });
});
希望能有帮助