1

i already gone through below answers:- update multiple records using mongoosejs in node

Update multiple docs with different values

but in my case my query is dynamic , means i need to query dynamically based on obj, I am also not sure if forEach is a goof solution Is there any better alternative for same, as this is taking too much time in each time.

     req.body.forEach(function (obj) {
    myModel.find(
        {
            country: obj.country,
            product: obj.product,
            month: parseInt(obj.month),

        }
    ).update({
            $set: {
                value: parseInt(obj.value),
                'lastmodified': Date.now()
            }
        },
        { upsert: true},
        function (err) {
            //callback();
            if (err) {
                return res.status(500).send(err);
            }
            return res.status(200);
        }
    );
1
  • Is req.body an array or an object with key value pairs? array.forEach works on arrays. for...in statement iterates over the enumerable properties of an object. Maybe if you show more of your code more people might be able to help you Commented Apr 19, 2016 at 7:30

1 Answer 1

0

Don't know if this is helpful but it looked like you weren't sure about parsing req.body to find the documents. This is just pseudo code. Maybe it will give you an idea.

app.put("/post", function(req, res){
    myModel.find({
        country : req.body.country,
        product : req.body.product,
        month : parseInt(req.body.month)
    })
    myModel.save(function(err, results){
        if(err) console.log(err);
        console.log(results)
        res.send(results)
    })
})
Sign up to request clarification or add additional context in comments.

1 Comment

since I am passing multiple objects in one request, req.body.country will not have that info, that is the reason i am using " req.body.forEach(function (obj) {" to parse through each object

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.