I made an application here, 60% of the work has already been done. Made it on mean.io with mongodb base. Now here I sit and think how to realize the links between documents.
People say that Mongo is a complete mess to use SQL. But for the sake of one function, you do not want to redo everything.
Plz tell me how you can solve the following problem using mongodb?
For the application of a taxi, it is necessary to fix the car for the driver, the history of drivers must be saved to the collection of drivers.
Model` Driver
var DriversSchema = new Schema({ name: { type: String, required: true, trim: true, unique: true, dropDups: true }, id: { type: Number, required: true, unique: true, dropDups: true }, car: { type: Schema.ObjectId, ref: 'Car' } });
Car model
var CarsSchema = new Schema({ number: { type: String, unique: true, dropDups: true, required: true }, model: { type: String, required: true }, mileage: Number, color: String, isBusy: { type: Boolean, default: false }, drivers: [{ date: Date, driver: { type: Schema.ObjectId, ref: 'Driver', required: true } }] });
I find the driver and save a copy of the car
$scope.findOne = function() { Drivers.get({ driverId: $stateParams.driverId }, function(driver) { console.log('driver ', driver); $scope.driver = driver; $scope.oldCar = driver.car; $scope.cars.unshift(driver.car); }); };
I update the document
$scope.update = function(isValid) { if (isValid) { var driver = $scope.driver; if (!driver.updated) { driver.updated = []; } driver.updated.push(new Date().getTime()); driver.user = MeanUser.user; driver.acl = MeanUser.acl; console.log('update driver ', driver); if(driver.car){ driver.car.isBusy = true; } driver.$update(function() { if($scope.oldCar){ updateCar($scope.oldCar); } $location.path('drivers/' + driver._id); }); } else { $scope.submitted = true; } }; function updateCar(oldCar) { Cars.get({carId: oldCar._id}, function (car) { car.isBusy = false; car.$update(function (response) { console.log('OldCar updated ', response); }) }); }
How to change cars, save drivers who previously went to her?