Select specific fields from database

岁酱吖の 提交于 2019-12-08 15:16:51

问题


I just want to know that is it possible to select specific fields using waterline, orientdb query is given below.

e.g. 
select phone from user

I want to select phone from user vertices by using this query

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'); });

回答1:


Yes, it's possible, you just need to add select to your search criteria, for example (assuming you are searching for a records with id 1):

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

or alternatively:

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

or if you want all records, you don't need to supply criteria:

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

This doesn't seem to be documented anywhere but it should. In version 0.11 it will also possible to define select by doing model.pick('name', 'age'): https://github.com/balderdashy/waterline/pull/952




回答2:


Source and more detail -https://stackoverflow.com/a/24170388/1392194

Yes, it's possible but not with select as it's still in development. But there is a way to achieve it using fields.

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

This won't work with findOne.




回答3:


In Sails version 1 they have added provision for sending query as well as projection to the find()/findOne() methods. You can simply do: Model.find({where: {id: id}, select: ['name', 'phoneNumber']})

Find reference here: https://sailsjs.com/documentation/reference/waterline-orm/models/find#?using-projection




回答4:


You can use the .select() method

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

The opposite of .select() is .omit()



来源:https://stackoverflow.com/questions/29768682/select-specific-fields-from-database

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!