gpt4 book ai didi

node.js - 更新项目时如何防止 mongoDB 中的重复项目

转载 作者:太空宇宙 更新时间:2023-11-03 23:15:13 25 4
gpt4 key购买 nike

我正在创建一个多用户词典,多个用户可以在其中添加单词。我不希望同一个词有重复。我能够在添加单词时防止重复,但在更新单词时却难以使用防止重复的功能。例如,数据库中有一个单词“apple”,让我们想象一下有人想要更新单词“apply”并且不小心写了“apple”,在这种情况下,更新的单词“apple”不应该进入数据库,因为那里已经有这个单词了。请帮我。

Words API

const express = require('express');
const router = express.Router();
const Word = require('../../models/Word');
const validateWordInput = require('../../validation/word');
const passport = require('passport');

// @route GET api/words/test
// @desc tests words route
// @access Public
router.get('/test', (req, res) => res.json({ msg: 'Words works' }));

// @route POST api/words
// @desc Add words to profile
// @access Private
router.post(
'/',
passport.authenticate('jwt', { session: false }),
(req, res) => {
const { errors, isValid } = validateWordInput(req.body);

// Check validation
if (!isValid) {
// Return any errors
return res.status(400).json(errors);
}

Word.find({}).then(word => {
if (
word.filter(
wrd =>
wrd.ugrWordCyr.toString().toLowerCase() ===
req.body.ugrWordCyr.toLowerCase()
).length !== 0
) {
return res
.status(404)
.json({ wordalreadyexists: 'Word already exists' });
} else {
const newWord = new Word({
user: req.user.id,
ugrWordCyr: req.body.ugrWordCyr,
rusTranslation: req.body.rusTranslation,
example: req.body.example,
exampleTranslation: req.body.exampleTranslation,
origin: req.body.origin,
sphere: req.body.sphere,
lexis: req.body.lexis,
grammar: req.body.grammar,
partOfSpeech: req.body.partOfSpeech,
style: req.body.style
});

newWord.save().then(word => res.json(word));
}
});
}
);

// @route Put api/words/:id
// @desc Update a word by id
// @access Private

router.put(
'/:id',
passport.authenticate('jwt', { session: false }),
(req, res) => {
const { errors, isValid } = validateWordInput(req.body);

// Check validation
if (!isValid) {
// Return any errors
return res.status(400).json(errors);
}

Profile.findOne({ user: req.user.id }).then(profile => {
Word.findById(req.params.id)
.then(word => {
// Check for word owner
if (word.user.toString() !== req.user.id) {
return res
.status(401)
.json({ notauthorized: 'User not authorized' });
}

const wordID = req.params.id;
const wordInput = req.body;

// Update
Word.findByIdAndUpdate(
{ _id: wordID },
{ $set: wordInput },
{ returnOriginal: false },
(err, word) => {
if (err) {
console.log(err);
}
}
).then(word => {
res.json(word);
});
})
.catch(err => res.status(404).json({ nowordfound: 'No word found' }));
});
}
);

// @route GET api/words
// @desc Dislay all words
// @access Public
router.get('/', (req, res) => {
Word.find()
.sort({ date: -1 })
.then(words => res.json(words))
.catch(err => res.status(404).json({ nonwordsfound: 'No words found' }));
});

//@route Get api/words/:id
//@desc Get word by id
//@access Public
router.get('/:id', (req, res) => {
Word.findById(req.params.id)
.then(word => res.json(word))
.catch(err =>
res.status(404).json({ nonwordfound: 'No word found with that ID' })
);
});

//@route DELETE api/words/:id
//@desc DELETE word
//@access Private

router.delete(
'/:id',
passport.authenticate('jwt', { session: false }),
(req, res) => {
Profile.findOne({ user: req.user.id }).then(profile => {
Word.findById(req.params.id)
.then(word => {
// Check for post owner
if (word.user.toString() !== req.user.id) {
return res
.status(401)
.json({ notauthorized: 'User not authorized' });
}

// Delete
word.remove().then(() => res.json({ success: true }));
})
.catch(err => res.status(404).json({ postnotfound: 'No post found' }));
});
}
);

module.exports = router;

我使用以下代码来防止添加重复。

   if (
word.filter(
wrd =>
wrd.ugrWordCyr.toString().toLowerCase() ===
req.body.ugrWordCyr.toLowerCase()
).length !== 0
) {
return res
.status(404)
.json({ wordalreadyexists: 'Word already exists' });
} else {
const newWord = new Word({
user: req.user.id,
ugrWordCyr: req.body.ugrWordCyr,
rusTranslation: req.body.rusTranslation,
example: req.body.example,
exampleTranslation: req.body.exampleTranslation,
origin: req.body.origin,
sphere: req.body.sphere,
lexis: req.body.lexis,
grammar: req.body.grammar,
partOfSpeech: req.body.partOfSpeech,
style: req.body.style
});

我应该编写什么代码来在更新时执行相同的操作?

最佳答案

在执行 Word.findByIdAndUpdate() 之前,您可以检查 req.body 中的文本是否与数据库中的任何现有单词匹配。

// @route  Put api/words/:id
// @desc Update a word by id
// @access Private

router.put(
'/:id',
passport.authenticate('jwt', { session: false }),
(req, res) => {
const { errors, isValid } = validateWordInput(req.body);

// Check validation
if (!isValid) {
// Return any errors
return res.status(400).json(errors);
}

Profile.findOne({ user: req.user.id }).then(profile => {
Word.findById(req.params.id)
.then(word => {
// Check for word owner
if (word.user.toString() !== req.user.id) {
return res
.status(401)
.json({ notauthorized: 'User not authorized' });
}

const wordID = req.params.id;
const wordInput = req.body;

//find all words
Word.find()
.then((allWords) => {
//create an array of strings using each word ("apple", "apricot", ...etc)
const wordStrings = allWords.map((word) => word.ugrWordCyr) //not sure if thats the property that has the word-spelling

//check if user input already exists in all words
if(wordStrings.includes(req.body.ugrWordCyr)){
return res.status(400).json({ error : "word already exists" })
}

// Update
Word.findByIdAndUpdate(
{ _id: wordID },
{ $set: wordInput },
{ returnOriginal: false },
(err, word) => {
if (err) {
console.log(err);
}
}).then(word => {
res.json(word);
});

})
.catch((errors) => {
return res.status(400).json({ errors: "could not find any words" })
})

})
.catch(err => res.status(404).json({ nowordfound: 'No word found' }));
});
}
);

或者,您也可以更新您的 Word 模型并在设置 mongoose 架构时使用 unique 属性。我想你的架构看起来像这样:

const mongoose = require("mongoose")

const wordSchema = new mongoose.Schema({
user: {
type: mongoose.Schema.Types.ObjectId,
ref: "User"
},
ugrWordCyr: { type: String, unique: true}, <-- set unique to true
rusTranslation: { type: String },
example: { type: String },
exampleTranslation: { type: String },
origin: { type: String },
sphere: { type: String },
lexis: { type: String },
grammar: { type: String },
partOfSpeech: { type: String },
style: { type: String }
})

const Word = mongoose.model("Word", userSchema)

module.exports = Word

现在,当您使用 findByIdAndUpdate() 时,除非您将新的/唯一的字符串传递给 ugrWordCyr 或任何您用作显式单词的内容,否则它将无法完成。

      Word.findByIdAndUpdate(
{ _id: wordID },
{ $set: wordInput },
{ returnOriginal: false },
(err, word) => {
if (err) {
console.log(err);
}
}
).then(word => {
res.json(word);
});
.catch(err => {
return res.status(400).json({ error: "could not update word" })
})
})

关于node.js - 更新项目时如何防止 mongoDB 中的重复项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56320856/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com