Fred J.
Fred J.

Reputation: 6049

Meteor findOne to return the value in a given field

I have a Meteor collection which has a document, I need to get the value referenced by a given field name.
I was reading the docs and few online examples but they return the cursor for a given field and value pair. Not what I am after.

On the server, I tried collectionName.findOne('fieldName'); which returned undefined. Thanks

Upvotes: 1

Views: 278

Answers (1)

Pankaj Jatav
Pankaj Jatav

Reputation: 2184

This is how you can send only selected field on meteor.

collectionName.findOne({},{fields:{_id:1}})

<field>: <1 or true>    Specify the inclusion of a field.
<field>: <0 or false>   Specify the suppression of the field.

Here your output will be :: Object {_id: "mbhQBFDFQ6z6BP2Rc"}. You can replace the _id with any other fieldName. For selected multiple filed just separate the fieldNames with comma.

The fields work for find as well as findOne. For more about fields you can check here

For sending only single field using findOne you can directly user

collectionName.findOne().fieldName

Upvotes: 1

Related Questions