简体   繁体   English

如何使用 Node.js 在 MongoDB 中查找记录

[英]How to find a record in MongoDB using Node.js

I am trying to find whether my collection has a record with profilename = john and if exists I return status success if else I return fail but in my case, it is returning success for both cases.I am new to node and mongo can any one help me.我试图找出我的收藏是否有一个profilename = john的记录,如果存在,我返回status success ,否则我返回失败,但在我的情况下,它在两种情况下都返回成功。我是 node 的新手,mongo 可以任何人帮我。

My function,我的功能,

    exports.searchprofilename = function (req, res) {
  var params = req.params;console.log(req.params.id);
  var record= db.collection('profile');
   record.find({profilename:params.id}, (err, result) => {
   if (err){ return console.log(err)
    }
      if(!result){
            data = {status:'success'};
        } else{
             data = {status:'profile name already exists'};
        }
      res.send(data);
  });
};

If you are only checking if a record exists, you should be easily able to do it using db.collection.count() method and checking if the number of records = 0 or not.如果您只是检查记录是否存在,您应该可以轻松地使用 db.collection.count() 方法并检查记录数是否为 0。

https://docs.mongodb.com/manual/reference/method/db.collection.count/ https://docs.mongodb.com/manual/reference/method/db.collection.count/

Honestly, I am way new to mongodb and I still cannot grasp the idea of cursors which is the return type of db.collection.find() as per https://docs.mongodb.com/manual/reference/method/db.collection.find/老实说,我是 mongodb 的新手,我仍然无法理解游标的概念,它是 db.collection.find() 的返回类型,如https://docs.mongodb.com/manual/reference/method/db。集合.find/

我通过将find({})更改为findOne({})来清除它,谢谢大家。

If your query matches then it means you have record then return Success如果您的query matches则表示您有record然后返回Success

 exports.searchprofilename = function (req, res) {
  var params = req.params;console.log(req.params.id);
  var record= db.collection('profile');
   record.find({profilename:params.id}, (err, result) => {
   if (err){ return console.log(err)
    }
    // If record exist then return 'Success'
      if(result.length>0){
            data = {status:'success'};
        } else{
             data = {status:'profile name already exists'};
        }
      res.send(data);
  });
};

Try this尝试这个

    exports.searchprofilename = function (req, res) {
        console.log("PARAMS",req.params.id);
        var data = {};
        profile.findOne( {profilename:req.params.id} , function (err, fetchDataObj) {
                if (err) {
                    console.log(err)
                   return err;
                } else {
                    data.status = 'success';
                    data.result = fetchDataObj;
                    return data;
                }
            }).lean(true)

  });

I think in your case, req.params.id is a String for example '123', but in your mongodb profilename field is stored as an Number.我认为在您的情况下, req.params.id是一个字符串,例如“123”,但在您的 mongodb profilename 字段中存储为一个数字。

So you can try this:所以你可以试试这个:

change {profilename:params.id} to {profilename:parseInt(params.id)}{profilename:params.id}更改为{profilename:parseInt(params.id)}

Try to Debug.尝试调试。 the type of result is array, so try to check the length of it:结果的类型是数组,所以尝试检查它的长度:

if(result.length==0){
    data = {status:'success'};
} else{
    data = {status:'profile name already exists'};
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM