0

I've got an stock application where I want to set some details about the stock and then insert all the items of the stock. I want to insert the stock details and the items in two different collection so then I can filter the items. I'm using the MEAN Stack where I've modified the crud module to accept some extra fields and also made the UI for filling the items array.This what I have so far:

scope.stockItems = [];

    $scope.createStockItem = function () {
        $scope.stockItems.push(
            {
                brand: $scope.brand,
                style: $scope.style,
                amount: $scope.amount
            }
        );

        $scope.brand = false;
        $scope.style = false;
        $scope.amount = '';
    };

    // Create new Stock
    $scope.create = function() {
        // Create new Stock object
        var stock = new Stocks ({
            name: this.name,
            details: this.details,
            stockDate: this.stockDate
        });

        // Redirect after save
        stock.$save(function(response) {
            $location.path('stocks/' + response._id);

            // Clear form fields
            $scope.name = '';
        }, function(errorResponse) {
            $scope.error = errorResponse.data.message;
        });
    }; 

The stock model:

var StockSchema = new Schema({
name: {
    type: String,
    default: '',
    required: 'Please fill Stock name',
    trim: true
},
details: {
    type: String,
    default: '',
    required: 'Please fill Stock details'
},
stockDate: Date
created: {
    type: Date,
    default: Date.now
},
user: {
    type: Schema.ObjectId,
    ref: 'User'
}
});

and the method in the server controller:

exports.create = function(req, res) {
var stock = new Stock(req.body);

stock.user = req.user;

stock.save(function(err) {
    if (err) {
        return res.status(400).send({
            message: errorHandler.getErrorMessage(err)
        });
    } else {
        res.jsonp(stock);
    }
});
};

How can I send into the request and save the stockItems also?

Facu Ferrari
  • 145
  • 1
  • 2
  • 17

1 Answers1

0

By saying 'simultaneously' I think you are requiring transaction feature, which is really an RDBMS thing, and is not supported by MongoDB. If your application strongly relies on such features, I'm afraid MongoDB is not the right choice for you.

So back to your question, I don't understand why you have to store stock and stock item in 2 different collections. Store them in one collection would be a better choice. You can refer to the Data Model Design of MongoDB Manual for more information. If it's just to filter all the stock items, aggregation framework is designed for such purpose. As well as Map/Reduce. Here aggregation framework suits better for your issue. You would have something like:

db.stock.aggregate([
  {$match: {...}},  // same as find criteria. to narrow down data range
  {$unwind: "$items"},  // unwind items.
  ... // probably some other $match to filter the items
]);
yaoxing
  • 4,003
  • 2
  • 21
  • 30
  • so you I can use the denormalized data model and embed the stockItems inside Stocks. But using this model can I query forr all the red stockItems from all stocks? – Facu Ferrari Sep 19 '14 at 09:49
  • @FacuFerrari Yes I mean you can store stock items in embeded document so that when you save them it's atomic. I don't really know how are you going to query the 'red stockItems'. Do you have another collection of item stocks, and each record represents an stock of a specific item? – yaoxing Sep 21 '14 at 07:15