【发布时间】:2014-05-29 21:13:09
【问题描述】:
我有以下用户模型:
var model = module.exports = {
autoPK: false,
attributes: {
id: {
type: 'string',
primaryKey: true
},
email: {
type: 'string',
required: true
},
hash: {
type: 'string',
required: true
},
}
}
以及下面的查询就可以了:
User.findOne({_id: req.param('user_id')}).populate('drafts').then(function(user) {
console.log("USER: " + JSON.stringify(user) + " FOR: " + req.param('user_id'));
res.send(user.drafts, 200);
});
从打印语句中,我知道没有为 ID“Rfrq8un5f”返回任何内容,但 mongodb 命令行输出如下:
> db.user.find();
{ "email" : "m@m.com", "hash" : "[...]", "createdAt" : ISODate("2014-05-18T16:32:21.023Z"), "updatedAt" : ISODate("2014-05-18T16:32:21.023Z"), "_id" : "9PTIqHxEc" }
发生了什么事?
【问题讨论】:
-
嗯,您正在搜索 ID 为“Rfrq8un5f”的用户。您的数据库包含一个用户,ID 为“9PTIqHxEc”。所以...
-
@ScottGress 所以...我是个白痴手掌到额头
-
Nawwwwwwwww 它发生了 ;)
-
其他几点:1)
autoPK在使用sails-mongo 时没有任何实际意义,2) 适配器会自动为您在id和_id之间进行转换.所以你不需要在你的模型中显式配置id属性,你可以查询id而不是_id来得到你想要的结果。
标签: javascript node.js mongodb express sails.js