Home > Web Front-end > JS Tutorial > body text

Detailed explanation on how to solve the problem of unique settings in mongoose not taking effect and how to remove unique restrictions

小云云
Release: 2017-12-26 13:25:01
Original
1942 people have browsed it

This article mainly introduces to you the solution to the problem that unique settings in mongoose database do not take effect, as well as examples of how to remove unique restrictions in Mongoose. The article introduces it in detail through sample code. Friends who need it can refer to it. Below Let’s learn together with the editor.

Preface

unique is a member of schema constraint verification. Its main function is to make the value of a certain field unique (cannot be repeated)

To maintain the uniqueness of fields, use type values: {type:String,unique:true,dropDups: true}

Note: Once mongoose changes the data storage mechanism, the database must be restarted. Many novices This is the reason why setting some properties does not take effect

The restart mentioned here is not simply closing the mongoose database server and reopening it, but first deleting the entire database and then restarting the database service

Simple schema special usage example

//导入模块

var mongoose = require('mongoose');

//连接数据库
mongoose.connect('mongodb://localhost/itheima');

//创建schema

//schema第一个参数是我们自定义的数据类型 第二个参数是管理schema默认的数据类型
var studentSchema = mongoose.Schema({
 name:{type:String,required:true},//数据类型为string,不能非空
 age:{type:Number,default:18},//数据类型为string,默认值18
 study_id:{type:Number,select:true},//学号,默认查询字段
 address:{type:String,lowercase:true},//地址,默认小写
 email:{type:String,match:RegExp(/^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(.[a-zA-Z0-9_-])+/)},//邮箱,正则表达式验证
 phone:{type:String,unique:true,dropDups: true}//电话号码唯一性
},{
  versionKey: false,//去掉版本锁 __v0

 timestamps: { createdAt: 'createTime', updatedAt: 'updateTime' }//自动管理修改时间

});

//创建model

var student = mongoose.model('student',studentSchema);

//创建Entity

var zhangsan = new student({
 name:'zhangsan',//名字必须要有,否则会报错: name: Path `name` is required.
 address:'ZhongLiang',//字符串都会变成小写
 email:'a12345@qq.com',//邮箱格式不对,添加会报错 Path `email` is invalid (a12345qq.com).
 study_id:2017001,
 phone:'123456789'//在添加唯一性字段时,mongoose会先查询数据库所有的phone值,一旦发现该值已存在则会报错
});

//添加数据



student.create(zhangsan,function(err){

 if(err){
  throw err;
 }

 console.log('插入成功' + zhangsan);

});
Copy after login

Mongoose removes unique restrictions

The email in the program initially set the unique restriction, which caused the email to not be repeatedly inserted into this collection. Now Want to remove unique restrictions.

db.your_collection.dropIndexes();
Copy after login

Related recommendations:

Two methods of mongoose updating objects

Detailed explanation of Mongoose’s virtual field query implementation method

Node.js MongoDB driver Mongoose basic usage tutorial_node.js

The above is the detailed content of Detailed explanation on how to solve the problem of unique settings in mongoose not taking effect and how to remove unique restrictions. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!