如何在mongodb中只返回一个字段的值

2022-01-13 00:00:00 mongodb database nosql javascript

在 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"]

相关文章