带有concat的mongodb聚合项目objectId

编程入门 行业动态 更新时间:2024-10-23 15:32:06
本文介绍了带有concat的mongodb聚合项目objectId的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 db.test.aggregate({ $match : { "themType" : "SuperTest" , "mType" : { "$in" : [ 1 , 2]}} }, { $project : { "_id" : 1, "refTestId" : 1, "avatar" : { $concat : [$refTestId] } } });

并且化身向我返回null,可能是因为它的objectId,在此查询中是否有可能从此objectId字符串中得出?

and avatar returns me null, probably its because its objectId, is it possible in this query to make from this objectId string ?

推荐答案

从MongoDB 4.0及更高版本开始,有一个 $toString 运算符,它以十六进制字符串形式返回ObjectId值:

From MongoDB 4.0 and newer, there is a $toString operator which returns the ObjectId value as a hexadecimal string:

db.test.aggregate([ { "$match": { "themType": "SuperTest", "mType": { "$in" : [1 , 2] } } }, { "$addFields": { "avatar": { "$toString": "$refTestId" } } } ])

或使用 $convert

db.test.aggregate([ { "$match": { "themType": "SuperTest", "mType": { "$in" : [1 , 2] } } }, { "$addFields": { "avatar": { "$convert": { "input": "$refTestId", "to": "string" } } } } ])

更多推荐

带有concat的mongodb聚合项目objectId

本文发布于:2023-11-22 20:47:15,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1618961.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:项目   concat   mongodb   objectId

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!