Query on associated value in sails

There are two tables, Books which has many-to-many association with Tags. How can the Book that have only particular tag id be found? I tried:

Book.find().populate("tag", where:{id: 1 }).exec(console.log)

But it gives all the books.

If your looking for all books with a specific tagid, your going to want to do something like:

Tag
.findOne(1)
.populate('books')
.then(function(tag) {
    if (!tag) {
        res.serverError(error);
    } else {
        res.json(tag.books);
    }
});

Basically you want to look up the tag by id (1), and then populate the books association for that tag.

You must provide criteria in find(), not in populate.

Your fixed variant:

Book
    .find()
    .where({
        id: 1
    })
    .populate('tag')
    .exec(function(error, books) {
        if (error) {
            res.serverError(error);
        } else {
            res.json(books);
        }
    });