4

I am performing a Mongodb collection.find using node.js, how do I return only the data without the column name into an array.

var cursor = collection.find( { title: title }, { title: 1, _id: 0 });
cursor.sort( { title: 1 });
cursor.toArray(function (err, all_documents) { .... });
{"title":"MongoDB Overview"}
{"title":"NoSQL Overview"}
{"title":"Tutorials Point Overview"}
2
  • 1
    You can map it after the fact - var arr = all_documents.map(function(d) { return d.title }); Commented Dec 8, 2015 at 15:10
  • 1
    Can you give us some sample output you expect to see? Commented Dec 8, 2015 at 15:15

1 Answer 1

1
collection.find(...).toArray().map( function(u) { return u.title; } )

or

var result = []
collection.find().forEach(function(u) { result.push(u.title) })
Sign up to request clarification or add additional context in comments.

1 Comment

forEach and map are client side use aggregate() and the $push operator.

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.