【问题标题】:Retrieving model using mongoose使用猫鼬检索模型
【发布时间】:2015-07-28 19:08:18
【问题描述】:

我想通过“id”从我的数据库中检索整个模型。 在这种情况下最好使用哪个查询?

这是我的架构的外观:

{ _id: 54b5ed4b967a4e3b96fe8a39,
  email: 'blabla@yahoo.com',
  __v: 0,
  deleted: false,
  createdAt: Tue Jan 13 2015 23:15:07 GMT-0500 (EST),
  shipping: {},
  profile: { picture: '', location: 'Romania', name: 'bla' },
  tokens: [],
  role: 'shopper'} 

而我拥有的是:54b5ed4b967a4e3b96fe8a39

【问题讨论】:

    标签: node.js mongodb mongoose


    【解决方案1】:

    如果您知道要查找的Model 的类型,即Person。做吧:

    var id = "54b5ed4b967a4e3b96fe8a39";
    Person.findById(id, function(err, person) {
        if (err) {
            return console.log('oh no! error', err);
        }
        console.log('found person', person);
    });
    

    文档here

    【讨论】:

    • 谢谢!这行得通。现在我想访问 person.mail 但我得到的响应是它未定义。即使当我执行 console.log(person) 时,我也可以看到架构。如何访问 person.email?
    • person.mailperson.email?您可以发布console.log() 和/或架构声明的相关部分吗?
    • 成功了,抱歉。是别的东西挡住了它。非常感谢!
    猜你喜欢
    • 1970-01-01
    • 2015-06-12
    • 1970-01-01
    • 1970-01-01
    • 2017-10-05
    • 2019-08-04
    • 2018-03-04
    • 2018-07-15
    • 2014-12-09
    相关资源
    最近更新 更多