Tag: mongoose populate

如何删除对象考虑到在Mongoose Node.js中的引用?

这是我的MongoDB架构: var partnerSchema = new mongoose.Schema({ name: String, products: [ { type: mongoose.Schema.Types.ObjectId, ref: 'Product' }] }); var productSchema = new mongoose.Schema({ name: String, campaign: [ { type: mongoose.Schema.Types.ObjectId, ref: 'Campaign' } ] }); var campaignSchema = new mongoose.Schema({ name: String, }); module.exports = { Partner: mongoose.model('Partner', partnerSchema), Product: mongoose.model('Product', productSchema), Campaign: mongoose.model('Campaign', campaignSchema) } […]

为什么我不能访问mongoose模式的方法?

我有一个Nodejs应用程序中的这个Mongoose模式: const mongoose = require('mongoose'), Schema = mongoose.Schema, sodium = require('sodium').api; const UserSchema = new Schema({ username: { type: String, required: true, index: { unique: true } }, salt: { type: String, required: false }, password: { type: String, required: true } }); UserSchema.methods.comparePassword = function(candidatePassword, targetUser) { let saltedCandidate = candidatePassword + targetUser.salt; if […]

Mongoose查询:从Post Schema中填充前两个评论

我有3个集合:用户,发布和评论。 post有多个评论。 我想抓住50个职位,填充作者,填充评论,但我只想要按date(_id)sorting前2个最多投票评论 const PostSchema = new Schema({ author: { type: Schema.Types.ObjectId, ref: 'User' }, content: String, comments: [{ type: Schema.Types.ObjectId, ref: 'Comment' }] }); const Post = mongoose.model('Post', PostSchema); const CommentSchema = new Schema({ author: { type: Schema.Types.ObjectId, ref: 'User' }, content: String, votes: Number }); const Comment = mongoose.model('Comment', CommentSchema); Post .find({}) .limit(50) […]

Mongoose .populate()不填充

我已经尝试了一些不同的东西,我不能让Mongoose将Users信息填充到Items集合中。 文件: users.js var mongoose = require( 'mongoose' ) Schema = mongoose.Schema, ObjectId = Schema.Types.ObjectId; var userSchema = Schema( { _id: ObjectId, barcode: String, name: String, email: String, type: String } ); var Users = mongoose.model( 'Users', userSchema ); module.exports = Users; module.exports.schema = userSchema; 文件: items.js var mongoose = require( 'mongoose' ) Schema = […]

深度填充Mongoose多个二级对象不起作用

我有一个模型A机智这个领域: var field = { foo: String, b: [{ type: Schema.Types.ObjectId, ref: 'B' }] } 和这个领域的模型B : var field = { c: { type: Schema.Types.ObjectId, ref: 'C' } // let's say this has 3 string field d: { type: Schema.Types.ObjectId, ref: 'D' } // so was this } 基于Trinh Hoang Nhu的这个回答 , A.find({_id:req.params.id}) .populate({ path: […]

如何在mongoose填充嵌套的实体?

我有以下mongoose模式结构 userSchema = new Schema({ roles: [ role: {type: Schema.Types.ObjectId, ref: 'Role' } ] }) rolesSchema = new Schema({ name: String, roleEntities: [ { entity : {type: Schema.Types.ObjectId, ref: 'RoleEntity' }, abilities : [{type: Schema.Types.ObjectId, ref: 'Ability' }] } ] } roleEntitiesSchema = new Schema({ name: String }) abilitiesSchema = new Schema({ name: String }) […]

Mongoose深度填充结果在二级

花了1天的时间想完成工作之后,我想是时候请求帮忙了。 我想填充文档的两个级别。 我尝试与填充(),但它似乎只适用于第一级,而不是深填充! 我读了很多,我知道它应该工作,但我想我错过了一些真正愚蠢的东西… 请让我知道我犯了什么错误。 这里是相关的代码。 架构 var compositionSchema = new Schema({ name: { type: String, required: true }, contributions: [{ type: Schema.Types.ObjectId, ref: 'Contribution' }] }); mongoose.model('Composition', compositionSchema); var contributionSchema = new Schema({ name: { type: String, required: true }, media: { type: Schema.Types.ObjectId, ref: 'Media' } }); mongoose.model('Contribution', contributionSchema); var mediaSchema = new […]

瘦()里面填充mongoose

我正在使用mongoose填充用户集合,并且希望在用户使用社交帐户注册时设置标志。 所以我select填充的领域。 但由于安全问题,我不想通过它作为回应。 所以我使用lean()将它转换为纯对象,并将值设置为null。 但是,一旦某个特定用户的值被设置为空,那么它将为该user_id设置值为null。 如何将它们设置为空或如何将它们隐藏起来以免响应。 Pins.find(condition).limit(limit).sort({time: -1}).populate({path: 'user_id',select: '_id name pic twitter_id fb_id',options: { lean: true}}).lean().exec(function(err, data) { if(data){ var flag = 0 async.eachSeries(data, function(v, callback1) { if(v.user_id.twitter_id || v.user_id.fb_id ){ v.socialaccount=1 v.user_id.fb_id ='' v.user_id.twitter_id ='' }else{ v.socialaccount=0 v.user_id.fb_id ='' v.user_id.twitter_id ='' } callback1() },function(){ console.log(data) callback(data) }) } else { callback(data) } }) […]

在Mongoose中为汇总调用中的引用文档填充字段值

我有两个基本模式用户和课程 //用户模型 var UserSchema = new Schema({ userId: { type: String, default: '', required: 'Please provide UserId.', index : true }, Name :{ type: String, default: '', trim : true } }); //课程模式 var CourseSchema = new Schema({ title: { type: String, default: '', required: 'Please fill course title', trim: true, index: true }, description: […]

Mongoose +在子文档中填充一个属性

随着mongoose,我想在我的主文档的子文档中填充一个属性。 这是我的模特: var device = { deviceId: {type: String, required: true}, number: {type: Number, default: 0} }; var notification = new Schema({ createdAt: {type: Date, default: Date.now}, device: {type: Schema.ObjectId, ref: 'Device'} }); var clientSchema = new Schema({ […] information: { code: {type: String, required: true}, name: {type: String, required: true} }, notifications: [notification], […] […]