Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mongodb query result without field name

Tags:

mongodb

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.

like image 342
pawinder gupta Avatar asked May 22 '14 15:05

pawinder gupta


People also ask

How do I exclude fields in MongoDB?

To exclude the _id field from the output documents of the $project stage, specify the exclusion of the _id field by setting it to 0 in the projection document.

How do I display null values in MongoDB?

MongoDB fetch documents containing 'null' If we want to fetch documents from the collection "testtable" which contains the value of "interest" is null, the following mongodb command can be used : >db. testtable. find( { "interest" : null } ).

How do I display only one field in MongoDB?

You can select a single field in MongoDB using the following syntax: db. yourCollectionName. find({"yourFieldName":yourValue},{"yourSingleFieldName":1,_id:0});

How do I ignore null values in MongoDB?

Solution 1: In case preservation of all null values[] or null fields in the array itself is not necessary. Filter out the not null elements using $filter , the ( all null elements) array would be empty, filter that out from documents using $match then $sort on values .


1 Answers

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} );
like image 189
mohamedrias Avatar answered Oct 14 '22 02:10

mohamedrias