如何在mongodb中增加id

如何在mongodb中增加id,mongodb,mongoose,Mongodb,Mongoose,这是我的代码,当我从服务器接收到数据时,我需要增加id,它应该是唯一的,并且当我删除id时,id值也必须更新。我遇到了自动增量,但当我删除特定Id时,它不会更新Id。这是他们的任何其他方法,因此我可以增量Id。使用模块 findbyiandremove需要一个\u id字段,但这里您指的是自定义id字段,它显然不起作用。 import mongoose from 'mongoose'; // mongoose Schema is what is used to define attributes

这是我的代码,当我从服务器接收到数据时,我需要增加id,它应该是唯一的,并且当我删除id时,id值也必须更新。我遇到了自动增量,但当我删除特定Id时,它不会更新Id。这是他们的任何其他方法,因此我可以增量Id。

使用模块


findbyiandremove
需要一个
\u id
字段,但这里您指的是自定义
id
字段,它显然不起作用。
import mongoose from 'mongoose';
// mongoose Schema is what is used to define attributes for our documents
var Schema = mongoose.Schema;
//connect to a MongoDB database
var db = mongoose.connect('mongodb://127.0.0.1:27017/student');
//Connection establishment between mongoose and mongodb
mongoose.connect('connected', function() {
    console.log("database connected successfully")
});


var userSchema = new Schema({

    id:{
        type:Number,
            unique:true,
            autoIndexId:true
    },
    Name: {
        type: String,
        required: true,
                unique:false
    },
    Age: {
        type: Number,
        required: true
    }
},   {
    collection: ('studentcollection')
});

var User = mongoose.model('User', userSchema);

function createStudent(name, age, cb) {
    var list = new User({
            id:4,
        Name: name,
        Age: age
    });

    list.save(function(err) {
        if (err) return cb(err);
        return cb();
    });
}

function listStudent(cb) {
    User.find({}, function(err, studentcollection) {
        if (err) return cb(err);
        return cb(null, studentcollection);
    }).select('-__v');
}


exports.createStudent = createStudent;
exports.listStudent = listStudent;

User.findByIdAndRemove(2,function(err){
    if(err) throw err;
    console.log("deleted");
});
var mongoose = require('../lib/mongoose');
    Schema = mongoose.Schema;
    autoIncrement = require('mongoose-auto-increment');
    companySchema = require('./company').Company;

autoIncrement.initialize(mongoose);

var schema = new Schema({
    itemId: {
        type: Number,
    },
    itemName: {
        type: String,
        required: true,
        unique: true
    },
    providerCompany: [{
        type: String,
    }]

});

var productSchema = mongoose.model('Product', schema);

schema.plugin(autoIncrement.plugin, {model: 'Product', field: 'itemId'});
module.exports.Product = productSchema;