1

I am very new to NoSQL world. Here the screenshot of my Mongoose schema.

mongoose schema

I need to insert,update and delete document to/from vehicles array.

What I have tried so far:

Add: (Working)

Companies.findOne({'mobile' : givenMobileNumber, 'VehicleGroups.vehicle_group_id' : vehicleGroupId}, (err, res) => {
    if( err  || res == null) {
        callback(err, res);
    }else{

        var len =  res.VehicleGroups.length;
        for(var i=0; i<len; i++)
        {
            if(res.VehicleGroups[i].vehicle_group_id == vehicleGroupId)
                res.VehicleGroups[i].vehicles.push(data);
        }
        res.save(callback);
    }
})

Delete: (Working)

Companies.findOneAndUpdate({ mobile : givenMobileNumber, 'VehicleGroups.vehicle_group_id' : vehicleGroupId},
{ $pull : {'VehicleGroups.$.vehicles' : { 'vehicle_id' :  vehicleId} } }, callback);

Still working to update data. Is my approach valid?

Thanks

Shihab
  • 2,641
  • 3
  • 21
  • 29
  • You should show the community what you have tried, and where it fails. Try coming up with a minimal, complete, and verifiable example. https://stackoverflow.com/help/mcve – Greg Schmit Jan 18 '18 at 00:43
  • @GregSchmit I have updated my question. Thanks – Shihab Jan 18 '18 at 09:39
  • 1
    That's a much better question! If no one answers and you figure it out, feel free to [answer your own question](https://stackoverflow.com/help/self-answer), because the current one is not acceptable IMO. That way if others have this question, they will be more likely to find it. – Greg Schmit Jan 18 '18 at 09:43
  • @Veeram Sorry for mentioning but I am stuck. – Shihab Jan 19 '18 at 22:14
  • 1
    Sorry, didn't get notified. I happen to stumble upon your post. For insert you can try `Companies.findOneAndUpdate({ mobile : givenMobileNumber, 'VehicleGroups.vehicle_group_id' : vehicleGroupId}, { $push: {'VehicleGroups.$.vehicles' : data } }, callback);`. For updating vehicle you can use arrayFilters ( available from 3.6) `Companies.findOneAndUpdate( { mobile : givenMobileNumber}, { $inc: { "VehicleGroups.$[vg].vehicles.$[v].status": "new status" } }, { arrayFilters: [ { "vg.vehicle_group_id": vehicleGroupId } , { "v.vehicle_id":vehicleId} ], multi: true}, callback );` – s7vr Jan 22 '18 at 03:24

1 Answers1

1

You can consider setting up separate schemas for your VehicleGroups and vehicles and reference them through ObjectId in your Companies schema:

VehicleGroups: [{
  _id: { type: Schema.Types.ObjectId, ref: 'vehicleGroup' },
  // All other properties here
  vehicles: [{ type: Schema.Types.ObjectId, ref: 'vehicle' }]
}]

Adding a new document to theVehicleGroup schema would then go something like this:

const newVehicleGroup = new VehicleGroup({
  name: 'New Vehicle',
  // Set all other fields
})

And adding it to the Companies schema:

Companies.findOneAndUpdate({
  mobile : givenMobileNumber, 'VehicleGroups.vehicle_group_id' : vehicleGroupId,
  { $push: { vehicleGroups: newVehicleGroup._id } }
})

Since you reference vehicles and VehicleGroups by ObjectId, all you have to do to update that reference is to update the document in the respective collection.

However, if you delete a document you will need to remove its reference from the respective array in the Companies collection.

See if this approach makes it a little easier!

JazzBrotha
  • 1,628
  • 11
  • 15