Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select specific fields from database

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'); });
like image 709
Zeeshan Avatar asked Apr 21 '15 09:04

Zeeshan


People also ask

How do I select a specific field in MySQL?

If you want to select only specific columns, replace the * with the names of the columns, separated by commas. The following statement selects just the name_id, firstname and lastname fields from the master_name table.

How do I select a specific row and column in SQL?

To select rows using selection symbols for character or graphic data, use the LIKE keyword in a WHERE clause, and the underscore and percent sign as selection symbols. You can create multiple row conditions, and use the AND, OR, or IN keywords to connect the conditions.


2 Answers

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

like image 174
Dário Avatar answered Sep 17 '22 18:09

Dário


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

like image 24
Niraj Gupta Avatar answered Sep 18 '22 18:09

Niraj Gupta