mongodb query result without field name

ぐ巨炮叔叔 提交于 2021-02-19 01:06:42

问题


Is there a way to get mongodb query results with only the values and not the field names. My query gives me the following result:

{
           "t_number" : 2508
},
{
           "t_number" : 2560
},
{
           "t_number" : 2599
}

Ideally I want the query result to be [2508,2560,2599]. Or if that is not possible, is it possible to get the query result as [{2508},{2560},{2599}]. I know that I can iterate over the result and change the format in my programming language. I am looking for a way to get that from mongodb and save some work.


回答1:


No, you can't do it directly.

But this one liner could help you:

db.collection.find({},{_id:0, t_number:1}).toArray().map(function(ele) {return ele.t_number} );



回答2:


I think that is not possible because mongodb returns only JSON Objects and according to http://www.json.org a JSON-Object is surrounded by curly brackets { } and (if it is not empty) a value is allways indecated by a String (and seperated by a :).



来源:https://stackoverflow.com/questions/23810494/mongodb-query-result-without-field-name

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!