Tag: mongoose schema

Mongoose每次使用预保存钩子保存时都会更改密码

我正在使用bcrypt的预保存钩子来encryption系统上的密码。 它创build或更改密码时正常工作。 问题是,似乎每次更改并保存不同的字段(例如电子邮件)时都会重新encryption密码。 可能更容易用代码解释。 这里是模型: const UserSchema = new Schema({ email: { type: String, required: true, lowercase: true, unique: true, trim: true }, password: { type: String, required: true } }) 和钩子: UserSchema.pre('save', function(next){ const user = this; console.log(user); bcrypt.genSalt(10, function(err, salt){ if (err){ return next(err) } bcrypt.hash(user.password, salt, null, function(err, hash){ if(err){return next(err)} user.password […]

如何更新集合/ MongoDb / Nodejs中所有数据的参数

我需要为特定集合中的所有数据中的特定参数设置特定的值。 例如; 我有一个数据库,我的模式具有年龄和名称参数,我想更新每个人的年龄为5.我已经搜查的问题,但我找不到任何类似的情况。 我怎么能做到这一点,或者我可以用mongoose或mongoclient或除了mongoshell之外呢? 我使用nodeJSmongoose和expression。 这是我的代码(当然,它不工作:) 'exports.changeAll = function (req, res) { var reqparameter=req.body.parameter; var database=req.params.database; var collection=req.params.collection; mongoose.Promise = global.Promise; mongoose.connect('mongodb://127.0.0.1::27017/' + project, { useMongoClient: true, }) .then(() => { var Project = mongoose.model(collection, ProjectSchema, collection); Project.update({},{$set:{parameter:reqparameter},function(err,done){ if (err) res.send(err); else res.send('done' +reqparameter); } }); }); } // reqparameter =请求参数值// 我也试过mongoclient:(它不工作,错误是关于db.update不是一个函数) MongoClient.connect(url, function (err, […]

如何在sql数据库中显示mongodb模式之间的关系?

我有两个模型,一个是“职位”,另一个是“评论”。 我想展示这两个模型之间的关系,这是一个单一的职位可能有意见,但我卡在这里。 我知道如何在sql数据库中显示这种关系。 目前我正在使用mongoose模式和节点js。

Mongoose Model.find()参数的问题

我正在研究一个节点应用程序(与IOS前端),并偶然发现这个问题。 我和mongoose一起使用了mongodb。 我有这个路由,/得到接收正确的用户ID,并试图find所有'Voots'具有相同的用户ID。 这是'Voot'的样子: { "_id": "59db9fa2659bb30004899f05", "title": "Pizza!", "body": "hoppakeeee", "user": { "__v": 0, "password": "$2a$10$Rwb5n7QoKaFaAOW37V0aWeEeYgfn6Uql474ynUXb83lHi7H2CuB1u", "email": "noelle.schrikker@planet.nl", "name": "Noelle Schrikker", "_id": "59db9ecf659bb30004899f04" }, "__v": 0, "downVotes": [], "upVotes": [] }, 正如你所看到的,它有一个名为user的属性,它是一个包含名字,电子邮件,密码和_id的用户对象。 我按照我的要求这样做: // Send all voots from the specific user Voot.find({"user._id": userId}, function(err, voots) { if (err) { console.log(err); res.status(400).send(err) res.end() } if […]

mongoose,从数组中删除一个对象,并更新其他对象

我是一个Web开发新手,我正在尝试构build一个简单的可编辑图像库。 我有一个Collections模式和一个绘画模式: var collectionSchema = new mongoose.Schema({ name: String, paintings: [ { type: Schema.Types.ObjectId, ref: "Painting" } ] }) && var paintingSchema = new mongoose.Schema({ img: String, description: String, index: Number, parent: String }); 我正在试图添加一个销毁路线,将从一个集合中删除一个绘画。 我到目前为止: app.delete("paintings/:type/:index", function(req,res){ var type = req.params.type; var index = req.params.index; Painting.findOneAndRemove({parent:type,index:index},function(err,removedPainting){ if(err){ console.log(err); } else{ res.redirect("/paintings/"+type); } }); }); […]

有条件跳过mongoose钩function

我有一个预保存挂钩来encryptionUser架构的password字段,如: var schema = new mongoose.Schema({ username: 'string', password: 'string' }); schema.pre('save', encrptPasswordHook); schema.pre('update', encrptPasswordHook); schema.pre('findOneAndUpdate', encrptPasswordHook); … 通过这种方式,每次User创build或更新时,我都在我的数据库中encryption了密码string。 现在我有一个旧的User数据与encryption密码的JSON文件。 我想使用这个User模型将JSON文件导入到我的数据库中。 如何避免预存钩再次encryption密码?

mongoose“架构方法”callback不起作用

我是新来的mongoose和node.js。 我尝试按照这个教程: https : //scotch.io/tutorials/using-mongoosejs-in-node-js-and-mongodb-applications#sample-model-for-users 在我的入口点index.js中,我试图调用“chenya.saltHashPassword(function(err,passwordHash)”,它实际上是在user.js中调用的,因为user.js可以打印出三条日志消息,但是没有日志这个方法的消息在index.js中调用,而save方法可以打印出表示成功保存的日志消息: //Lets load the mongoose module in our program var mongoose = require('mongoose'); //Lets connect to our database using the DB server URL. mongoose.connect('mongodb://localhost:27017/server_naturis'); // if our user.js file is at app/models/user.js var User = require('./user'); // create a new user called Chenya var chenya = new User({ userName: 'Chenya', […]

如何访问另一个模型mongoose数据库中的一个模型模式

我有两个模式,一个是user.js文件中的用户模式,另一个是product.js文件中的产品模式 user.js文件中的我的用户架构如下: var userSchema = new Schema({ firstname: { type: String, required: true }, lastname: { type: String, required: true }, password: { type: String, required: true }, mobileno: { type: Number, unique: true, required: true }, facebookid: { type: String }, userimage: { type: String } }); 并且我正在使用mongoose-auto-increment模块自动生成_id来自动增加用户集合中的userId。 在product.js文件中我的产品架构如下: var productSchema = new Schema({ […]

mongoose找不到任何结果

我在Mongo和一个相应的mongoose模型简单的收集。 这个集合总是只包含一个文档。 当我在mongoshell中运行一个查询时,它给了我的结果,但是当我试图find一个使用mongoose它根本没有返回任何结果。 有人能帮我弄清楚什么是错的。 以下是我的代码。 模型: const mongoose = require('mongoose'); const schema = new mongoose.Schema({ lastResetMonth: { type: Number }, lastResetWeek: { type: Number }, currentFisYear:{ type: Number } }); module.exports = mongoose.model('ReserveResetTrack', schema, 'reserveResetTrack'); const ReserveResetTrack = require('../models/ReserveResetTrack'); ReserveResetTrack.findOne({}) .then(trackData => { return { lastFisMonth: trackData.lastMonth, lastFisWeek: trackData.lastWeek } }); 上面的代码总是只返回一个承诺。 这是我collections的唯一文件,这将是唯一的永远 { "_id" […]

获取(关联)Mongoose模型的数组

你可以像这样检索一个Mongoose模型: 让用户= mongoose.model('用户'); 我正在寻找获得这些模型的关联数组。 有没有一些聪明的方法来获取使用对象解构的模型列表? 就像是: const {User, Employees, Managers} = mongoose.model('x'); 我目前的解决scheme是这样做的: /project /models index.js 其中index.js看起来像: module.exports = { User: require('./user'), Employee: require('./employee'), Manager: require('./manager'), }; user.js,employee.js和manager.js文件看起来像这样: let mongoose = require('mongoose'); let Schema = mongoose.Schema; let userSchema = new Schema({…}); module.exports = mongoose.model('User', userSchema, 'users'); 然后我可以这样做: const {User, Employees, Managers} = require('./models'); 但是我正在寻找一个更好的解决scheme,如果可能的话,不需要手动工作。