我已经用nodejs和mongodb atlas创建了一个rest API。
我有一个对象数组,每个对象都有一个id,我想通过id得到这些对象
的数据
exports.GetUserInfo = (req , res , next)=>{
Client.find({ReadingHistory:
mongoose.Schema.ObjectId("5e557c8f0fda580b28866b9f")})
.select()
.exec()
.then(doc=>{
if (doc) {
console.log('from the database',doc);
} else {
console.log('invalid ID')
}
if (doc) {
res.status(200).json({
ReadingHistory: doc,
});
} else {
res.status(404).json({message: 'Sorry... no data'})
}
})
.catch(err =>{
console.log(err);
res.status(500).json({error: err})
});
}
}
字符串
1条答案
按热度按时间8aqjt8rx1#
MongoDB中的ObjectId不能用=或!==进行比较,我们必须使用
ObjectId.prototype.equals
。因此,您可以编写以下代码:字符串
完整的伪代码可能是:
型