Loopback.io找到包含

时间:2015-04-23 11:46:59

标签: mongodb loopbackjs

我使用Loopo.io和MongoDB。我无法想象如何使用" include"过滤

目标是让用户"用户"完全包含在"组"完成find()或findOne()时。我现在得到的只是数组中的id。感谢

theuser.json:

{
  "name": "theuser",
  "plural": "theusers",
  "base": "User",
  "idInjection": true,
  "properties": {
    "peerId": {
      "type": "string",
      "required": false
    },
    "peerStatus": {
      "type": "string",
      "required": false
    }
  },
  "validations": [],
  "relations": {
    "notes": {
      "type": "hasMany",
      "model": "note",
      "foreignKey": "ownerId"
    },
    "groups": {
      "type": "hasMany",
      "model": "group",
      "foreignKey": "groupId"
    }
  },

group.json:

{
  "name": "group",
  "plural": "groups",
  "base": "PersistedModel",
  "idInjection": true,
  "properties": {
    "name": {
      "type": "string"
    }
  },
  "validations": [],
  "relations": {
    "host": {
      "type": "belongsTo",
      "model": "theuser",
      "foreignKey": "ownerId"
    },
    "clients": {
      "type": "hasMany",
      "model": "theuser",
      "foreignKey": "clientId"
    }
  },
  "acls": [],
  "methods": []
}

我试图找到一个这样的小组:

Group.findOne({
        filter: {
            where: {"ownerId": 1},
            include: {relation: "clients"}
        }
}

1 个答案:

答案 0 :(得分:2)

Group.findOne({
  where: {
    ownerId: 1
  },
  include: 'clients'
}, cb);