2

SailsJS with MongoDB adapter not working as expected. I have following relations defined:

Post.js:

module.exports = {
  connection: 'mongodb',

  attributes: {
    title: 'string',
    categories: {
      collection: 'postCategory',
      via: 'posts'
    }
  }
};

PostCategory.js

module.exports = {
  connection: 'mongodb',

  attributes: {
    name: 'string',
    posts: {
      collection: 'post',
      via: 'categories'
    }
  }
};

When I query without criteria and populate categories like:

Post.find()
       .populate('categories')
       .then(...)

it gives me correct result, the document has categories nested.

But when I try to pass criteria it returns no result. e.g.

Post.find({categories: 'food'})
       .populate('categories')
       .then(...)

Note: I inserted category _id as string (food, travel, etc) in database

Please help

mahadazad
  • 541
  • 5
  • 19

1 Answers1

1

You will not get any results because in categories it will store the _id.

You can get the results by doing the following query.

PostCategory.find({name: 'food'})
       .populate('posts')
       .then(...)
Vishnu
  • 11,614
  • 6
  • 51
  • 90