Q:

How could I replace the text in my document in mongoose?

I'm trying to replace "switch" to "on" but it's "off" and trying to replace the document with id "1" right now in my database, here is a screenshot. https://imgur.com/a/oE0t3Yc. I don't know how to do that because I'm new to the mongoose.
Here is my Schema.
const mongoose = require('mongoose');

const switchSchema = mongoose.Schema({
    _id: Number,
    switch: String
  });

module.exports = mongoose.model('switch', switchSchema)

And my index.js
async function switchon(){
  const replace = await cmdlogging.findOneAndUpdate(
    { switch: 'on' },
    { new: true }
  );
  await replace.findById(1);
}

The error is:
UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'findById' of null

A:

findOneAndUpdate requires a filter to match your document as the first argument - so in your case - as you're trying to update the document with id 1 - you should change it to:
async function switchon(){
  const updatedDocument = await cmdlogging.findOneAndUpdate(
    { _id: 1 },
    { switch: 'on' },
    { new: true }
  );
  // there's no need to call `findById` again, 
  // as replace holds already the updated document, since you've set { new:true } 
 return updatedDocument;
}

