не могу удалить документы в mongodb используя mongoose в nodejs - PullRequest
0 голосов
/ 25 мая 2019

Я пытаюсь удалить все документы, которые имеют состояние, равное off, когда происходит / ledon, и удалить документы, которые имеют состояние = on, когда / ledoff маршруты встречаются в атласе mongodb, код которого показан ниже

Я пытался использовать modelname.remove, modelname.deleteMany, но у меня не работает в консоли, он показывает удаленный, но в атласе mongodb он не удаляет документы

const mongoose = require('mongoose')
const app = express()
const router = express.Router()
const path=require('path')

const data = require('../../models/data')

router.post('/ledon',(req,res)=>{
   const newdata1 = new data({
       state:'ledon'
   })
   newdata1.save()
   .then(console.log('done'))
   .catch(err=>console.log(err))
   data.find({state:'ledoff'})
   .then((isfind)=>{
       if(isfind){
           data.deleteMany({state:'ledoff'}).then(()=>{
               console.log('deleted')
           }).catch(err=>console.log(err))
       }
   })
   .catch(err=>console.log(err))
})

router.post('/ledoff',(req,res)=>{
    const newdata2 = new data({
        state:'ledoff'
    })
    newdata2.save()
    .then(console.log('done'))
    .catch(err=>console.log(err))
    data.find({state:'ledon'})
    .then((isfind)=>{
        if(isfind){
            data.deleteMany({state:'ledon'}).then(()=>{
                console.log('deleted')
            }).catch(err=>console.log(err))
        }
    })
    .catch(err=>console.log(err))

})

module.exports=router```

it is not deleting the documents in the mongodb atlas but in console it is showing that it is deleting the documents can anyone help me with this problem

my schema design look like this
```const moongose=require('mongoose')
const schema = moongose.Schema

const data =new schema({
    state:{
        type:String,
        default:true,
        required:true
    }
})

module.exports=databack = moongose.model('buttondata',data)```
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...