14

I am new in Meteor, and especially in MongoDB.

I have googled a lot regarding this issue but nothing found related to this.

So there is an app that contains two collections: EventsCollection and RacesCollection and runs on Meteor server.

The RacesCollection has number of records like:

RacesCollection.insert({raceId:"r1", eventId:"e1", raceName:"Moto race 1", status:"statusDetail", mode:"modeDetail"});
RacesCollection.insert({raceId:"r2", eventId:"e1", raceName:"Moto race 2", status:"statusDetail", mode:"modeDetail"});

This is the resultant collection which contains rows having eventId = e1

var race = RacesCollection.find({eventId: "e1"});

Now what i want to do is to simply access the fields of race into javascript, something like race.raceId, race.raceName. How to implement this? Is there any getter method for accessing particular data field?

And how to iterate through multiple rows of race in case it contains number of rows?

Any recommendation will be appriciated.

4 Answers 4

31

use ForEach :

db.databaseName.find(
  {
    field:"valueofField"
  }
).forEach(function(obj){
    print(obj.fieldname)
})
Sign up to request clarification or add additional context in comments.

Comments

10

MongoDB's find() method returns what's called a "cursor". In javascript, you can iterate over the cursor as in these docs, and access the fields of the documents using standard javascript property access.

For example (untested code, but this is the idea):

var raceCursor = RacesCollection.find({eventId: "e1"});
var race;
while ( raceCursor.hasNext() ) {
    race = raceCursor.next();
    console.log( race.raceName );
}

Since Meteor is pure javascript, it also supports using forEach() to iterate over the cursor's documents, as in this example.

7 Comments

forEach can be used on arrays, but not on a MongoDB Cursor. You'd have to call toArray on the cursor if you wanted to do that.
@JohnnyHK - It seems (looking at the last example I posted in the question) that Meteor doesn't require the toArray call (even if other javascript-based drivers do).
Interesting. So meteor must add its own layer on top of the driver to support that.
@shelman .. I've tried your code, but it shows following error on JAVASCRIPT CONSOLE in Google Chrome: Uncaught TypeError: Object [object Object] has no method 'hasNext'
Yeah. mine is getting the same problem as @sohelkhalifa . funny that the documentation states that there is a hasNext method. docs.mongodb.org/manual/reference/method/cursor.hasNext
|
4

I just had the same problem and hasNext() did not work for me.

Instead Meteor provides fetch() to convert a cursor to a javascript array. So you can use:

var raceCursor = RacesCollection.find({eventId: "e1"});
var races = raceCursor.fetch();
for (var i=0; i<races.length; i++) {
    console.log( races[i].raceName );
}

1 Comment

It is better to use the forEach method instead of fetching the documents and looping through them yourself. See the answer by Sekai.
4

Sekai's answer works, however, it's written from a MongoDB perspective. From a Meteor JavaScript helper, using the variables used in the question, it would look like:

RacesCollection.find(
   {eventId: "e1"}
).forEach(function(race){
   console.log( race.raceName );
};

Comments

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.