mongoose-populate

How to exclude null values from Mongoose populate query

[亡魂溺海] 提交于 2021-02-19 01:55:07
问题 I am building an app and I have create 2 models. const UserSchema = new Schema({ _id: Schema.Types.ObjectId, account:{ type: String, unique: true }, email: String, first_name: String, last_name: String } const VenueSchema = new Schema({ _id: Schema.Types.ObjectId, venue_type: String, capacity: Number }) and const MediatorSchema = new Schema({ _id: Schema.Types.ObjectId, account:{ type: String, unique: true }, user: {type: Schema.Types.ObjectId, ref:'User' } venue: {type: Schema.Types.ObjectId

How to exclude null values from Mongoose populate query

僤鯓⒐⒋嵵緔 提交于 2021-02-19 01:55:04
问题 I am building an app and I have create 2 models. const UserSchema = new Schema({ _id: Schema.Types.ObjectId, account:{ type: String, unique: true }, email: String, first_name: String, last_name: String } const VenueSchema = new Schema({ _id: Schema.Types.ObjectId, venue_type: String, capacity: Number }) and const MediatorSchema = new Schema({ _id: Schema.Types.ObjectId, account:{ type: String, unique: true }, user: {type: Schema.Types.ObjectId, ref:'User' } venue: {type: Schema.Types.ObjectId

Clean up dead references with Mongoose populate()

别说谁变了你拦得住时间么 提交于 2021-02-18 18:59:49
问题 If a user has an array called "tags": var User = new Schema({ email: { type: String, unique: true, required: true }, tags: [{ type: mongoose.Schema.Types.ObjectId, ref:'Tag', required: true }], created: { type: Date, default: Date.now } }); and I do a populate('tags') on a query: User.findById(req.params.id) .populate("tags") .exec(function(err, user) { ... }); If one of the tags in the list has actually been deleted, is there a way to remove this dead reference in "tags"? Currently, the

Clean up dead references with Mongoose populate()

≡放荡痞女 提交于 2021-02-18 18:59:32
问题 If a user has an array called "tags": var User = new Schema({ email: { type: String, unique: true, required: true }, tags: [{ type: mongoose.Schema.Types.ObjectId, ref:'Tag', required: true }], created: { type: Date, default: Date.now } }); and I do a populate('tags') on a query: User.findById(req.params.id) .populate("tags") .exec(function(err, user) { ... }); If one of the tags in the list has actually been deleted, is there a way to remove this dead reference in "tags"? Currently, the

How to populate nested entities in mongoose?

三世轮回 提交于 2021-02-15 11:03:36
问题 I have the following mongoose schema structure 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 }) How can i populate all these nested documents while doing a find on the USER

How to populate nested entities in mongoose?

喜欢而已 提交于 2021-02-15 11:02:55
问题 I have the following mongoose schema structure 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 }) How can i populate all these nested documents while doing a find on the USER

mongoose populate in array of custom objects

主宰稳场 提交于 2021-02-11 14:42:28
问题 In the user model, I have an array of custom objects followedPlaylists which contains two attributes ( playlist: the id of the playlist, public: to determine whether it is public or not) as shown below const userSchema = new mongoose.Schema({ ..... other attributes followedPlaylists: [{ playlist: { type: mongoose.Schema.ObjectId, ref: 'Playlist', unique: true }, public: Boolean }] }) I want to populate on followedPlaylists.playlist so the response would be something like [{ playlist: * the

Populate does not seem to populate for all the array elements

心不动则不痛 提交于 2021-02-11 13:54:54
问题 Consider the code below: const aux = await Fasta.find({}, "healthyTissue") .limit(1) .populate({ //---------------------first level (healthy or tumor fasta file) ---------------- path: "healthyTissue", model: "Hidden", options: { limit: 2 }, //--------------------- second level (hidden documents) ---------------- populate: { path: "children", options: { limit: 2 }, model: "FastaElement" } }); This is the output: The problem: just the first children is populated . I have double-checked the

Mongoose populate across 2 databases

瘦欲@ 提交于 2021-02-08 13:58:10
问题 I have a node app that uses 2 databases. One is the the Names and the other for the rest of all the data. I have this connection setup: // 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' This connection is working properly. There's also no problem upon saving data to both app_db and names_db working perfect. But the problem is this: when I try to

How to rename path in response for populate

社会主义新天地 提交于 2021-02-04 23:57:33
问题 I have a query like this: galleryModel.find({_id: galleryId}) .populate({ model: 'User', path: 'objectId', select: 'firstName lastName' }) End response for objectId will be like this: objectId: { ... } How can I change it to user in response without changing real path? 回答1: You can do this by virtual populate, introduced in mongoose version 4.5 . For that you need to define a virtual field in mongoose schema. var GallerySchema = new mongoose.Schema({ name: String, objectId: { type: mongoose