1

First I am making entries in mongodb by:

var device = new Device(inputDetailsJson); //device model instance

device.save(function(err) { 
                            if (err) {
                                res.send(err);
                            } else {
                                res.write('Successful');
                                res.end();
                            }

                        });

This is the device model:

// Dependencies
var mongoose = require('mongoose');
var Schema = mongoose.Schema;

// Schema Definition
var deviceSchema=new Schema ({
    "deviceId":{ type : String , unique: true},
    "version":{ type : String },
    "manufacturer":{ type : String },
    "modelname":{ type : String },


});

// Model
module.exports = mongoose.model('de', deviceSchema);

This is the inputDetailsJson:

{   
            "deviceId":"3236172417",
            "version":"5.2.3",
            "manufacturer":"abc",
            "modelname":"example"
}

These details are getting entered correctly, but when I try to update something, it reflects no changes.

var device=new Device();
device.update({deviceId:"3236172417"},{modelname:"test"}
,function(err) {
if (err) {
            return console.error(err);
        }
else{
res.write('successful');
res.end();

It displays 'successful' even though no changes are made in the mongodb database.

2 Answers 2

3
var Device = require('your device model file');
Device.model.findOneAndUpdate({
       deviceId:"3236172417",
},{$set:{modelname:"test}},function(err, user) {

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

2 Comments

TypeError: Device.findOneAndUpdate is not a function
upvoted, but I have less reputation so it wont be displayed publicly
0

For updating the device you don't need to create a new device, using var device=new Device();

you should simply update, same as you try to find a document.

Device.update({deviceId:"3236172417"},{modelname:"test"}
,function(err) {
if (err) {
            return console.error(err);
        }
else{
res.write('successful');
res.end();

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.