Tag: mongoose populate

Mongoose.js有条件的填充

我正在处理一些旧数据,其中一些模式具有“混合”types。 基本上有时候一个值会被引用的ObjectID,但有时候会是一些文本(超级糟糕的devise)。 由于非ObjectID出现的次数,我无法正确填充此数据。 所以,对于我的实际问题:是否有可能创build一个有条件的集合(在一个集合上) 我需要能够告诉填充跳过这些other值。

填充多个级别不起作用

First = new mongoose.Schema({ name: String, second: {type: Schema.Types.ObjectId, ref: 'Second'}, }); Second = new mongoose.Schema({ name: String, third: {type: Schema.Types.ObjectId, ref: 'Third'}, }); Third = new mongoose.Schema({ name: String }); First.find({}).populate({ path: 'Second', populate: { path: 'Third'} }).exec(function(err, result) { console.log(result) }) 首先填充是好的,但第三总是null 。 意思是我有这样的一些东西: { name: 1, second: { name: 2, third: null […]

Mongoose填充子文档数组

道歉,如果这已经被问及,我的search没有出现相同的情况。 我有两个模式,如下所示: var experimentSchema = new mongoose.Schema({ name : 'string' elements : [{ type : mongoose.Schema.ObjectId, ref: 'Element' }], resources : [{ type : mongoose.Schema.ObjectId, ref : 'Resource' }], }) var elementSchema = new mongoose.Schema({ name : 'string', component : { type : mongoose.Schema.ObjectId, ref : 'Component' } }) 我想执行一个深入的人口,所以当我要求一个实验时,我得到一个elements和resources数组的对象,每个元素的字段component也被填充。 我已经尝试了几件事情: Experiment.findOne(query).populate(['resources','elements','elements.component']).exec(…) 没有成功。 任何人都可以提供这种types的操作正确的语法? 提前致谢!

Mongoose:schema._getVirtual不是一个函数

我突然遇到麻烦(在我更新Mongoose包版本之前工作正常)。 目前使用Mongoose 4.7.6。 var userSchema = require('schemas/user'), User = db.model('User', userSchema); // db is already known User .findById(user._id) // user is already known due to auth .populate('group currentPlayer') .exec(function (findErr, userPlayer) { … }); 如果我的用户架构是必要的,我会发布,但目前还没有由于其长度(虚拟,方法,静态)。 错误: /app/node_modules/mongoose/lib/model.js:2986 var virtual = modelForCurrentDoc.schema._getVirtual(options.path); ^ TypeError: modelForCurrentDoc.schema._getVirtual is not a function at getModelsMapForPopulate (/app/node_modules/mongoose/lib/model.js:2986:49) at populate (/app/node_modules/mongoose/lib/model.js:2660:15) at […]

如何在asynchronous调用期间填充数组并将其发送到响应对象中

这是我试图做的一个例子,检索mongo数据库中的所有post,为每个post填充作者,然后使用作者对象从Cloudinary检索configuration文件图片。 什么是正确的方式去做这个? 我已经尝试了许多方法来填充数组,并在响应中发送这些数据,但是由于asynchronous调用,它们在发送数据之前从不运行。 router.get('/posts',auth, function(req, res, next) { //var id = req.payload._id; Post.find(function(err, posts){ if(err){ return next(err); } posts.forEach(function(post){ post.populate('author',function(err,post){ post.image = cloudinary.image("v"+post.author.avatarVersion+"/profile/"+post.author._id,{ width:100, height:100,crop:'thumb',gravity:'face',radius:'max' }) //here the post object is updated console.log(post) }) }) //res.json(some posts array); }); }); 感谢Dan Moldovan的解决scheme! router.get('/posts',auth, function(req, res, next) { var id = req.payload._id; Post.find({}).populate('author').exec(function(err,posts){ if(err){ return next(err); […]

mongoose遍布2个数据库

我有一个使用2个数据库的node app 。 其中一个是所有数据的其余部分的Names 。 我有这个连接设置: // DATABASE CONNECTION var APP_DB_URI = config.APP_DB; // mongodb://localhost:27017/app_db var app_DB = mongoose.createConnection(APP_DB_URI); var name_DB = app_DB.useDb(config.NAME_DB); // 'name_db' 这个连接工作正常。 将数据保存到app_db和names_db完美工作也没有问题。 但问题是这样的:当我试图查询说, Account模型,然后填充引用的Name模型,填充过程返回null 。 帐户架构: var mongoose = require('mongoose'); var Schema = mongoose.Schema; var field = { email: {type: String, unique: true}, password: {type: String, select: false}, name: {type: […]

mongoose'颠倒'的人口,即基于子模式中定义的引用填充父对象

让我们借由修改如下的吓人的 例子来举例 : 项目组架构: var ProjectGroupSchema = new Schema({ projectGroupId : String, title : String }); 项目模式: var ProjectSchema = new Schema({ title : {type : String, default : '', required : true}, group : {type: String, ref: 'ProjectGroup' }, subscribers : [{type: String, ref: 'User' }] }); 用户架构: var UserSchema = new Schema({ userId […]

mongoose如何从人口中排除_id

我正在填充angular色文档中的_group return this.find(query, {'_group': 1, 'name':1, 'description': 1} ) .populate('_group', ['name', 'description']) .sort({ createdAt: -1 }) … 我也得到_group的_id {"_id":"5959ef7db9938a0600f05eb2", "_group":{ "_id":"5959ef7db9938a0600f05eae", "name":"GroupA", "description":"Description GroupA" }, "name":"manager", "description":"can R group, can RW user"}, 我怎样才能摆脱_group._id? 我试过了: return this.find(query, {'_group': 1, 'name':1, '_group._id':0, '_id':0} ) 但是会产生一个错误:投影不能混合包含和排除。 和 return this.find(query, {'_group': 1, 'name':1, '_id':0, '_id':0} 只删除role._id 感谢您的反馈

如何使用mongoose填充无限嵌套级别的文档

我正在devise一个pipe理父母和小孩公司组织结构的Web应用程序。 有两种types的公司:1 – 主要公司,2 – 子公司。公司只能属于一家公司,但可以有几个小公司。 我的mongoose模式看起来像这样: var companySchema = new mongoose.Schema({ companyName: { type: String, required: true }, estimatedAnnualEarnings: { type: Number, required: true }, companyChildren: [{type: mongoose.Schema.Types.ObjectId, ref: 'Company'}], companyType: {type: String, enum: ['Main', 'Subsidiary']} }) module.exports = mongoose.model('Company', companySchema); 我把所有的公司都存放在一个集合中,每个公司都有一个引用它的子公司的列表。 然后,我想显示所有公司作为一个树(在客户端)。 我想查询所有主要的公司,填充他们的孩子和孩子,他们的孩子等,无限的嵌套级别。 我怎样才能做到这一点? 或者,也许你知道更好的方法。 另外我需要能够查看,添加,编辑,删除任何公司。 现在我有这个: router.get('/companies', function(req, res) { Company.find({companyType: 'Main'}).populate({path: […]

Mongoose populate()返回空数组

我想通过clientSchema中的属性'token'来查询子文档数组。 但我无法填充子文档数组。 它总是返回空值。 这是我的尝试 var performAuthAsync = promise.promisify(performAuth); var response = {}; performAuthAsync(req).then(function (client) { sendStatus(res, 200, { "success": "true", "value": client }); }).catch(ApiError, function (e) { response.error = "true"; response.message = e.message; if (e.message == "Invalid Authorization" || e.message == "Unauthorized access") { console.log(e.message); sendStatus(res, 401, response, req.query.type); } else { sendStatus(res, 500, response, […]