Mongo query help

Hello,

I’m trying to get a list of users and their trip milleage summary.
For list users I am using Meteor.users and for their trips I have a mongo.db collection called ‘Trips’

The output should be something like (example) :
const data = [[“Igor L”, 6360],
[“Aleksei D”, 836]];

So far my query looks like:

const query = Meteor.users.find().fetch();
const trips = query.map(function(o){
let tripDist = Trips.findOne({"userId": {$eq: o.profile._id}}, {fields: {dist:1}, limit:1}).dist;
console.log(tripDist);
return [o.profile.name_first+" "+o.profile.name_last, tripDist];
});

I do the get profile.name_first & profile.name_last, but I cannot perform the search using the $eq: o.profile._id.
How do I do that?
Here’s my Trips collection:

addTrip: function(day, month, year, car, a, b, dist, cost, odometer, comment){
if(!Meteor.userId()){
	throw new Meteor.Error('No access: You are not logged in.');
}


Trips.insert({
userId: Meteor.userId(),
createdAt: new Date(),
day: day,
month: month,
year: year,
odometer: odometer,
car: car,
a: a,
b: b,
dist: dist,
cost: cost,
comment: comment
});


	//Roles.setUserRoles(Meteor.userId(), 'admin');

},

hi,
I would would open the the mongo console on a running meteor app (in a new console type ‘meteor mongo’). Then play with the find query, till you get the right result.

In the mongo console type:
use meteor
show collections
db.Trips.find({‘userId’: ‘hardcoded_id’}, {‘dist’:1})

Once the query works, put some console.logs in your meteor app to see that o.profile._id gives you the correct form for ‘hardcoded_id’

This is what worked for me

const userQuery = Meteor.users.find().fetch();
const topDist = userQuery.map(function(o){
var sum = 0;
console.log("ids are: "+o._id);
let tripIds = Trips.find({'userId': {$eq: o._id}}).fetch().map(function(x){
sum = sum + x.dist;
});


/*for(let index = 0; index < tripIds.length; ++index){
console.log("Deleting trip "+tripIds[index]);
Meteor.call('delTrip', tripIds[index]);
}*/
return [o.profile.name_first+" "+o.profile.name_last, sum];
});