I am newbie to mongoose, I have a mongoose schema like this:
var user = mongoose.Schema({
userID: {
type: String,
required:true
},
seq: {
type: Number,
default: 0
},
firstname: {
type: String
},
lastname: {
type: String
},
dob: {
type: String
},
email: {
type: String,
required: true
},
username: {
type: String,
required: true
},
displayname: {
type: String
},
password: {
type: String,
required: true
},
mobile: {
type: String
},
profilePic: {
type: String
},
city: {
type: String
},
gender: {
type: String
},
profileType: {
type: String,
required: true
},
profileId: {
type: String
},
isActive: {
type: Number
},
ageVerified: {
type: String
},
ipAddress: {
type: String
},
key: {
type: String
},
osType: {
type: String
},
osVersion: {
type: String
},
deviceName: {
type: String
},
joinedDate: {
type: String
},
connectedAccounts: [{
profileType: {
type: String
},
profileId: {
type: String
},
email: {
type: String
}
}]
}, {collection: 'user'});
Please note the userID is an auto increment number field, for inserting value am using the mongoose query like:
new user(contents).save(function (err,doc){};
'contents' is a object, which contain data for all the field except userID, here my question is how to insert value for the userID(autoincrement number) while inserting records for other fields? And I refer this linkto set the auto increment value... But I don't know how to use this in mongoose?
_id field is auto generated by Mongoose and gets attached to the Model, and at the time of saving/inserting the document into MongoDB, MongoDB will use that unique _id field which was generated by Mongoose.
Mongoose plugin that auto-increments any ID field on your schema every time a document is saved. This is the module used by mongoose-simpledb to increment Number IDs. You are perfectly able to use this module by itself if you would like.
Populate with mongoose Population is the process of automatically replacing the specified paths in the document with document(s) from other collection(s).
Following the MongoDB tutorial, Create an Auto-Incrementing Sequence Field, you need to first create a separate counters
collection to track the last number sequence used. The _id
field contains the sequence name i.e. the userID
field in the user collection and the seq
field contains the last value of the sequence.
To start with, insert into the counters collection the initial value for the userID
:
db.counter.insert(
{
"_id": "userID",
"seq": 0
}
)
Having populated the counters collection, generate its schema in Mongoose:
var counterSchema = mongoose.Schema({
"_id": { "type": String, "required": true },
"seq": { "type": Number, "default": 0 }
});
var counter = mongoose.model('counter', counterSchema);
Then redefine your user schema so that when you save a user model it first calls the counter model's findByIdAndUpdate()
method to atomically increment the seq value and return this new value which can then be used as the next userID
value:
var userSchema = mongoose.Schema({
"userID": { "type": String, "required": true },
"firstname": { "type": String },
"lastname": { "type": String },
// other properties ...
}, { "collection": "user" }
);
userSchema.pre("save", function (next) {
var doc = this;
counter.findByIdAndUpdate(
{ "_id": "userID" },
{ "$inc": { "seq": 1 } }
, function(error, counter) {
if(error) return next(error);
doc.userID = counter.seq.toString();
next();
});
});
If you want an autoincrement prop based on the length of your collection, you can do something like this:
UserSchema.pre("save", function (next) {
if (this.isNew) {
this.constructor.find({}).then((users) => {
this.autoIncrementProp = users.length + 1;
next();
});
}
});
isNew is a reserved Schema name (Boolean flag specifying if the document is new.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With