0

我在 mongodb 数据库中有两个集合,每个集合都有模型

应用模型

module.exports = {

    tableName: 'app',
    attributes: {
        _id : {
            primaryKey: true,
            unique: true,
            type: 'string',
        },
        userId: {
            model: 'user'
        },
        title: {
            type: 'string',
            required: true,
            unique: true,
        },
        createdDate : 'string'
    },
};

和用户模型

module.exports = {

    tableName: 'user',
    attributes: {
        id : {
            primaryKey: true,
            unique: true,
            type: 'string',
            collection: "app",
            via : "userId"
        },
        password: {
            type: 'string',
            required: true
        },
        apps : {
            collection: "app",
            via : "userId"
        }
    },

};

当我使用数值加入此集合时,它工作正常,但是当我尝试使用 mongodb 本机 id 对象时,我得到空结果

我如何调用连接查询

User.find().populate('apps').exec(function(err, result) {});
4

1 回答 1

0

您需要摆脱模型中的_idid属性定义。Waterline 将自动为您处理主键字段(将它们标准化为id),因此除非您需要更改字段类型,否则可以安全地忽略它们。另外,我不确定您通过添加collection和定义via的意图是什么id,但主键永远不会成为关联。

否则,您的模型看起来是正确的。如果你摆脱这两个属性,事情应该可以正常工作。

于 2014-09-26T20:40:52.147 回答