Hello! There is a mongo collection of the following type

const GroupsShema = new Schema({ name: { type: String, required: [ true, 'Укажите название группы'] }, groupInvite: { type: String, required: [ true, 'Укажите инвайт группы'] }, users: [ { author: { type: String }, role: { type: String }, token: { type: String }, login: { type: String, }, hash: String, salt: String } ] }); GroupsShema.methods.setPassword = function (password) { this.users.salt = crypto .randomBytes(16) .toString('hex'); this.hash = crypto .pbkdf2Sync(password, this.users.salt, 1000, 512, 'sha512') .toString('hex'); }; GroupsShema.methods.validPassword = function (password) { var hash = crypto .pbkdf2Sync(password, this.users.salt, 1000, 512, 'sha512') .toString('hex'); return this.users.hash === hash; }; 

The task is to find a group in which there is a user from users by the invite group (the groupInvite field), and then in this group to find the user by login and token and fill in the remaining fields (that is, update the fields in 1 users document. + Second the problem is that the setPassword and validPassword methods are not working, it’s all darkness, how to describe them, so that they are applied specifically to the users document, and not the groups.

Your ideas?

    1 answer 1

    See this code. Here you can add your own methods of a specific Model and everything will work as you need.

     var mongoose = require('mongoose'); mongoose.connect(_MONGODB_URL_CONNECT_, { server: { reconnectTries: Number.MAX_VALUE, reconnectInterval: 1000 } }); var db = mongoose.connection; var shemeExample = new Schema({ test: { type: String, default: '', }, update_at: { type: Date, default: Date.now }, create_at: { type: Date, default: Date.now // time create tx } }); var modelTest = mongoose.model("exampleCollection", shemeExample); 

    example save

     let rowExample = new modelTest(test:'stringSaveExampleBlaBla'); rowExample.save() .then((res)=>{console.log('success save',res);}) 

    find example find (where)

     modelTest.find({test:'stringSaveExampleBlaBla'}) .then((result_rows)=>{console.log('find res rows',result_rows);}) 

    find example update (where, update, options);

     modelTest.update({test:'stringSaveExampleBlaBla'},{test:'updatetest'},{multi:true,new:true}) .then((res_updated)=>{console.log('updated info',res_updated);})