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.

Drivers Drivers

Cars enter image description here

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?

  • one
    If you keep the history of drivers is not necessarily in the drivers document, then you can put it in a separate collection. Where each document will be a point in history - show the driver, car and date of change - Vladimir Gamalyan
  • 3
    Well, in general, correctly told you about Mongu said. You have not yet considered your code in terms of transactions. And when, for example, between the update of the documents of the driver and the machine, the server crashes (or the second operation fails in Mong), you get an inconsistent state. Use Mongu to store the passengers' comments about the drivers, and for important information - to follow. Otherwise there will be a lot of extra work to build a reliable system. - Vladimir Gamalyan

1 answer 1

For such things, you can store in the driver's history only a unique identifier of the car and the necessary given, rather than real car object:

 "driver" : { "_id": { "$oid": "58eb8bb010d68b0ffc070063" }, name: "Иванов Иван Иванович", car: "54b8bb610d68b345c070078", history: [ { "timeStamp": { "$date": "2017-04-18T08:55:08.560Z" }, "carId": "54b8bb610d68b345c070078", "id": 1 } ] } 

The unique identifier and will serve as a link on the backend (link). Similarly, you can make and history of the car, and in some field "currentDriver" store the id of the current driver.