Populate many subdocument levels down in mongoose - javascript

I'm creating a comment system where a comment can have subComments (when you comment on a comment). I can say how many levels deep I want to populate the subComments, but I don't know how many there are in advance. Is there a way to tell mongoose to just keep populating the subComments of already populated comments until there are no more subDocuments?
CommentModel.js
const mongoose = require("mongoose");
const schema = mongoose.Schema;
const commentSchema = new schema(
{
post: { type: schema.Types.ObjectId, required: true, ref: "post" },
content: { type: String, required: true },
votes: { type: Number, required: true },
user: { type: schema.Types.ObjectId, required: true, ref: "user" },
subComments: [{ type: schema.Types.ObjectId, ref: "comment" }],
parentComment: { type: schema.Types.ObjectId, ref: "comment" },
},
{ timestamps: true }
);
module.exports = Comment = mongoose.model("comment", commentSchema);
PostRouter.js
router.get("/full/:postId", async (req, res) => {
const postId = req.params.postId;
const post = await Post.findById(postId).populate({
path: "comments",
populate: {
path: "subComments",
},
// how can i populate infinitely down in the path subComments?
});
res.json(post);
});

Please check the graph lookup feature: https://docs.mongodb.com/manual/reference/operator/aggregation/graphLookup/
Example with arrays can be found here: https://www.oodlestechnologies.com/blogs/how-to-use-graphlookup-in-mongodb/

Related

How to fill multiple models with mongodb using "population"

I am trying to display all the data with the "populate" statement. But I only get one "populate" but when I put many it doesn't work.
what I want to do is to bring me the data from the "User", "Customer" model as well.
This is my code
My Model:
import { model, Schema } from 'mongoose';
const RoomSchema = new Schema({
Users: [
{
agentId: {
type: Schema.Types.ObjectId,
ref: "User",
required: false,
},
customerId: {
type: Schema.Types.ObjectId,
ref: "Customer",
required: false,
},
typeId: Number, // 1 - agent, 2 - client
},
],
Messages: [
{
agentId: {
type: Schema.Types.ObjectId,
ref: "User",
required: false,
},
customerId: {
type: Schema.Types.ObjectId,
ref: "Customer",
required: false,
},
message: {
type: String,
required: true,
},
date: Date,
sender: Number,
},
],
FinishAt: Date,
FinishBy: String,
typeFinishBy: Number, // 1 - agent, 2 - client
}, {
timestamps: true,
versionKey: false
});
export default model('Room', RoomSchema);
this is the sentence I am using
import Room from "../models/Room.js";
async function getOnlyRoom(id) {
const foundRoom = await Room.findById(id)
.populate('Users.agentId')
.populate('Users.customerId')
.populate('Messages.agentId')
.populate('Messages.customerId')
.execPopulate();
return foundRoom
}
Image of Json Postman
only works with one
foundRoom.populate('Users.customerId')
Works with only populate
this is the error
image of error
Thank you very much for your help
I have found the solution to my problem
I was researching the solution on the mongoose website and did it with a single populate.
const populateRoom = await foundRoom
.populate([
{ path: 'Users.agentId', select: 'name' },
{ path: 'Users.customerId', select: 'name' },
{ path: 'Messages.agentId', select: 'name' },
{ path: 'Messages.customerId', select: 'name' }
])
return populateRoom;

NodeJs How can i show first 3 authors in my model?

I made a simple api. With 3 tables.(collections with mongodb). Like Collections-Authors-Articles. I want to show 3 author on collections but i cant found how can i do it ? When i send get request to collections on postman i need to see like;
"name":xxx;
"icon":xxx;
"authors":{
authorid,
authorid,
authorid }
My Schemes :
article.js =
const { db } = require("../collection");
const mongoose = require("mongoose"),
Schema = mongoose.Schema;
var Articles = Schema({
author: {
type: Schema.Types.ObjectId,
ref: "Authors",
},
collectionid: {
type: Schema.Types.ObjectId,
ref: "Collections",
},
name: {
type: String,
required: true,
},
content: {
type: String,
required: true,
},
createddate: {
type: Date,
default: Date.now,
},
updateddate: {
type: Date,
default: Date.now,
},
});
module.exports = mongoose.model("Articles", Articles);
author.js =
const mongoose = require("mongoose"),
Schema = mongoose.Schema;
var Authors = Schema({
name: {
type: String,
required: true,
},
avatar: {
type: String,
required: true,
},
});
module.exports = mongoose.model("Authors", Authors);
collections.js =
const mongoose = require("mongoose"),
Schema = mongoose.Schema;
var Collections = Schema({
name: {
type: String,
required: true,
},
icon: {
type: String,
required: true,
},
});
module.exports = mongoose.model("Collections", Collections);
You can use populate to populate collection authors, see the mongoose docs

Mongoose getting the error: MissingSchemaError: Schema hasn't been registered for model

I am getting this error and can't figure it out. I HAVE NAMED THE REF EXACLTY AS THE MODEL:
MissingSchemaError: Schema hasn't been registered for model "ParticipantStatus".
Here are my models:
ParticipantStatus model:
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const participantStatusSchema = new Schema({
_id: {
type: Number,
required: true,
},
name: {
type: String,
required: true,
},
});
module.exports = mongoose.model('ParticipantStatus', participantStatusSchema);
EventParticipant model:
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const eventParticipantSchema = new Schema(
{
userId: {
type: Schema.Types.ObjectId,
ref: 'User',
required: true,
},
eventId: {
type: Schema.Types.ObjectId,
ref: 'Event',
required: true,
},
teamId: {
type: Schema.Types.ObjectId,
ref: 'Team',
},
statusId: {
type: Number,
ref: 'ParticipantStatus',
required: true,
},
isActive: {
type: Boolean,
required: true,
default: true,
},
},
{ timestamps: true }
);
module.exports = mongoose.model('EventParticipant', eventParticipantSchema);
This is the code where the error is thrown, when i try to get event participants and populate statusId(ParticipantStatus):
let participants = await EventParticipant.find(
{
eventId: req.params.eventId,
isActive: true,
},
{ _id: false }
)
.populate('userId', 'email')
.populate('statusId')
.select('userId');
I have been stuck for hours on this. Only the .populate('statusId') part throws the error, the rest works well. Thanks in advance
type of field that you want to populate based on should be ObjectId so change the type of statusId from Number to Schema.Types.ObjectId like this:
statusId: {
type: Schema.Types.ObjectId,
ref: 'ParticipantStatus',
required: true,
}
Well the problem was that u need to import:
const ParticipantStatus = require('../models/participantStatus.model');
even if you do not REFERENCE IT DIRECTLY in your code, which is really strange in my opinion.
Hope this helps anyone.

Mongoose Virtual reference between two ObjectIds

I know it is possible to create a virtual reference using a local _id to a foreign ObjectId, but is it possible to use a local ObjectId to a foreign ObjectId?
I'm trying to figure out if I have a bug somewhere or if this is not possible because it's not working.
const Post = new mongoose.Schema({
user: {
type: mongoose.Schema.Types.ObjectId,
ref: 'user',
},
text: {
type: String,
}
})
const ProfileSchema = new mongoose.Schema({
user: {
type: mongoose.Schema.Types.ObjectId,
ref: 'user',
},
bio: {
type: String,
max: 500,
}
});
ProfileSchema.virtual('posts', {
ref: 'Post',
localField: 'user',
foreignField: 'user',
});
// query
const profile = await Profile.findOne({
user: req.user.id,
}).populate('posts')
Yes it is possible, but you need to add { toJSON: { virtuals: true } } to the Schema.
const ProfileSchema = new mongoose.Schema(
{
user: {
type: mongoose.Schema.Types.ObjectId,
ref: 'user',
},
bio: {
type: String,
max: 500,
},
},
{
toJSON: { virtuals: true },
}
);
From docs:
Keep in mind that virtuals are not included in toJSON() output by default. If you want populate virtuals to show up when using functions that rely on JSON.stringify(), like Express' res.json() function, set the virtuals: true option on your schema's toJSON options.
https://mongoosejs.com/docs/populate.html#populate-virtuals

Node mongodb stores data as string instead of an object

I have two same functions, first saves some stuff and also saves ref to productId and second saves another stuff and saves ref to productId too. The problem is first writes ref to poductId as object and everything is ok but second function saves ref as string and in mongodb i see ObjectId but when im trying to display data on screen i cant access into object i have only string with ID of refer. Any ideas?
first model which is ok
const mongoose = require('mongoose')
const dishPositionsSchema = mongoose.Schema({
productId: {
type: mongoose.Schema.Types.ObjectId,
required: true,
ref: 'Products'
},
dishId: {
type: mongoose.Schema.Types.ObjectId,
required: true,
ref: 'Dishes'
},
weight: {
type: Number,
required: true
}
})
module.exports = mongoose.model('DishPositions', dishPositionsSchema)
and second which stores only strings instead of objects
const mongoose = require('mongoose')
const diaryPositionsSchema = mongoose.Schema({
productId: {
type: mongoose.Schema.Types.ObjectId,
required: true,
ref: 'Products'
},
dishId: {
type: mongoose.Schema.Types.ObjectId,
ref: 'Dishes'
},
weight: {
type: Number,
required: true,
required: true
},
timeOfEatingId: {
type: mongoose.Schema.Types.ObjectId,
required: true,
ref: 'TimeOfEating'
},
date: {
type: String,
required: true
}
})
module.exports = mongoose.model('DiaryPositions', diaryPositionsSchema)
I know where i did mistake... i didnt write populate('productId')... everything is fine now thanks!

Categories

Resources