如何解决mongoTemplate 使用 LookupOperation 找不到值
我想通过 articleComment 的 _id 和 articleReply 的 commentId 找到 articleReply 值。
articleComment 类型的_id 为ObjectId,articleReply 类型的commentId 为String。
我只找到文章评论值,找不到文章回复值。
查找结果例如:
{_id=60b312a73df6b758e09396d7,territoryId=1,articleId=60ac9e9a3df6b7312c4cbab0,userId=2,userAccount=163397@qq.com,userHeadImage=base/20210501234567.png,comment=帅哥,num=0,goodNum=0,activeFlag=1,updateTime=Sun May 30 12:20:55 CST 2021,createTime=Sun May 30 12:20:55 CST 2021,reply=[]}
我的代码:
public void readComment(String commentId){
LookupOperation lookup1 = LookupOperation.newLookup()
.from("articleReply")
.localField("_id")
.foreignField("commentId")
.as("reply");
MatchOperation match = Aggregation.match(Criteria.where("_id").is(commentId));
Aggregation agg = Aggregation.newAggregation(match,lookup1);
AggregationResults<Map> articleComment = mongoTemplate.aggregate(agg,"articleComment",Map.class);
System.out.println(articleComment.getUniqueMappedResult());}
我该如何解决?
解决方法
我用 db.runComment() 执行 mongodb origin sql 得到正确的数据,是的我得到了正确的 sql,然后 springboot 可以使用 mongoTemplate.executeCommand(String sql) 得到正确的数据。所以它解决了。 我的 mongo sql:
{聚合:“文章评论”, 管道:[ { $match : {"_id" :ObjectId("60aca12d3df6b7312c4cbab1")}},{$project:{"aId": {"$convert": {"input": "$articleId","to": "objectId"}},"bId": {"$convert": {"input": "$_id","to": "string"}},"userId":1,"userAccount":1,"userHeadImage":1,"comment":1,"num":1,"goodNum":1,"createTime":1} },{$lookup:{from: "article",localField: "aId",foreignField: "_id",as: "article" }},{$lookup:{from: "articleReply",localField: "bId",foreignField: "commentId",as: "articleReply" }} ],maxTimeMS: 10000,光标:{} }
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。