[PainPoint] Mongoose ์คํค๋ง ํ์ ์ฃผ์

๐ฅ client ์์ POSTํ ๋ฐ์ํ ์๋ฌ
- client ์์ ์ ๋ณด๋ฅผ POSTํ๋ ์ค ์๋์ ๊ฐ์ ์ค๋ฅ๊ฐ ๋ฐ์ํ๋ค.
MongoServerError: E11000 duplicate key error collection: blog.users index: userID_1 dup key: { userID: null }
at InsertOneOperation.execute (/Users/leeyunjae/programming/react/react_study/blogpractice/api/node_modules/mongodb/lib/operations/insert.js:51:19)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
at async executeOperation (/Users/leeyunjae/programming/react/react_study/blogpractice/api/node_modules/mongodb/lib/operations/execute_operation.js:136:16)
at async Collection.insertOne (/Users/leeyunjae/programming/react/react_study/blogpractice/api/node_modules/mongodb/lib/collection.js:155:16) {
errorResponse: {
index: 0,
code: 11000,
errmsg: 'E11000 duplicate key error collection: blog.users index: userID_1 dup key: { userID: null }',
keyPattern: { userID: 1 },
keyValue: { userID: null }
},
index: 0,
code: 11000,
keyPattern: { userID: 1 },
keyValue: { userID: null },
[Symbol(errorLabels)]: Set(0) {}
}
์ฐ์ต์ ์ํด ๊ธฐ์กด์ MongoDB๋ฅผ ๊ทธ๋๋ก ํ์ฉํ์ฌ ๊ธฐ๋ฅ๊ตฌํ์ ํ๋ ์ค ๊ต์ก์ค ์์ฑํ ์๋ณธ์ userID๊ฐ์ด NULL์ด๋ผ ๋ฌธ์ ๊ฐ ๋ฐ์ํ๋ค. ๋ณ๋๋ก ์๋ณธ์ ์คํ์ํค์ง๋ ์์๊ธฐ ๋๋ฌธ์ ์ด๋ ๋ถ๋ถ์์ ์๋ณธ์ ๊ฐ์ ํ์๋กํ๋์ง ์์๋ด์ผํ๋ค.
๐ฅบ ์๋ํ ํด๊ฒฐ๋ฐฉ๋ฒ (1)
ํ๋ก๊ทธ๋จ์ ๊ป๋ค๊ฐ ์ผ๋ณด์!
์ฐพ์๋ณธ ๊ฒฐ๊ณผ ์ข
์ข
์ด์ ๋ฐ์ดํฐ๊ฐ ์ผ๋ถ ๋จ์ ์ค๋ฅ๋ฅผ ๋ฐ์ํ๋ ๊ฒฝ์ฐ๊ฐ ์๋ค๊ณ ํ๋ค. ์ด๋ฐ ๊ฒฝ์ฐ๋ VSC๋ฅผ ์ข
๋ฃ ํ ์ฌ์คํํ๋ฉด ํด๊ฒฐ๋๋ ๊ฒฝ์ฐ๊ฐ ์๋ค๊ณ ํ๋ค.
๊ทธ๋ฌ๋ ์ฌ์คํ์ ํด๋ ๋ฌธ์ ๋ ํด๊ฒฐ๋์ง ์์๋ค...
๐ฅบ ์๋ํ ํด๊ฒฐ๋ฐฉ๋ฒ (2)
MongoDB์ ๊ธฐ์กด์ ๋ฐ์ดํฐ๋ฅผ ๋ค ์ญ์ ํด๋ณด์!
์๋ณธ์์๋ userID์ ์ฌ์ฉํ์์ผ๋ ์ฐ์ตํ ๋๋ username์ผ๋ก ์คํค๋ง๋ฅผ ๊ตฌ์ฑํ๊ธฐ ๋๋ฌธ์ ํน์ ํ๋์ ๋ฐ์ดํฐ๋ฒ ์ด์ค ์ปฌ๋ ์
๋ด๋ถ์ ์คํค๋ง์ ๊ตฌ์ฑ์ด ๋ฌ๋ผ ์๊ธฐ์น ๋ชปํ ์ค๋ฅ๊ฐ ๋ฐ์ํ๊ฒ์ธ๊ฐ ๋ผ๋ ์๊ฐ์ํ๊ฒ ๋์ด ํ๋ฒ ๊ธฐ์กด์ ๋ฐ์ดํฐ๋ฅผ ์ญ์ ํ๊ณ ๋ค์ ๊ตฌ์ฑํด๋ณด๊ธฐ๋ก ํ๋ค.
ํ์ง๋ง, ์ด์ ์๋ ์ฐ์ต์ ์ํด ์ถ๊ฐ ๋ฐ์ดํฐ ํ์์ด ํ์ํ ๋ ๋ง๋ค ํ๋์ฉ ์คํค๋ง์ ์ถ๊ฐํ ์ ์ด ์๊ธฐ ๋๋ฌธ์ ๋ฐ์ดํฐ๋ฒ ์ด์ค ๋ด๋ถ์์ ์คํค๋ง๊ฐ ๋ค๋ฅด๋ค๊ณ ๋ฌธ์ ๊ฐ ๋ฐ์ํ๋ ๊ฒ์ ๊ฒฝํํ์ ์์๊ณ , ์ญ์๋ ์ด ๋ฐฉ๋ฒ์ผ๋ก๋ ๋ฌธ์ ๋ ํด๊ฒฐ๋์ง ์์๋ค.
๐ฅณ ํด๊ฒฐ๋ฐฉ๋ฒ (์ต์ข )
๋ฌธ์ ๋ฅผ ์ฐพ์๋ณธ ๊ฒฐ๊ณผ Stackoverflow ์์ ๋์ผํ ๋ฌธ์ ์ ๋ํด ํ ๋ก ํ ๋ถ๋ถ์ ์ฐพ์๋ค. ๊ทธ๊ณณ์์ ์ด์ผ๊ธฐํ ๋ด์ฉ์ ๋ฐ๋ฅด๋ฉด ์ฌ์ฉํ์ง ์๋ indexes ๋ ์ญ์ ๋ฅผ ํด์ผ ์ด ์ค๋ฅ๊ฐ ๋ฐ์ํ์ง ์๋๋ค๋ ์ค๋ช
์ ํ๋ค.
Database -> Collections -> (๋ฌธ์ ์ ์ปฌ๋ ์
์์ญ) -> Indexes ์ ๊ฒฝ๋ก๋ก ๋ค์ด๊ฐ ์ฌ์ฉํ์ง ์๋ userID๋ฅผ ์ญ์ ํด์ฃผ๋
๋ฌธ์ ๊ฐ ํด๊ฒฐ๋์๋ค.
์ฐพ์๋ณธ ๊ฒฐ๊ณผ mongoose์์ ์คํค๋ง๋ฅผ ์ค์ ํ๋ฉด์ required ๋ฅผ true๋ก ์ค์ ํ create๋ฅผ ์งํํ๋ฉด ๋ฐ์ดํฐ๋ฒ ์ด์ค์ indexes์์ญ์์ ๋ณด๋ฉด required๊ฐ ์ ์ฉ๋จ์ ์ ์ ์์๋ค.
๊ฒฐ๋ก ์ ์ผ๋ก ๋ฌธ์ ๋ mongoose ์คํค๋ง์์ ์ค์ ํ required๊ฐ ๋ฐ์ดํฐ๋ฒ ์ด์ค์๋ ์ํฅ์ ์ค์ ํด๋น ๊ฐ์ ๋ฌด์กฐ๊ฑด ์
๋ ฅํด์ผํ๋ ์ํ๊ฐ ๋๊ฒ์ด๋ค. ๊ทธ๋ ๊ธฐ์ ๋ณ๋๋ก ๊ทธ ๊ฐ์ ํ์๋กํ๋ ํ๋ก๊ทธ๋จ์ ์คํ์ํค์ง ์์๋ ๋ฐ์ดํฐ๋ฒ ์ด์ค ์์ฒด์ ์คํค๋ง๊ฐ ์ค์ ๋๊ฒ์ผ๋ก ํ๋ก๊ทธ๋๋ฐ์ ๋ฐ์ดํฐ๋ฒ ์ด์ค์ ๋ง๊ฒ ์์ฑํ ํ์๊ฐ ์๋๊ฒ์ด๋ค.
๐โโ๏ธ ๋๋์
์ฌ์ค ํ๋์ ๋ฐ์ดํฐ๋ฒ ์ด์ค๋ฅผ ์ฐ์ต์ ์ํด ๋ค๋ฅธ ์ฝ๋์ ์ฐ๊ฒฐํ๋ ๊ฒฝ์ฐ๋ฅผ ์ ์ธํ๊ณ ๋ ์ด๋ฐ ๋ฌธ์ ๊ฐ ๋ฐ์ํ ์ผ์ด ์์๊น ์ถ์ง๋ง, ์ด๋ฒ ๋ฌธ์ ๋ก ์คํค๋ง์ ํ์
์ ํน์ฑ์ ๋ค๋ฃฐ ๋ ๊ณ ๋ คํด์ผํ ๋ถ๋ถ์ ์ต๋ํ๊ณ , ํ๋ก๊ทธ๋๋ฐ์ ์งํํ ๋ ๊ท์น์ ์ผ๊ด์ฑ์ ์ค์์ฑ์ ๋ค์ํ๋ฒ ์ฒด๊ฐํ ์ ์์๋ค.
์ฐธ๊ณ ์ฌ์ดํธ
https://stackoverflow.com/questions/24430220/e11000-duplicate-key-error-index-in-mongodb-mongoose
E11000 duplicate key error index in mongodb mongoose
Following is my user schema in user.js model - var userSchema = new mongoose.Schema({ local: { name: { type: String }, email : { type: String, require: true, unique: true }, ...
stackoverflow.com