0

i am attempting to insert an array

Here is my schema:

/**
 * Created by root on 12/4/16.
 */
module.exports = function (mongoose) {
    var hotPrice = mongoose.Schema({
        days: String,
        price: String
    });

    return mongoose.model('hotPrice', hotPrice);
};

And here is my route:

router.route('/createHot')
    .get(function (req, res) {
        var values = [
            {days: 1, price: "63"},
            {days: 2, price: "125"},
            {days: 3, price: "185"},
            {days: 4, price: "250"},
            {days: 5, price: "314"},
            {days: 6, price: "375"},
            {days: 7, price: "425"},
            {days: 8, price: "495"},
            {days: 9, price: "560"},
            {days: 10, price: "620"}
        ];
        mongoose.models.hotPrice(values).insert(function (err, message) {
            res.status(200);
        });
    });

However i get the following:

TypeError: mongoose.models.hotPrice(...).insert is not a function

Can anyone tell me how i can bulk insert this array?

1 Answer 1

2

You can find the answer here: http://mongoosejs.com/docs/api.html#model_Model.create

You can do as follows: (using the example of mongoose docs)

// pass an array
var values = [
            {days: 1, price: "63"},
            {days: 2, price: "125"},
            {days: 3, price: "185"},
            {days: 4, price: "250"},
            {days: 5, price: "314"},
            {days: 6, price: "375"},
            {days: 7, price: "425"},
            {days: 8, price: "495"},
            {days: 9, price: "560"},
            {days: 10, price: "620"}
        ];
YourSchemeObject.create(values, function (err, values) {
  if (err) // ...

})

You also can find a similar question here:

How to save an array of objects to mongoose DB with only one call?

Hope it helps.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.