【发布时间】:2016-07-08 00:56:12
【问题描述】:
假设我想通过 _id 查找用户并检查“喜欢”值(数组)是否包含某个帖子 _id。如何查询数据库以执行此操作?可以将这些 _id 存储在一个数组中,还是 mongodb 约定更喜欢存储其他内容作为对其他文档的引用?
所以我只想检查用户是否在“liked”数组中有帖子_id。
var users = new mongoose.Schema({
name : {type: String, unique : true, required : true, dropDups: true},
password : {type: String, required : true}, //hash
liked : [String],
created : {type: Date, default: Date.now}
});
我认为这可能是这样的:
function checkIfLiked() {
let uname = "Jim";
let postId = "abc";
//check if $USER has `postId` in $LIKED
user.findOne({$USER: uname},{$LIKED: {$in: postId} }, function(err, results) {
//do something after check
});
}
【问题讨论】:
标签: javascript arrays node.js mongodb mongoose