本文介紹了如何在mongodb中只返回一個(gè)字段的值的處理方法,對大家解決問題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!
問題描述
在 mongodb 中應(yīng)用查找操作后..我得到以下文檔列表..
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" }
如何轉(zhuǎn)換成
["Hey","Hi","Hello","yes"].
我試過了
db.users.find(...).map( function(u) { return "u.text"; } )
但它給出了錯(cuò)誤!
推薦答案
不確定你的語言實(shí)現(xiàn)是什么,但基本概念是:
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"]
這篇關(guān)于如何在mongodb中只返回一個(gè)字段的值的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!
【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(lián)系我們刪除處理,感謝您的支持!