[英]MongoDB: remove unique constraint
我正在嘗試“使用帶有 Express 和 Mongoose 的 Node.js 開發 RESTful API”示例,但遇到了 MongoDB 架構的問題:
POST:
{ title: 'My Awesome T-shirt 2',
description: 'All about the details. Of course it\'s black.',
style: '12345' }
{ [MongoError: E11000 duplicate key error index: ecomm_database.products.$style_1 dup key: { : "12345" }]
name: 'MongoError',
err: 'E11000 duplicate key error index: ecomm_database.products.$style_1 dup key: { : "12345" }',
模式定義中有一個獨特的約束:
var Product = new Schema({
title: { type: String, required: true },
description: { type: String, required: true },
style: { type: String, unique: true },
modified: { type: Date, default: Date.now } });
我該如何擺脫它? 當我刪除 unique: true 並重新啟動應用程序時,架構不會更新。
mongodb 如何處理模式的“更改”?
“mongodb 如何處理對架構的“更改”?”
MongoDB 是無模式的
唯一強制執行的唯一事情是在索引級別,您可以在其中定義具有唯一條件的集合的索引。 因此,您可能希望刪除相關索引並在必要時重新創建它。
我希望這會有所幫助
db.collection_name.getIndexes()
返回一個數組,其中包含標識和描述集合中現有索引的文檔列表。 現在從列表中找到您的索引名稱,然后刪除該索引,如下所示
db.users.dropIndex("your_index_name_here")
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.