Mongoose: how to solve the problem that unique does not take effect and how to remove the unique restriction, mongooseunique

Source: Internet
Author: User

Mongoose: how to solve the problem that unique does not take effect and how to remove the unique restriction, mongooseunique

Preface

Unique is a member of schema Constraints Verification. Its role is to make the value of a field unique (cannot be repeated)

To keep the field unique, use the type value:{type:String,unique:true,dropDups: true}

Note:Once mongoose modifies the data storage organization, the database must be restarted. This is the reason why many new users set some properties to be invalid.

Here, the restart is not simply to shut down the mongoose database server and re-open it, but to delete the entire database and then restart the database service.

Simple schema special usage example

// Import module var mongoose = require ('mongoose'); // connect to the database mongoose. connect ('mongodb: // localhost/itheima '); // create schema // The first parameter of schema is our custom data type. The second parameter is the default data type var studentSchema = mongoose for schema Management. schema ({name: {type: String, required: true}, // the data type is string, and cannot be empty. age: {type: Number, default: 18 }, // The data type is string, the default value is 18 study_id: {type: Number, select: true}, // student ID, the default query field address: {type: String, lowercase: true }, // address, default lowercase email: {type: String, match: RegExp (/^ ([a-zA-Z0-9 _-]) + @ ([a-zA-Z0-9 _-]) + (. [a-zA-Z0-9 _-]) +/)}, // mailbox, regular expression verification phone: {type: String, unique: true, dropDups: true} // phone number uniqueness }, {versionKey: false, // remove the version lock _ v0 timestamps: {createdAt: 'createtime', updatedAt: 'updatetime'} // automatically manage the modification time }); // create modelvar student = mongoose. model ('student ', studentSchema); // create Entityvar zhangsan = new student ({name: 'zhangsan', // The name must be available; otherwise, an error is returned: name: path 'name' is required. address: 'zhongliang ', // The string will be changed to lowercase email: 'a12345 @ qq.com'. // If the email format is incorrect, the "Path 'email 'is invalid (a12345qq.com) will be reported ). study_id: 2017001, phone: '000000' // when a unique field is added, mongoose first queries all the phone values of the database. If the value already exists, an error is returned }); // Add data student. create (zhangsan, function (err) {if (err) {throw err;} console. log ('inserted successfully' + zhangsan );});

Mongoose remove unique restrictions

In the program, the unque restriction is set for the email at the beginning, so that the email cannot be inserted repeatedly in this collection. Now, you want to remove the unique restriction.

db.your_collection.dropIndexes();

Summary

The above is all the content of this article. I hope the content of this article has some reference and learning value for everyone's learning or work. If you have any questions, please leave a message to us, thank you for your support.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.