如何把MongoDB的aggregate的返回值转换成一个字符串数组
正文
今天有一个需求,需要将aggregate的返回值转换成一个字符串数组,这个事情颇为费了一番脑筋,最后终于得以解决,参考这里。
db.users.aggregate([
{ "$unwind": "$emails" },
{ "$group": { "_id": "$emails.address" } }
]).map(function(el) { return el._id })
aggregate返回的总是对象,对这个结果直接使用map方法,对其进行转换。应该参考的是这里,使用了cursor的概念,这里还有toArray的方法,这个方法也非常有用。
在aggregate的输出后面,使用map方法,返回id
的值。
参考
https://stackoverflow.com/questions/33425565/how-to-return-array-of-string-with-mongodb-aggregation
https://examples.javacodegeeks.com/software-development/mongodb/mongodb-map-example/
posted on 2018-08-30 17:48 chaiyu2002 阅读(836) 评论(0) 编辑 收藏 举报