node.js - MongoDB $pull 查询无法正常工作
问题描述
伙计们,我想用 $pull qıery提出取消关注请求,但它不起作用。如何解决这个问题?
当我想关注请求用户时,此方法也适用于$push
查询,但不适用于$pull
.
我的 app.js:
app.post('/unfollow/:id', function(req, res){
User.find(req.user
).exec(function(err, user){
if(err){
console.log(err);
}
User.find({"username":req.params.id}, function(err, friend){
if(err){
console.log(err);
}
User.update(user,{$pull: { follow: friend}}, function(err){
if(err){
console.log(err);
}else{
res.send("Success")
}
});
});
});
});
这是我的用户关注的样子:
{ follow:
[ 5edfe8f3bfc9d677005d55ca,
5edfe92fbfc9d677005d55cc,
5ee2326cc7351c5bb0b75f1a ],
我将从这个数组中获取朋友 ID。
还有我的用户模型(关注部分):
follow:[{
type: ObjectId,
}]
如果 pull 不起作用,您能否建议我查询或取消关注请求的方法。
解决方案
User.find({ username: req.params.id }
会返回一个文档,但看起来你确实在字段中存储ObjectId
了 s 。follow
我除了改成 $pull: { follow: friend }
应该$pull: { follow: friend._id }
可以解决问题。
推荐阅读
- ios - How to display two different images coming from backend in different image views in iOS?
- impala - Kudu 表评论未显示。我应该怎么办?
- javascript - Javascript历史记录错误如何删除历史记录
- php - Symfony 事件订阅者不工作,即使服务被定义
- android - Rxjava - doOnError 完成后如何重试调用
- mysql - MySQL 字符编码更改。是否保留了数据完整性?
- postgresql - 在 PostgreSQL 中为 DATE 列插入 CURRENTDATE 失败
- java - 改造可空参数映射
- javascript - 将正则表达式匹配传递给 console.log 中的变量以在文本框中显示输出
- python - 检查目标时出错:预期 softmax_1 的形状为 (1,),但得到的数组的形状为 (2,)',Keras