如何在mongodb中只返回一个字段的值 [英] How to return only value of a field in mongodb

查看:21
本文介绍了如何在mongodb中只返回一个字段的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 mongodb 中应用查找操作后..我得到以下文档列表..

After applying the find operation in mongodb.. i get the following list of documents..

  db.users.find(....)

我明白了:

 { "text" : "Hey" }
 { "text" : "Hi" }
 { "text" : "Hello" }
 { "text" : "yes" }

如何转换成

 ["Hey","Hi","Hello","yes"].

我试过了

 db.users.find(...).map( function(u) { return "u.text"; } )

但它给出了错误!

推荐答案

不确定你的语言实现是什么,但基本概念是:

Not sure what you language implementation is but the basic concept is:

var result = []
db.users.find().forEach(function(u) { result.push(u.text) })

result的返回值为:

["Hey","Hi","Hello","yes"]

这篇关于如何在mongodb中只返回一个字段的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆