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

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

问题描述

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

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) })

返回值结果是:

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

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

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