Tag: mongoose schema

Mongoose自引用模式不为所有子文档创buildObjectId

我有一个在mongoose中有一个自引用字段的Schema,像这样: var mongoose = require('mongoose'); var CollectPointSchema = new mongoose.Schema({ name: {type: String}, collectPoints: [ this ] }); 当插入一个CollectPoint对象时: { "name": "Level 1" } 没关系,结果如预期: { "_id": "58b36c83b7134680367b2547", "name": "Level 1", "collectPoints": [] } 但是当我插入自引用的子文档时, { "name": "Level 1", "collectPoints": [{ "name": "Level 1.1" }] } 它给了我这个: { "_id": "58b36c83b7134680367b2547", "name": "Level 1", "collectPoints": [{ […]

在添加之前填充:RangeError:超出最大调用堆栈大小

我想要实现一个在线编辑器,比如plunker。 我已经定义了以下数据结构:一个post (即一个项目)包含一个folders列表(这意味着不同版本的post),一个folder只是一个files列表。 var PostSchema = new mongoose.Schema({ folders: [{ type: mongoose.Schema.Types.ObjectId, ref: 'Folder' }] … }); var FolderSchema = new mongoose.Schema({ files: [{ name: String, body: String, editorOption: Object }], post: { type: mongoose.Schema.Types.ObjectId, ref: 'Post' } }) 我想使用下面的请求来添加一个文件夹/版本(通过编辑器上的savebutton)。 需要注意的是,由于一个post可以同时在不同的地方打开,所以在添加一个版本之前 ,我们必须从数据库中取出post ,然后把最新的post返回给控制器。 o.addFolder = function (id, folder) { return $http.post('/posts/' + id + '/editor/folders', […]

mongoose设置默认数组大小和值

我试图在我的mongoose模式中设置一个默认的数组大小和值,但是返回总是以[]出现,除非文档实际上有数据。 "transform": { type: [ Number ], default: [0, 0, 0] } 返回值是: "transform":[] 如何configuration我的模式,以便它返回: "transform":[0,0,0] 答 : required: true设置required: true将数据添加到创build的任何新文档。 旧文件不会被更新,但是。 "transform": { type: [ Number ], default: [0, 0, 0], required: true } 更新为了快速解决我的数据问题,我在Mongo上运行了一个更新,使用以下行升级所有现有的文档。 db.getCollection('objects').updateMany( { "properties.transform": []}, { $set: { "properties.transform" : [0,0,0] }} ); 注意:我的变换字段是属性的子元素,因此"properties.transform"

mongoose创build与推多个参考

所以有一个与我的数据库的嵌套结构这里是我的三个模式:用户架构: var UserSchema = new mongoose.Schema({ email: { type: String, lowercase: true, unique: true, required: true }, password: { type: String, required: true }, role: { type: String, enum: ['trainer', 'client', 'admin'], default: 'client' }, trainer: { type: mongoose.Schema.Types.ObjectId, ref: 'User' }, programs: [ { type: mongoose.Schema.Types.ObjectId, ref: 'Program' } ], diet: [ { calories: […]

通过没有ObjectId的mongoose更新条目

我有一个Mongoose模式定义为 var mongoose = require('mongoose'); var Schema = mongoose.Schema; var userSchema = new Schema({ user_id: String, event_organizer: [String], }); module.exports = mongoose.model('User',userSchema); 现在,我有一个函数,我希望将这个用户的ID添加到一个事件。 当然,这个事件已经在DB中出现了。 function addUserToEvent(user_id, event_id) { } 如何添加一个event_id到架构中定义的用户的event_organizer数组? 数组可能已经被填充,我需要附加这个id,而不是重置它。

Mongoose / Node.JS中的本地化值不会更新

这是我所看到的一个奇怪的问题。 我有一个使用mongoose-i18n-localize的mongoose模式: var mongoose = require('mongoose'); var mongooseI18n = require('mongoose-i18n-localize'); var Schema = mongoose.Schema; var TestSchema = new Schema({ id: String, active: {type: Boolean, default: false}, name: {type: String, required: true, i18n: true} }); TestSchema.plugin(mongooseI18n, { locales: ['en_US'] }); module.exports = mongoose.model('Test', TestSchema); 在我的电话中,我有这样的: app.put('/test/:id', function(req, res) { var query = {'id': req.params.id); var test […]

更新mongoose模式后,新input的字段不会插入已创build的集合中

我已经创build了一个MongoDB集合,使用快速web服务中的mongoose模式,几天后,我发现在mongoose表中需要一个新的字段,所以在mongoose模式中添加了这个字段,并尝试插入一个新添加字段的值,它不会被插入到新添加的文档中。 所以我做了一个故障排除过程,我完全删除了我现有的集合,然后将我的新文档插入集合,然后我发现它开始正常工作。 我新插入的字段被插入到集合中。 那么有没有其他的方式来在MongoDB中使用mongoose添加一个新的字段而不删除整个集合。 我现有的用户集合模式: var mongoose = require('mongoose'); var Schema = mongoose.Schema; var userSchema = new Schema({ username: { type: String, required: true, unique: true }, password: { type: String, required: true }, }); var User = mongoose.model('User', userSchema); module.exports = User; 当我插入一个新的文件,用户名和密码在上面的集合它工作正常。 添加新字段(名称)后,我的新用户集合模式: var mongoose = require('mongoose'); var Schema = mongoose.Schema; var userSchema […]

Mongodb模式的不同types的post/文章

我正在开发一个Mongodb,Mongoose和Expressjs应用程序,其中有不同types的文章或post。 例如,有一个只包含URL或地址的URL发布,其中没有文本。 有普通的post,地图post,图片post和一堆其他types。 现在我想在我的应用程序中定义Post的模式。 我有点困惑,如果我应该为每个定义不同的模式,因为他们应该有不同的属性和属性时,他们共享一些属性,或把Post模式中的所有可能的属性,并使用type的名称属性指定共享的post的types。 做这种事情的最佳做法是什么?

基于属性的dynamicMongoose模式

我有以下用户架构(其中不同用户types的所有不同属性被合并): var UserSchema = new mongoose.Schema({ status: String, firstName: String, lastName: String, address: Object, email: {type: String, lowercase: true, unique: true, required: [true, "can't be blank"], match: [/\S+@\S+\.\S+/, 'is invalid'], index: true}, organization: [{ type: mongoose.Schema.Types.ObjectId, ref: 'Organization' }], phone: {type: Number, unique: true, required: true, required: [true, "can't be blank"]}, role: String, hash: String, […]

有关Mongoose Schemadevise的问题

对于NoSQL世界,我是全新的,并且很难围绕它。 本周我正在用Node.js学习MongoDB(Mongoose),这是我目前的模式: var eventDataSchema = new Schema({ _id : Number, notes : {type: String, required: true}, start_date : {type: Date, required: true}, end_date : {type: Date, required: true}, }, { id : false, collection : 'event-data' }); eventDataSchema.plugin(AutoIncrement); var EventData = mongoose.model('EventData', eventDataSchema); 现在,这是工作,我想添加一个用户和密码,并有权访问EventData个人访问。 另外…后来,如果我想只发送一个JSON的eventData,而不是用户到我的JavaScript,我该怎么做? 我目前正在以这种格式将我的eventData发送给我的js: router.get('/data', function(req, res){ EventData.find({}, function(err, data){ if (err) { […]