【问题标题】:Select specific fields from database从数据库中选择特定字段
【发布时间】:2015-04-21 09:50:33
【问题描述】:

我只是想知道是否可以使用水线选择特定的字段,下面给出了orientdb查询。

e.g. 
select phone from user

我想使用此查询从用户顶点中选择电话

userModel.find(phone)
.then(function(phonelist){ 
  if(!phonelist) 
     console.log('msg: RECORD_NOT_FOUND'); 
  else 
     console.log(phonelist);
.catch(function(err){ console.log('err: 'err'); });

【问题讨论】:

    标签: waterline sails-orientdb


    【解决方案1】:

    是的,有可能,您只需将select 添加到您的搜索条件中,例如(假设您正在搜索 id 为 1 的记录):

    userModel.find({ select: ['phone'], id: 1 })
    

    或者:

    userModel.find({ select: ['phone'], where: { id: 1 } })
    

    或者如果您想要所有记录,则无需提供条件:

    userModel.find({ select: ['phone'] })
    

    这似乎没有记录在任何地方,但它应该。在 0.11 版本中,还可以通过 model.pick('name', 'age'): https://github.com/balderdashy/waterline/pull/952 来定义选择

    【讨论】:

    • 这在 Waterline 0.10.21 中对我不起作用,我没有任何选择就获得了所有字段 :( - 我希望 0.11 尽快发布!
    • @FranDios,你在使用sails-mongo吗?如果是这样,则其中存在一个仅在 8 天前(v0.11.3)修复并发布的错误。
    • 是的,我确实使用sails-mongo。感谢您的信息,将检查它:)
    • 我有同样的问题,将sails-mongo 更新到0.11.3 但不幸的是url:localhost:1337/modelName?select=['date'] 仍然返回所有字段。
    • select 在sails v 0.12.13 上适用于我,但是,如何防止id 与响应一起标记?
    【解决方案2】:

    来源和更多细节 -https://stackoverflow.com/a/24170388/1392194

    是的,这是可能的,但select 不行,因为它仍在开发中。但是有一种方法可以使用fields 来实现它。

    Model.find({ id: id }, {
      fields: {
        name: 1,
        phoneNumber: 1
      }
    }).limit(1).exec(function(...) {};
    

    这不适用于findOne

    【讨论】:

      【解决方案3】:

      在 Sails 版本 1 中,他们添加了向 find()/findOne() 方法发送查询和投影的规定。你可以简单地做: Model.find({where: {id: id}, select: ['name', 'phoneNumber']})

      在此处查找参考: https://sailsjs.com/documentation/reference/waterline-orm/models/find#?using-projection

      【讨论】:

        【解决方案4】:

        您可以使用 .select() 方法

        let phones = await userModel.find().select(['phone']);
        

        .select()的反义词是.omit()

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2016-12-10
          • 2020-01-23
          • 1970-01-01
          • 2021-08-16
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多