getting nothing from lookup mongodb - node.js

I want to get all the posts with their author details from user model. I am using mongoDB lookup. But getting an empty array. I am matching author.uid from post to _id of user.
I want to get all the posts with their author details from user model. I am using mongoDB lookup. But getting an empty array. I am matching author.uid from post to _id of user.
//Post Model
const mongoose = require("mongoose");
const Schema = mongoose.Schema;
const postSchema = new Schema({
category : {
type: String
},
content: {
type: String
},
caption: {
type: String
},
tags: [{
type: String
}],
createdAt: {
type: Number,
required: true
},
author: {
uid:{
type: String,
required: true
},
name:{
type: String
}
},
likes:[{
type:String
}],
comments:[{
type: mongoose.Schema.Types.ObjectId,
ref: "Comment"
}]
});
module.exports = mongoose.model('Post', postSchema);
//User Model
const mongoose = require('mongoose');
const userSchema = new mongoose.Schema({
_id: {
type: String,
required: true
},
name:{
type: String,
required: true
},
avatar:{
type:String
},
bio:{
type: String
},
followers:[
{
type: String
}
],
followings:[
{
type: String
}
],
posts:[{
type: mongoose.Schema.Types.ObjectId,
ref: "Post"
}]
});
module.exports = mongoose.model('User', userSchema);
//Node js
const express = require('express');
const router = express.Router();
const Post = require('../../models/Post');
const User = require('../../models/user');
router.get('/', (req, res) => {
Post.aggregate([
{
$lookup:
{
from: 'User',
localField: "author.uid",
foreignField: "_id",
as: "creator"
}
}
]).exec((err, result) => {
if (err) {
console.log("error" ,err)
}
if (result) {
console.log(JSON.stringify(result));
}
});
});
//Output
{"_id":"5b9c7f30d",
"author": {"uid":"y08RxtsHe","name":"Sujoy Saha"},
"tags": ["#lo"],
"likes":[], "comments[],
"category":"image","content":"jsdnvs","caption":"standing
\n#lol","createdAt":1536982759517,"__v":0,"creator":[]}
You can see, i am getting empty creator array. Please help me out.

mongoose.js pluralizes (adds 's' after your model name) when it creates a collection in MongoDb.
Can you try with from: 'users' in your $lookup clause?

Related

Mongoose can find related documents with lookup but not populate

I have a simple Mongo DB with two models: Campaign and Donation, where a Campaign is related to many Donations.
Using mongoose I can query for a specific Campaign using an aggregation with a lookup() and it returns all of the related Donations. But if I try to use a populate() statement, no Donations are returned. I cannot figure out why.
Campaign
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
require('models/Donation');
const CampaignSchema = new Schema({
name: {
type: String,
required: true
},
goal: {
type: Number,
required: true
},
donations: [{
type: Schema.Types.ObjectId,
ref: "Donation"
}]
},
{
collection: 'campaigns'
});
mongoose.model('Campaign', CampaignSchema);
Donation
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
require('models/Campaign');
const DonationSchema = new Schema({
campaign: {
type: Schema.Types.ObjectId,
ref: "Campaign",
required: true
},
amount: {
type: Number,
required: true
},
date: {
type: Date,
required: true,
default: Date.now
}
},
{
collection: 'donations'
});
mongoose.model('Donation', DonationSchema);
This code will return a single Campaign based on _id and all of the associated donations:
const filter = {_id: mongoose.Types.ObjectId(req.params.id)};
Campaign.aggregate().match(filter).lookup({
from: "donations",
localField: "_id",
foreignField: "campaign",
as: "donations"}).then((results) => {
console.log(results[0].donations); // HAS ALL OF THE RELATED RECORDS
});
This code returns an empty array for donations:
const filter = {_id: mongoose.Types.ObjectId(req.params.id)};
const campaign = await Campaign.findOne(filter).populate('donations').then((c) =>{
console.log(c.donations); // EMPTY
});

How to query based on related element's field value?

In my database, every article has its own 6-digit identifying number (in addition to id). Every comment belongs to one of the articles. My question is, how to query comments knowing only the article number, but not the article's id?
controllers/fetch-comments.js:
const Article = require('../models/article')
const User = require('../models/user');
const Comment = require('../models/comment');
module.exports = async function (req, res) {
try {
let { article_number, articleId } = req.body
let filter;
console.log(article_number)
/* Here is my failed attempt: */
if (article_number) filter = { 'article.number': article_number };
if (articleId) filter = { 'article': articleId };
let projection = null
let options = null
let comments = await Comment.find(filter, projection, options).populate('author', 'username fullname').lean()
return res.status(200).send(comments)
} catch (error) {
console.log("error on getting comments: " + error.message);
return res.status(500).send('Server side error');
}
}
models/article.js:
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const articleSchema = new Schema({
author: { type: Schema.Types.ObjectId, ref: 'User', required: true },
title: { type: String, required: true },
content: { type: String, required: true },
coverPhotoUrl: { type: String, required: false },
number: { type: Number, required: true }, // number is public id for address bar
createdAt: { type: Date, required: true },
category: { type: String, required: false },
fake: { type: Boolean, required: false },
});
module.exports = mongoose.model('Article', articleSchema);
models/comment.js:
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const commentSchema = new Schema({
author: { type: Schema.Types.ObjectId, ref: 'User', required: true },
content: { type: String, required: true },
createdAt: { type: Date, required: true },
article: { type: Schema.Types.ObjectId, ref: 'Article', required: true }
});
module.exports = mongoose.model('Comment', commentSchema);
Please do not suggest doing 2 queries intead of one, I already know how to do that.
You can use the aggregation framework and use a $match and $lookup operator to get the comment of a particular article number.
Following would be the code for the same:
db.Article.aggregate( [
{ $match : { number : article_number} },
{
$lookup:
{
from: "Comment",
localField: "_id",
foreignField: "article",
as: "article_comments"
}
}
] )
This will return an array of the matching comments.

500 Error when using method populate in Express and Mongo

I cannot fetch users data from Mongo when I use populate() method for posts of users.
router.get("/users", (req, res) => {
User.find()
.populate("posts")
.exec()
.then((users) => {
res.json({ users });
})
.catch((err) => console.log(err));
});
Mongo Schema
const mongoose = require("mongoose");
const Schema = mongoose.Schema;
const { ObjectId } = mongoose.Schema.Types;
const Post = require("../models/Post");
const UserSchema = new Schema({
name: {
type: String,
required: true
},
username: {
type: String,
requied: true
},
userimg: {
type: String
},
followers: [
{
type: ObjectId,
ref: "User"
}
],
following: [
{
type: ObjectId,
ref: "User"
}
],
posts: [
{
type: ObjectId,
ref: "Post"
}
],
email: {
type: String,
required: true
},
password: {
type: String,
required: true
},
date: {
type: Date,
default: Date.now
}
});
module.exports = User = mongoose.model("users", UserSchema);
When I remove .populate("posts") than I get 200 Success status and users data but without popoulation of referred posts from the posts collection.
So i'll be doing a little modification to your model file
const mongoose = require("mongoose");
const Schema = mongoose.Schema;
const Post = require("../models/Post");
const UserSchema = new Schema({
name: {
type: String,
required: true
},
username: {
type: String,
requied: true
},
userimg: {
type: String
},
followers: [
{
type: Schema.Types.ObjectId,
ref: "User"
}
],
following: [
{
type: Schema.Types.ObjectId,
ref: "User"
}
],
posts: [
{
type: Schema.Types.ObjectId,
ref: "Post"
}
],
email: {
type: String,
required: true
},
password: {
type: String,
required: true
},
date: {
type: Date,
default: Date.now
}
});
module.exports = User = mongoose.model("User", UserSchema);
Can you change your exec() to execPopulate()
router.get("/users", (req, res) => {
User.find()
.populate("posts")
.execPopulate()
.then((users) => {
res.json({ users });
})
.catch((err) => console.log(err));
});
Thank you all for your help. I found the error:
ref: "posts" instead of ref: "Post"
ref: "users" instead of ref: "User"

mongoose.populate() returning an empty array

I know, this is one of the popular questions out there. On populate I expect it to return the user with his posts, but it's returning an empty array of posts.
Here is my User model.
User.js
const mongoose = require("mongoose")
const Schema = mongoose.Schema
const userSchema = new Schema({
username: { type: String, required: true },
email: { type: String, required: true },
password: { type: String, required: true },
posts: [{
type: mongoose.Schema.Types.ObjectId,
ref: "Post"
}]
}, { timestamps: true })
const User = mongoose.model("User", userSchema)
module.exports = User
Post.js
const mongoose = require("mongoose")
const Schema = mongoose.Schema
const postSchema = new Schema({
postTitle: { type: String, required: true },
postDescription: { type: String, required: true },
user: { type: Schema.Types.ObjectId, ref: "User" },
}, { timestamps: true }
)
const Post = mongoose.model("Post", postSchema)
module.exports = Post
router.get("/posts/:id", usersController.getUserPosts)
usersController.js
getUserPosts: (req, res) => {
User.findById(req.params.id).populate("posts").exec((err, posts) => {
if (err) console.log(err)
console.log(posts)
})
}
I'm getting this:
{ posts: [],
_id: 5e4e3e7eecd9a53c185117d4,
username: 'rick',
email: 'rick#gmail.com',
createdAt: 2020-02-20T08:08:30.878Z,
updatedAt: 2020-02-20T08:08:30.878Z,
__v: 0 }
Where am I going wrong?

Mongoose - model methods not found in middleware

It's possible that i'm just burned out, but I have the following models:
user
const mongoose = require('mongoose');
const validate = require('mongoose-validator');
const Post = require('./post');
let UserSchema = mongoose.Schema({
firstName: { type: String, required: true },
lastName: { type: String, required: true },
email: {
type: String, required: true, lowercase: true, trim: true, unique: true, index: true,
validate: [validate({ validator: 'isEmail', message: 'Invalid Email!' })]
},
posts: [{ type: mongoose.Schema.Types.ObjectId, ref: 'Post' }]
})
module.exports = mongoose.model('User', UserSchema);
posts
const _ = require('lodash');
const mongoose = require('mongoose');
const User = require('./user');
let PostSchema = mongoose.Schema({
user: { type: mongoose.Schema.Types.ObjectId, ref: 'User', required: true },
title: { type: String, required: true },
body: { type: String, require: true }
})
PostSchema.post('save', async function (next) {
await User.update({ _id: this.user }, { $push: { posts: this._id } })
return next();
})
module.exports = mongoose.model('Post', PostSchema);
When trying to add a new post, the post save hook runs, but I get the error User.update is not a function (same goes for findOneAndUpdate, findOne, etc).
I can call user.update from the rest of the app without issues, so not sure whats happening here. Both models are in the same directory.
What you missed is that post middleware has the first argument as the "document" and not the next handler:
user.js
const { Schema } = mongoose = require('mongoose');
const userSchema = new Schema({
firstName: String,
lastName: String,
posts: [{ type: Schema.Types.ObjectId, ref: 'Post' }]
});
post.js
const { Schema } = mongoose = require('mongoose');
const User = require('./user');
const postSchema = new Schema({
user: { type: Schema.Types.ObjectId, ref: 'User' },
title: String,
body: String
});
// note that first argument is the "document" as in "post" once it was created
postSchema.post('save', async function(doc, next) {
await User.update({ _id: doc.user._id },{ $push: { posts: doc._id } });
next();
});
index.js
const { Schema } = mongoose = require('mongoose');
const User = require('./user');
const Post = require('./post');
const uri = 'mongodb://localhost/posttest';
mongoose.set('debug', true);
mongoose.Promise = global.Promise;
const log = data => console.log(JSON.stringify(data, undefined, 2));
(async function() {
try {
const conn = await mongoose.connect(uri);
await Promise.all(Object.entries(conn.models).map(([k,m]) => m.remove()));
let user = await User.create({ firstName: 'Ted', lastName: 'Logan' });
let post = new Post({ user: user._id, title: 'Hi', body: 'Whoa!' });
post = await post.save();
mongoose.disconnect();
} catch(e) {
console.error(e)
} finally {
process.exit()
}
})()
Returns:
Mongoose: users.remove({}, {})
Mongoose: posts.remove({}, {})
Mongoose: users.insertOne({ posts: [], _id: ObjectId("5b0217001b5a55208150cc9b"), firstName: 'Ted', lastName: 'Logan', __v: 0 })
Mongoose: posts.insertOne({ _id: ObjectId("5b0217001b5a55208150cc9c"), user: ObjectId("5b0217001b5a55208150cc9b"), title: 'Hi', body: 'Whoa!', __v: 0 })
Mongoose: users.update({ _id: ObjectId("5b0217001b5a55208150cc9b") }, { '$push': { posts: ObjectId("5b0217001b5a55208150cc9c") } }, {})
Showing that the update fires with the correct detail.
In good design you really should avoid this and simply drop the posts array from the User model. You can always either use a virtual instead:
userSchema.virtual('posts', {
ref: 'Post',
localField: '_id',
foreignField: 'user'
})
Or just get the data via $lookup:
User.aggregate([
{ "$match": { "_id": userId } }
{ "$lookup": {
"from": Post.collection.name,
"localField": "_id",
"foreignField": "user",
"as": "posts"
}}
])
Storing and maintaining arrays of related ObjectId values "on the parent" is kind of an "anti-pattern" and leads to unnecessary overhead such as writing in two places where you only need "one".
Also in general you should be opting for embedding "first", and only considering "referencing" if and when the usage pattern of the application actually demands it. Simply copying the same patterns of an RDBMS with a database engine that was not designed for that is not the best way to utilize it.

Resources