10

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.

2
  • Use aggregate with $unwind. another pipeline use $group with $push. db.col.aggregate([{$unwind: "$t_number"}, {$group: {_id: field, "t_number": {$push: "$t_number"} }}]). Try it. Commented May 22, 2014 at 19:32
  • Thanks. Aggregate gives the result closer to what I am looking for. Commented May 23, 2014 at 2:12

2 Answers 2

11

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} );
Sign up to request clarification or add additional context in comments.

1 Comment

does this result in [2508,2560,2599] being sent over the wire (as opposed to extracted application side)?
0

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 :).

3 Comments

MongoDB returns BSON objects
Wenn that's true. BSON is a binary representation of JSON so the rules of defining a valid object are the same.
An array is an object and it is also a data type in BSON, in theory MongoDB could return a transformed result with only the array but such funcitonality does not yet exist

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.