Reputation: 8454
I'm using Sequelize to query a PostGIS database and return user records based on geographic proximity. I want to exclude the current user from the search results, but I can't figure out the proper syntax to add multiple conditions to this particular kind of search.
As it stands, the query is
models.Geometry.findAll({
where: models.sequelize.where(
models.sequelize.fn(
'ST_Distance_Sphere',
models.sequelize.col('the_geom'),
models.sequelize.fn(
'ST_GEOMFROMTEXT',
'POINT(' + row.dataValues.longitude + ' ' + row.dataValues.latitude + ')', 4326
)
),
'<=',
radius
)
})
I'd like to add
where: {
UserId: {
$not: currentUser.id
}
}
I couldn't find anything in the Sequelize docs with an example... the closest I got was
Post.findAll({
where: sequelize.where(sequelize.fn('char_length', sequelize.col('status')), 6)
});
but this is not really any different than my current situation.
Upvotes: 1
Views: 3653
Reputation: 7411
You can assign the models.sequelize.where
function value to some attribute in the where
object - this attribute will not be used as a lookup field
models.Geometry.findAll({
where: {
stDistanceSphere: models.sequelize.where(
models.sequelize.fn(
'ST_Distance_Sphere',
models.sequelize.col('the_geom'),
models.sequelize.fn(
'ST_GEOMFROMTEXT',
'POINT(' + row.dataValues.longitude + ' ' + row.dataValues.latitude + ')', 4326
)
),
'<=',
radius
),
UserId: { $not: currentUser.id }
}
})
Or you can use the sequelize.and()
function to construct AND
query
models.Geometry.findAll({
where: models.sequelize.and(
models.sequelize.where(
models.sequelize.fn(
'ST_Distance_Sphere',
models.sequelize.col('the_geom'),
models.sequelize.fn(
'ST_GEOMFROMTEXT',
'POINT(' + row.dataValues.longitude + ' ' + row.dataValues.latitude + ')', 4326
)
),
'<=',
radius
),
{ UserId: { $not: currentUser.id } }
)
})
Upvotes: 7