Updating values based of element

Hello,

I’m trying to update ‘odometer’ value for on my collection:

Cars = new Mongo.Collection('cars');

It has the following structure

addCar: function(carNum, regDate, mark, model, consump, fuelCost, owner, odometer){
if(!Meteor.userId()){
	throw new Meteor.Error('No access: You are not logged in.');
}

Cars.insert({
userId: Meteor.userId(),
owner: owner,
createdAt: new Date(),
carNum: carNum,
fuelCost: fuelCost,
regDate: regDate,
mark: mark,
model: model,
odometer: odometer,
consump: consump


});

I also have a different Collection called Trips,

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
});

},

What I’m trying to do is

  1. I want to add a trip, with written by me distance.
  2. When I added the distance, it reads the car’s odometer value and sums it with the distance value, adding both to the addTrip, so I know that for that trip I had the following odometer value, and following distance travelled, and when I look at Cars collection I see that the odometer value for the used recently car is updated.
  3. I want to update the car’s odometer value, but I guess I can’t use elementId, I need to find it via value named car (Trips) and carNum (Cars).
updateOdometer: function(elementId, value){
if(!Meteor.userId()){
	throw new Meteor.Error('No access: You are not logged in.');
}

Cars.update(elementId, {
      $set: { odometer: value },
    });

},

  1. I need to remove the distance travelled upon Trip removal
    Here’s what I have right now
delTrip: function(tripId){
	Trips.remove(tripId);
}

Solved it. This is what I used

var odoValue = Cars.findOne({"carNum" : {$eq: car}}, {fields: {odometer:1}, sort: {createdAt: -1}, limit: 1}).odometer;

var carId = Cars.findOne({"carNum" : {$eq: car}}, {fields: {_id:1}, sort: {createdAt: -1}, limit: 1})._id;

var cost = ((consump * dist / 100) / fuelCost).toFixed(2);




if(odoValue != null){
var odoNew = parseFloat(odoValue) + parseFloat(dist);
console.log("Old/New odo value: "+odoValue+" / "+odoNew);
Meteor.call('updateOdometer', carId, odoNew);
} else {
	Bert.alert("Odometer value is empty. Please delete car and readd it", 'info' );
}

Using this to delete/update values:

"click .delete-trip": function(event){
	if(confirm('Delete Trip?')){

		var odoValue = Trips.findOne({"_id" : {$eq: this._id}}, {fields: {odometer:1}, sort: {createdAt: -1}, limit: 1}).odometer;
		var car = Trips.findOne({"_id" : {$eq: this._id}}, {fields: {car:1}, sort: {createdAt: -1}, limit: 1}).car;
		var distance = Trips.findOne({"_id" : {$eq: this._id}}, {fields: {dist:1}, sort: {createdAt: -1}, limit: 1}).dist;

		if(odoValue != "" && odoValue != null){
		var carId = Cars.findOne({"carNum" : {$eq: car}}, {fields: {_id:1}, sort: {createdAt: -1}, limit: 1})._id;
        var newOdo = parseFloat(odoValue) - parseFloat(distance);
        if(newOdo >= 0)
        Meteor.call('updateOdometer', carId, newOdo);
    else
    Bert.alert('Could not change the odometer value', 'warning' );

		}

		Meteor.call('delTrip', this._id);
		Bert.alert('Selected trip has been deleted from the database', 'warning' );
	}
}