mongoosecollectionsdevise

这可能会从这里下降,但林困难包缠我的头这个问题。

在我的Mongoose Schema中,我有一个组列表。 这些小组将会是人,他们将会有一个_id,或者他们没有_id,他们会被他们的电子邮件列出。

对我来说,使用groups非常有意义的,这样我就可以循环访问列表,而不必硬编码列表名称,原因是我想让用户添加他们自己的组。 我不了解的是…

1)我怎样才能让用户添加到这个集合,我可以做groups.push(groupName) ? 不会把mongoose踢回去吗? 另外它是如何知道我想要在其他领域收集ID,名称,电子邮件。

2)我需要显示组的名称,然后分别显示组中的个人。 我不认为我可以用这种方式显示钥匙。 那么我怎样才能显示组的名称。

我认为我做这个根本错误,但我不能找出正确的方法

  const mongoose = require("mongoose"), bcrypt = require('bcrypt-nodejs'); const passportLocalMongoose = require("passport-local-mongoose"); mongoose.createConnection("mongodb://localhost/familySite"); const userSchema = new mongoose.Schema({ username: {type:String, required: true, unique: true}, email: {type: String, required: false, unique: false}, password: { type: String, required: true}, resetPasswordToken: String, resetPasswordExpires: Date, profile: {picture: String, nickname: String}, permission:{app: Number}, //This will tell the app what level, 0 for Admin, 1 for User name: { first:String, middle:String, last:String, other: { startDate: Date, endDate: Date, first: String, middle: String, last: String } }, lastName: String, address: String, city: String, state: String, zipCode: Number, phone: Number, birthday: Date, birthplace: String, userCover: String, categories: Array, defaultPermission: Number, events: [{ type: mongoose.Schema.Types.ObjectId, ref: "Event" }], moments: [{ type: mongoose.Schema.Types.ObjectId, ref: "Moments" }], instants: [{ type: mongoose.Schema.Types.ObjectId, ref: "Instant" }], groups: [{family:{ type: mongoose.Schema.Types.ObjectId, ref: "User"}, groupName: String, name: String, email: String }, {friends:{ type: mongoose.Schema.Types.ObjectId, ref: "User"}, name: String, email: String, groupName: String, }, {colleagues:{ type: mongoose.Schema.Types.ObjectId, ref: "User", name: String, email: String, groupName: String, } }], likedMoments: [{ //This is for what this person likes, so they can refer later type: mongoose.Schema.Types.ObjectId, ref: "Moments" }], likedEvents: [{ //This is for what this person likes, so they can refer later type: mongoose.Schema.Types.ObjectId, ref: "Event" }], favoriteEvents: [{ //This is for personal events that are favorited type: mongoose.Schema.Types.ObjectId, ref: "Event" }], favoriteMoments: [{ //This is for personal moments that are favorited type: mongoose.Schema.Types.ObjectId, ref: "Moments" }] }) // I THINK I NEED TO MAKE THIS A METHOD SO I CAN GET IT ALL INTO APP.JS // This uses bcrypt to hash passwords userSchema.pre('save', function (next){ var user = this; var SALT_FACTOR = 5; if(!user.isModified('password')) return next(); bcrypt.genSalt(SALT_FACTOR, function (err, salt) { if (err) return next(err); bcrypt.hash(user.password, salt,null, function (err, hash){ if(err) return next(err); user.password - hash; next(); }); }); }); userSchema.methods.comparePassword = function(candidatePassword, cb) { bcrypt.compare(candidatePassword, this.password, function(err, isMatch) { if (err) return cb(err); cb(null, isMatch); }); }; userSchema.plugin(passportLocalMongoose); module.exports = mongoose.model("User", userSchema)