User.find({ _id: { '!': user.id } }, function foundFriends (err, friends) {
if(err) return next(err);
res.view({
friends: friends
});
});
MONGODB :
{
_id: ObjectId("53b942f7c8638f7b17670acc"),
name: "PH",
admin: true,
online: false,
encryptedPassword: "$2a$10$PjcPRgL67ZSOWDjmEwTkvu30xKeDXdCwgZ.D0.bjyDRw9sOfS/4UK",
createdAt: ISODate("2014-07-06T12:37:11.522Z"),
updatedAt: ISODate("2014-07-09T18:22:47.25Z")
}此代码不起作用,我想按Id选择文档。我不知道如何在我的sails项目中解决这个问题。谢谢你的帮助。
发布于 2014-09-26 23:48:24
我在你的代码中发现了一些奇怪的地方。
sails 10的回调函数应该用.exec()
_id,而应该只搜索id。我认为waterline解析了这个下划线。记住这一点,你的代码应该看起来像这样
User.find({ id: { '!': user.id } }).exec(function (err, friends) {
if(err) return next(err);
res.view({
friends: friends
});
});发布于 2016-11-07 10:49:07
您好,您可以尝试通过select使用sails-mongo按id查找、更新或销毁:
//Schema
module.exports = {
autoPK : false,
attributes : {
id : {
type: 'string',
primaryKey: true
},
name : {
type : 'string'
},
email : {
type : 'string'
}
}
}
// Find
User.find({
select : {id : user.id}
}).exec((err,record)=> {
if(err) return console.log(err,"err");
console.log(record,"record");
})
// Update
User.update({
select : {id : user.id}
},{
name : "Foo"
}).exec((err,record)=> {
if(err) return console.log(err,"err");
console.log(record,"record");
})
// Destroy
User.destroy({
select : {id : user.id}
}).exec((err,record)=> {
if(err) return console.log(err,"err");
console.log(record,"record");
})希望这能对你有所帮助。
发布于 2019-08-22 23:29:43
您可以尝试这样做:
User.findOne({ _id : ObjectId(user.id.toString()) })希望这能有所帮助。
https://stackoverflow.com/questions/24661635
复制相似问题