假设我想通过他们的_id查找用户并检查“喜欢”值(数组)是否包含某个帖子_id . 如何查询数据库以执行此操作?是否可以将这些_id存储在数组中,或者mongodb约定是否更喜欢将其他内容存储为其他文档的引用?
所以我只想检查用户是否在“喜欢”数组中有post _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
});
}
1 回答
对于用户数据
在
liked
数组中检查用户名aa
和4
但
没有匹配的结果 .
Mongoose population可以这样做,你可以定义如下的用户模式