Tag: 模式

在mongoose进行预更新时,user.isModified不是函数

我正在尝试哈希密码在我正在build设的应用程序,他们哈希很好,当我创build一个用户通过调用这个函数(coffeesctipt): UserSchema.pre 'save', (next) -> user = this hashPass(user, next) hashPass = (user, next) -> # only hash the password if it has been modified (or is new) if !user.isModified('password') return next() # generate a salt bcrypt.genSalt SALT_WORK_FACTOR, (err, salt) -> if err return next(err) # hash the password using our new salt bcrypt.hash user.password, […]

从两个http req获取数据并合并它们

我有两个模式在mongoose一个医生信息和两个排名。 我的医生架构: var doctorsSchema = new schema({ Entity: {type:String}, name: {type:String}, Expertise : {type:String}, HMO: {type:String}, Address: {type:String}, reception_hours: [], lat: {type:Number}, lng: {type:Number}, Ranking : {type:Number} },{collection: 'doctorsLocation'}); 我的排名模式是: var calSchema = new schema({ Entity: {type:String}, name: {type:String}, Expertise : {type:String}, Address: {type:String}, Attention : {type:Number}, Professional: {type:Number}, Availability: {type:Number}, Atmosphere: {type:Number}, Recommendation: {type:Number} […]

如何validationMongoose模式中的对象键和值?

在我的Mongoose模式,我试图模拟字典offersInCategory看起来像这样: offersInCategory = { "Electronics": 2, "Furniture": 5 }; Mongoose不支持字典,所以我不得不在数组中使用对象字面值,如下所示: offersInCategory: [{ category: { type: String, enum: ['Furniture', 'Household', 'Electronicts', 'Other'] }, val: { type: Number, min: 0 } }] 我用这种方法的问题是感觉不直观。 此外,它并不妨碍我的模型为同一类别创build多个条目,如下所示: offersInCategory = [ { category: "Furniture", val: 2 }, { category: "Furniture", val: 0} ] 理想情况下,我会把我的offersInCategory属性的结构像这样: offersInCategory : { "Furniture" : 0, "Electronics" […]

用mongoose更新mongodb中的所有数据

我在我的mongo数据库中有以下数据。 { "FirstName": "Sunil", "LastName": "Jamkatel" } 我想将全名添加到数据库中的所有文档,以便从文档中获取名字和姓氏,并将其添加为: { "FirstName": "Sunil", "LastName": "Jamkatel", "FullName": "Sunil Jamkatel" } 请让我知道如何使用mongoosejs来做到这一点。 谢谢

mongoose – blogPost不是构造函数错误(模式/模型错误)

我是新来的mongoose和mongodb,我试图连接到mlab并插入一些数据,但我不断收到以下代码的错误 var newPost = new blogPost({title:'First Post', post:'this is my first post'}); ^ TypeError: blogPost is not a constructor at Object.<anonymous> (C:\Users\ipingou\website\hwnay\index2.js:39:15) at Module._compile (module.js:570:32) at Object.Module._extensions..js (module.js:579:10) at Module.load (module.js:487:32) at tryModuleLoad (module.js:446:12) at Function.Module._load (module.js:438:3) at Module.runMain (module.js:604:10) at run (bootstrap_node.js:394:7) at startup (bootstrap_node.js:149:9) at bootstrap_node.js:509:3 这是我的代码: const mongoose = require('mongoose'); var blogPost […]

MongoDB + Node.js:如何从一个外部文件使用架构为另一个架构?

我有一个类(或模型),需要使用另一个类作为其属性的一部分,如下所示。 **两个文件的标题** var mongoose = require('mongoose'), Schema = mongoose.Schema; item.js module.exports = function() { var ItemSchema = new Schema({ name: String, cost: Number }); mongoose.model('Item', ItemSchema); } receipt.js ItemModel = require('./item.js'); var Item = mongoose.model('Item'); module.exports = function() { var LineItemSchema = new Schema({ item: Item, amount: Number }); var LineItem = mongoose.model('LineItem', LineItemSchema); var […]

模式的自引用不起作用

以下是我的模式 – var mongoose = require('mongoose'); // Create Schema var userSchema = new mongoose.Schema({ name: String, email: String, created_by: { type: Schema.Types.ObjectId, ref: 'User'}, created_date: { type: Date, default: Date.now } }); // compile Schema into a model var User = mongoose.model('User', userSchema); // Make this instance available when require()d module.exports = User; 以下是我得到的错误 – […]

Mongoose Schema中分组数据的正确结构

我创build了一个Mongoose Schema,如下所示: var UserSchema = new Schema({ user: [{ name: { type: String, required: true }, email: { type: String, required: true, index: { unique: true } }, password: { type: String, required: true }, mobile: Number }], account: [{ locked: { type: Boolean, default: false }, accountType: { type: String, default: "guest" }, failedLogins: […]

Schema中的recursion元素:Mongoosebuild模

任何想法在Mongoose模式中build模树文件? var TreeSchema = new Schema({ "Non-leafNode" : { 'children': [ { type: "NodeElement"}], 'title': String}, "NodeElement: { 'elem':{ type: "LeafNode"}, 'elem2':{ type: "Non-leafNode" }}, // one of them is required. not both. "LeafNode" : { title : String} }); 怎么能模型呢? 整棵树是一个文件(最好)

如何设置Mongoose模式在MongoDB中存储对象数组?

我目前有一个array ,其中包含嵌套在其中的多个objects 。 这是格式… [ { id: 1, title: 'Squats', video: 'https://www.youtube.com/watch?v=aPYCiuiB4PA' }, { id: 2, title: 'Push-Ups',video: 'https://www.youtube.com/watch?v=aPYCiuiB4PA' }] 我试图.save()它在我的Schedule模型,看起来像这样… var ScheduleSchema = new mongoose.Schema({ schedule: [Object] }) 当我在服务器端的Schedule控制器中运行下面的代码时, .save()函数给了我一个“成功”的消息(我给它编程),但是当我看着MongoDB数据库时, schedule数组内没有任何内容。 这是保存的信息在数据库中的样子…. { "_id" : ObjectId("56c28a0d4c92bec03408c077"), "schedule" : [ ], "__v" : 0 } 我有一种感觉,我的模型是错误的。 模型模式我试过… 1) schedule: [] 2) schedule: [Schema.Types.Mixed] 以防万一这是有帮助的,这里是我的.save()函数,当我试图从计划控制器保存所述数据… var […]