mongodb 聚合连表lookup查询子表_id 格式ObjectId是会被当成BSON解析的问题

大猪oops 2022-06-15 23:32:41

sql语句Navicat控制台输出:(也是想要的结果)

db.user.aggregate([{ "$match" : {"userId":  NumberLong("4071" }} ,{$lookup: {
	 	from: 'tb_template',
	 	localField: 'userId',
	 	foreignField: 'userId',
	 	as: 'template'
}}])

以下是结果:

{
    "_id": ObjectId("62a8314584ea1"),
    "userId": NumberLong("4071"),
    "template": [
        {
            "_id": ObjectId("628f39c0b594df"),
            "userId": NumberLong("4071"),
            "visits": NumberInt("0"),
            "content": "",
            "status": NumberInt("0"),
            "hashMap": { },
            "list": [ ],
            "object": { }
        }
    ]
}

 

以下是java查询代码:

Aggregation agg = Aggregation.newAggregation(
       Aggregation.match(Criteria.where("userId").is(4071L)),
       Aggregation.lookup("tb_template","userId","userId","template")
);

java输出结果:template.id问题出现导致前端无法使用

{
    "_id": ObjectId("62a8314584ea1"),
    "userId": NumberLong("4071"),
    "template": [
        {
           "_id": {
                "timestamp": 1653551504,
                "counter": 11906057,
                "time": 1653551504000,
                "date": "2022-05-26T07:51:44.000+0000",
                "timeSecond": 1653551504,
                "processIdentifier": 18880,
                "machineIdentifier": 2697172
             },
            "userId": NumberLong("4071"),
            "visits": NumberInt("0"),
            "content": "",
            "status": NumberInt("0"),
            "hashMap": { },
            "list": [ ],
            "object": { }
        }
    ]
}

理想结果是:template.id=628f39c0b594df 字符串二不是一个BSON格式,

请问大神都有什么解决方法,不在查询结果过for格式化或者转义情况下,使用mongodb自带方法最好

 

 

 

...全文
570 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
大猪oops 2022-11-12
  • 打赏
  • 举报
回复

使用template实体类

1,799

社区成员

发帖
与我相关
我的任务
社区描述
MongoDB相关内容讨论区
社区管理员
  • MongoDB社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧