Haugen
Haugen

Reputation: 11

$inc not working for my mongo database (on localhost)

This should be a simple problem. I've formatted my 'update' exactly how it is on mongo's documentation, as you can see:

Message.update(
      { id: messageID },
      { $inc: { votes: 1 } }
    );

I am trying to increase 'votes' by 1. Yet it is not changing. Here is my schema file:

var mongoose  = require('mongoose');

var MessageSchema = new mongoose.Schema({
  room: {
    type: String,
    required: true
  },
  timestamp: {
    type: Object,
    required: true
  },
  votes: {
    type: Number
  },
  id: {
    type: String,
    required: true
  },
  uid: {
    type: String,
    required: true
  },
  parent: {
    type: String,
    required: true
  },
  text: {
    type: String,
    required: true
  }
});

module.exports = mongoose.model('messages', MessageSchema);

The input 'messageID' is correct, as the 'find' method works perfectly well with this table.

Upvotes: 0

Views: 64

Answers (1)

Mariya James
Mariya James

Reputation: 985

Try with findByIdAndUpdate instead of update . Hope it works

Message.findByIdAndUpdate(messageID ,{ $inc: { votes: 1 }},function(err, response) {
res.send(response);
});

Upvotes: 1

Related Questions