Search code examples
mongodbmeteorcollectionsmongo-collection

Retrieving of data in an object from mongo collection


I'm using meteor, and i have not typed "meteor remove autopublish" therefore i believe my client is able to retrieve the mongo collection "List" with no issues.

So i've added 1 object into the List collection, but i can't seem to retrieve the data from within the object.

List.find().fetch() //i wrote this in the console and the next few lines was the reply
[{…}]
0
:
{_id: "JqsKLoY4sx9qT8ZRR", module: "Math", user: "Tom"}
length
:
1
__proto__
:
Array(0)

But when i wrote conosle.log(List.find({module: "Math"}).user), what is returned to me in the console was "undefined".

is there a reason for this or something I'm doing wrongly? I want to retrieve the username "Tom". Because what i truly want to do in the end is to use the value within javascript itself, for e.g - var creator = List.find({module: "Math"}).user


Solution

  • In Mongo DB, if you use find.fetch it returns you array and so when you are trying to get the value it gives you undefined.

    So there is another option you can choose which is findOne, which returns object.

    Here's how you can do it:

    var creator = List.findOne({module: "Math"}).user || null;