Tag: mongoose populate

在用户注册中已经添加了其他字段的架构中,使用mongoose在MongoDB中插入数组

我正在创build在线课程的申请。 我为用户注册创build了一个模式。 在架构中,我也想添加一个用户在注册的课程的名称。 课程名称是一个数组。 用户注册成功。 之后,我创build了/仪表板的路线,用户发送POST请求添加课程名称。 该课程应该添加到注册课程的相同注册模式字段中。 但是,当我保存一个注册模式的新对象时,它会创build一个新的文档字段courseEnrolled字段。 我希望这个POST请求值被添加到用户的文档字段当然是以数组forms进行注册。 这是我如何定义我的注册模式。 它的名字在account.js var mongoose = require('mongoose'); var Schema = mongoose.Schema; var passportLocalMongoose = require('passport-local-mongoose'); var courseSchema = new Schema({ courseName : String }); var Account = new Schema({ username: { type: String, unique: true }, password: String, email: String, firstName: String, lastName: String, courseEnrolled: [{courseName : […]

mongoose关系devise

我最近开始在一个Node.js应用程序中使用Mongoose和Express.js,我有一个正确的方式来devise我的模式的问题。 我有几个有一些关系的模式,即位置模式有一个对象数组(它不是在这个上下文中的JS对象),而对象模式有它的位置属性。 我已经知道Mongoose中的关系是通过使用人口来解决的,但是当我实现这个方法的时候,我注意到我必须input很多重复的代码,也就是每当我想创build一个新的对象的时候,我也必须更新Location的数组对象,然后将该位置分配给该对象的属性。 在一个单独的查询中手动组装所有具有与我想从数据库中获取的位置相同的locationId属性的对象是不是更加简单? 我也考虑过将对象存储在位置文档的数组中(作为子文档),但是我决定要能够独立于位置(不查询位置)使用对象(创build,移除,更新),所以这种方法我猜不符合我的需求。 但是,在我的情况下,群体也有其缺点,所以我想这是最好的方式去手动收集由该位置的ID单独查询中的特定位置的对象。 我想听到这个技术的一些专业或高级用户对deviseMo​​ngoose模式的意见,以便我和其他人不会在后来保持和扩展我们的应用程序时遇到麻烦。 以下是我目前的模式: var locationSchema = new mongoose.Schema({ title: String, objects: [{ type: String, ref: 'object' }] }); var objectSchema = new mongoose.Schema({ title: String, location: { type: String, ref: 'location' } });

Mongoose:覆盖更新的数组属性

如何覆盖基于给定数组作为源文档的数组属性? 架构: var postSchema = new mongoose.Schema({ title: { type: String, required: true, index: { unique: true } }, content: { type: String }, tags: [{ type: mongoose.Schema.Types.ObjectId, ref: 'Tag' }] }); 我现在有一个标签对象的ID数组,我想覆盖标签属性。 我现在遇到的问题是,它添加了新的标签,但它不会删除不在源数组中的标签。 我正在使用findOneAndUpdate执行更新,如下所示: // Pseudo code example Post.findOneAndUpdate({ _id: id }, { tags: ["id1…", "id2…"], {}, cb);

mongoose查询需要填充的数据

我有一个很简单的Mongoose模型: aclSchema = mongoose.Schema({ _id: String, public_read: Boolean, public_write: Boolean, }); aclModel = mongoose.model('acl', aclSchema); 另一个引用它的模型: thingSchema = mongoose.Schema({ _id: String, _acl: { type: String, ref: 'acl' } }); thingModel = mongoose.model('thing', thingSchema); 我需要能够find文件(thingModel)_acl.public_read是真实的。 我遇到的问题是,因为thing._acl是ref,所以直到查询完成后才填充它。 例: thingModel.find({"_acl.public_read":true}).populate('_acl').exec(callback); 这不会返回任何结果,因为,我猜,_acl是一个ref,它不会填充,直到查找返回文档之后。 只是一个侧面说明,模式比这更复杂,其他的参考文件可能是循环的。 为了简单起见,我没有把它们包括在内,但是基本的想法就在那里。 如果这真的很简单,我会使用子文件,它会按预期工作。 有没有更好的方法来做到这一点,所以我得到预期的文件?

在添加之前填充: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创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: […]

从两个集合中填充

我为2个不同的集合定义了2个Schema,我需要将其中的一个填充到另一个: stationmodel.js var stationSchema = new Schema({ StationName: 'string', _id: 'number', Tripcount: [{ type: Schema.Types.ObjectId, ref: 'Tripcount'}] }, {collection: 'stations'} ); module.exports = mongoose.model('Station', stationSchema); tripmodel.js var tripSchema = new Schema({ _id: { type: Number, ref: 'Station'}, Tripcount: 'number' }, {collection: 'trips'} ); module.exports = mongoose.model('Tripcount', tripSchema); 根据mongoose填充文件 ,这是要走的路。 当我使用邮递员获取电台时,我遇到“Tripcount”仍然是[]的问题。 我的“电台”collections的数据库结构: { "_id": 1, "StationName": […]

Mongoosejs虚拟填充

我的项目中有一个圈子模型: var circleSchema = new Schema({ //circleId: {type: String, unique: true, required: true}, patientID: {type: Schema.Types.ObjectId, ref: "patient"}, circleName: String, caregivers: [{type: Schema.Types.ObjectId}], accessLevel: Schema.Types.Mixed }); circleSchema.virtual('caregiver_details',{ ref: 'caregiver', localField: 'caregivers', foreignField: 'userId' }); 照顾者架构: var cargiverSchema = new Schema({ userId: {type: Schema.ObjectId, unique: true}, //objectId of user document detailId: {type: Schema.ObjectId, ref: "contactDetails"}, facialId: […]

满足条件时才填充

我有一个mongodb数据库,并且使用了与nodejs一起的mongoose。 只有当我有字段“temtussvinculado = true”时,我需要从填充“tabela_tuss”的下一个查询返回数据。 这是我在做什么: ConvProced.find({'convenioId':new ObjectId(req.params.id)}) .populate('convenioId') .populate({ path:'procedId', populate:{ path:'tabela_tuss', match: { 'procedId.temtussvinculado': true} } }) .exec( (err,data) => { callback(err,data,res) }) 我的问题是,我与“程序.temtussvinculado:真”的匹配没有效果,“tabela_tuss”从来没有填充。 我究竟做错了什么? 这是我的模式: //// var conveniosSchema = new mongoose.Schema({ nome: {type: String, unique:true}, ativo: {type: Boolean} }); module.exports = mongoose.model('Convenio', conveniosSchema,'convenios' ); //// //// const agProcedimentosSchema = new mongoose.Schema({ ativo:{type:Boolean}, temtussvinculado:{type:Boolean}, […]

Mongoose模型在种群后得到未定义的属性

我遇到了一个基本要求的问题。 所有我提取的mongoose模型的属性在exec()callback中都是未定义的。 这是我的模式: userSchema: new Schema({ email: { type: String, limit: 50, index: true }, password: String, birthdate: { type: Date }, active: { type: Boolean, default: true }, friends: [{ _friend: { type: Schema.ObjectId, ref: 'User' }, addedDate: { type: Date, default: Date.now } }], registrationDate: { type: Date, default: Date.now } }) 你可能已经注意到我的“friends”属性是一个引用另一个模式的对象数组。 […]