【问题标题】:Node js / Mongoose .find()节点 js / Mongoose .find()
【发布时间】:2018-06-23 15:37:49
【问题描述】:

我在节点 js 服务器上的 mongoose 中使用 .find() 函数时遇到问题,我一直在尝试使用它,但无法从数据库中获取关键信息。

user.find({key: 1} , function(err, data){
  if(err){
    console.log(err);
  };
  console.log("should be the key VVV");
  console.log(data.key);
});

我主要是无法理解这个函数如何接受查询并返回来自数据库的响应。如果有人可以将其分解,我将非常感谢 mongoose 文档并没有太大帮助。

如果有帮助,这也是我的用户架构

var userSchema = new mongoose.Schema({
  username: {type: String, unique: true},
  password: {type: String},
  key: {type: String},
  keySecret: {type: String}
}, {collection: 'user'});


var User = mongoose.model('user',userSchema);

module.exports = User;

【问题讨论】:

    标签: javascript node.js mongoose


    【解决方案1】:

    嗨,我的朋友,我使用这个方法从数据库中检索数据我希望可以帮助

    user.find({key: 1})
       .then((userOne)=>{
           //if is an API
           res.status(200).json({data : userOne});
           //OR
           // if you had a view named profile.ejs for example
           res.render('/profile',{data : userOne, title : 'User profile' }); 
                console.log(userOne); // just for check in console
            })
        .catch((error)=>{
            //if an api
            res.status(400).json({messageError : error});
            // OR if not an api
            res.render('/profile',{data : 'No data of this profile',errorToDisplay : error})
        });
    });
    

    【讨论】:

      【解决方案2】:

      也许你应该使用

      Model.findOne({key: '1'}, function(err, data) {
        console.log(data.key);
      });
      

      find() 会得到一个 doc 数组,findOne() 只能得到一个 doc。

      您的字段keyString 类型,因此您的查询obj 应该是{key: '1'},而不是{key: 1}

      仔细阅读 mongoose 文档可能会对您有所帮助。

      【讨论】:

      • 我尝试这样做只是为了测试我这样做的唯一问题是我很困惑如何在仅使用查找一个键时将数据库指向正确的条目。有多个用户帐户和密钥实例。
      【解决方案3】:

      如果你想象你的数据库是这样的:

      [
          {
              "name": "Jess",
              "location": "Auckland"
          },
          {
              "name": "Dave",
              "location": "Sydney"
          },
          {
              "name": "Pete",
              "location": "Brisbane"
          },
          {
              "name": "Justin",
              "location": "Auckland"
          },
      ]
      

      执行以下查询;

      myDB.find({location: 'Brisbane'})

      将返回:

      [
          {
              "name": "Pete",
              "location": "Brisbane"
          }
      ]
      

      虽然myDB.find({location: 'Auckland'})会给你

      [
          {
              "name": "Jess",
              "location": "Auckland"
          },
          {
              "name": "Justin",
              "location": "Auckland"
          },
      ]
      

      如您所见,您正在数组中查找与您要查找的find 匹配的键,并以数组的形式返回与该键搜索匹配的所有文档。

      Mongoose 接口以回调的形式将这些数据提供给您,您只需要在它返回的数组中查找项即可

      user.find({location: "Auckland"}, function(err, data){
          if(err){
              console.log(err);
              return
          }
      
          if(data.length == 0) {
              console.log("No record found")
              return
          }
      
          console.log(data[0].name);
      })
      

      【讨论】:

      • 我认为你给出了最好的答案。我打算跳进去但是;你在这里创造了很多价值。很好的答案。
      • 谢谢,.find() 的替代方法是 .findOne() - .find() 返回和数组,而 .findOne() 返回找到的第一条记录(对象),忽略其余部分.这在您查找具有唯一字段(例如通过电子邮件或 ID)的文档的情况下非常有用。
      • @darshanan 您的意思是如何搜索文档中嵌套键中的值?如果是这样,那就是db.collections.find({"keyOne.keyTwo":"search"})
      • @DavidAlsh 请查看上面的评论。
      猜你喜欢
      • 2018-06-15
      • 2018-07-04
      • 2020-07-26
      • 1970-01-01
      • 2021-03-21
      • 1970-01-01
      • 2016-05-02
      • 1970-01-01
      相关资源
      最近更新 更多