对于我使用Node.js在MongoDB中遇到的问题,我只是有一个简短的问题。我无法使用命令“ db.users.remove({})”删除集合中的所有文档。我知道集合名称正确。我已经在终端中运行了该命令,尽管看起来好像命令已在运行,但它并没有删除集合中的所有文档。当我使用发布请求运行它时,它只是说db未定义,这对我来说没有意义。我没有查询字符串的原因是我想删除所有文档。我将所有代码都包含在下面,但是如果有人可以告诉我一种删除集合中所有文档的更好方法或解决我的问题的方法,我将不胜感激。谢谢!
const fs = require('fs')
const express = require('express')
const router = express.Router()
const checkTypes = require('checktypes-js')
const schema = require('../schemas/user')
const usersDB = require('../models/usersModel')
const { Types } = require('mongoose')
const fileQuery = "logs.txt"
router.post('/new', (req, res) => {
checkTypes(req.body, schema.user, async (err, passedObject) => {
if (err){
console.log(err)
return res.json({errors: err})
} else {
const existingUserEmail = await usersDB.findOne({email: passedObject.email})
const existingUserUsername = await usersDB.findOne({username: passedObject.username})
console.log(existingUserEmail, existingUserUsername)
if (existingUserEmail || existingUserUsername){
return res.json({error: "A user with that email or username exists too!"})
}
passedObject.active = true
const addedUser = await usersDB.create(passedObject)
res.send(addedUser)
const fileMessage = "\nNew user with username of " + existingUserUsername + ' has been added on ' + new Date()
console.log(fileMessage)
return fs.appendFile(fileQuery, fileMessage, function (err){
if (err){
console.error(err)
}
res.send("The file has been successfully written to!")
})
}
})
})
router.post('/update', (req, res) => {
checkTypes(req.body, schema.user, async (err, passedObject) => {
if (err){
console.log(err)
return res.json({errors: err})
} else {
const {_id, ...update} = passedObject
if(!Types.ObjectId.isValid(_id)){
return res.json({error: 'Invalid id supplied'})
}
const updatedUser = await usersDB.findByIdAndUpdate(_id, update, {new: true})
if (!updatedUser){
return res.json({error: "Could not find the user with that ID!"})
} else {
res.json(updatedUser)
const fileMessage = "\nUpdate user with id of " + _id + ' on ' + new Date()
console.log(fileMessage)
return fs.appendFile(fileQuery, fileMessage, function (err){
if (err){
console.error(err)
}
res.send("The file has been successfully written to!")
})
}
}
})
})
router.post("/delete", (req, res) => {
checkTypes(req.body, schema.user, async (err, passedObject) => {
if (err){
console.log(err)
res.json({error: err})
} else {
const {_id, ...deleteObj} = passedObject
console.log(_id)
if(!Types.ObjectId.isValid(_id)){
return res.json({error: 'Invalid id supplied'})
}
const deleteUser = usersDB.findByIdAndRemove({_id: _id}, function (err){
if (err){
return res.send("A user with that ID is not defined!")
} else {
res.send("The object has been deleted!")
const fileMessage = "\nUser with id of " + _id + ' has been delete on ' + new Date()
console.log(fileMessage)
return fs.appendFile(fileQuery, fileMessage, function (err){
if (err){
console.error(err)
}
res.send("The file has been successfully written to!")
})
}
})
}
})
})
router.post("/deleteAll", (req, res) => {
db.users.remove({})
res.send("All documents have been deleted!")
const fileMessage = "\nAll documents in the remindars collection have been deleted on " + new Date()
console.log(fileMessage)
return fs.appendFile(fileQuery, fileMessage, function (err){
if (err){
console.error(err)
}
res.send("The file has been successfully written to!")
})
})
router.post('/testFile', (req, res) => {
const fileMessage = "\nThe file writing script was tested on " + new Date()
console.log(fileMessage)
return fs.appendFile(fileQuery, fileMessage, function (err){
if (err){
console.error(err)
}
res.send("The file has been successfully written to!")
})
})
router.post('/clearFile', (req, res) => {
const fileMessage = ""
console.log(fileMessage)
return fs.writeFile(fileQuery, fileMessage, function (err){
if (err){
console.error(err)
}
res.send("The file has been successfully written to!")
})
})
module.exports = router
答案 0 :(得分:0)
您有:
const usersDB = require('../models/usersModel')
这是您的猫鼬模型,但您正在呼叫db.users.remove({})
。
像在代码中的其他任何地方一样使用模型:
const result = await usersDB.remove({}).exec()
或:
Author.remove({}).then(x => {
console.log(x)
})