【问题标题】:SailsJS relations not working as expectedSailsJS 关系未按预期工作
【发布时间】:2015-10-26 07:52:28
【问题描述】:

带有 MongoDB 适配器的 SailsJS 无法按预期工作。我定义了以下关系:

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

当我在没有条件的情况下查询并填充categories 时:

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

它给了我正确的结果,文档有嵌套的类别。

但是当我尝试通过标准时,它不会返回任何结果。例如

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

注意:我在数据库中插入了类别_id 作为字符串(食物、旅行等)

请帮忙

【问题讨论】:

标签: mongodb sails.js sails-mongo


【解决方案1】:

您不会得到任何结果,因为在categories 中它将存储_id

您可以通过以下查询获得结果。

PostCategory.find({name: 'food'})
       .populate('posts')
       .then(...)

【讨论】:

  • 我之前说过_id是字符串food,但是没有获取记录
  • @mahadazad 你能把mongodb中的记录添加到问题中吗?
猜你喜欢
  • 1970-01-01
  • 2018-11-29
  • 2019-03-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-03-15
  • 1970-01-01
相关资源
最近更新 更多