Mongoose - query to get data from multiple collections - node.js

I want to get query of the mongoose in nodejs application as describe below out put.
user.js, comment.js and post.js are the model files I used.
user.js
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var ObjectId = Schema.ObjectId;
var userSchema = new Schema({
nick_name:{type:String},
email: {
type: String,
trim: true,
required: '{PATH} is required!',
index: true,
},
},{ collection: 'user'});
var User = mongoose.model('User', userSchema);
module.exports = User;
comment.js
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var ObjectId = Schema.ObjectId;
var commentSchema = new Schema({
comment: type:String,
user_id:{
type:Schema.Types.ObjectId, ref:'User'
},
is_active :1
},{ collection: 'comment'});
post.js
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var ObjectId = Schema.ObjectId;
var postSchema = new Schema({
post: type:String,
user_id:{
type:Schema.Types.ObjectId, ref:'User'
},
is_active :1
},{ collection: 'post'});
wants to get out put as follows:
{
"nick_name":"prakash",
"email":"prakash#mailinator.com",
"comments":[
{
"comment":"this is a comment text1",
"is_active":1,
},
{
"comment":"this is a comment text2",
"is_active":1,
}
],
"posts":[
{
"post":"this is a post text1",
"is_active":1,
},
{
"post":"this is a post text2",
"is_active":1,
},
{
"post":"this is a post text3",
"is_active":1,
},
]
}
dependencies
"express" => "version": "4.7.4",
"mongoose" => "version": "4.4.5",
"mongodb" => "version": "2.4.9",
"OS" => "ubuntu 14.04 lts 32bit",
if query is not possible ,please suggests me a proper mongoose plugn.
but I don't want to any changes in user.js file and its userSchema object.

There are no 'joins' in Mongo. But what you would do is change your User Schema to store the ObjectId's of the Comment and Post documents in an array of your User. Then use 'populate' when you need the data with the user.
const userSchema = new Schema({
nick_name:{type:String},
email: {
type: String,
trim: true,
required: '{PATH} is required!',
index: true,
},
comments: [{ type: Schema.Types.ObjectId, ref:'Comment' }],
posts: [{ type: Schema.Types.ObjectId, ref:'Post' }]
}, {timestamps: true});
mongoose.model('User', userSchema);
Your query would then look something like this:
User.find()
.populate('comments posts') // multiple path names in one requires mongoose >= 3.6
.exec(function(err, usersDocuments) {
// handle err
// usersDocuments formatted as desired
});
Mongoose populate docs

It is possible .you should use aggregation.
it should work.
Initiate the variable
var mongoose = require('mongoose');
var userCollection = require('./user');//import user model file
var resources = {
nick_name: "$nick_name",
email: "$email"};
userCollection.aggregate([{
$group: resources
}, {
$lookup: {
from: "Comments", // collection to join
localField: "_id",//field from the input documents
foreignField: "user_id",//field from the documents of the "from" collection
as: "comments"// output array field
}
}, {
$lookup: {
from: "Post", // from collection name
localField: "_id",
foreignField: "user_id",
as: "posts"
}
}],function (error, data) {
return res.json(data);
//handle error case also
});

Of course it is possible, you just have to use populate, let me tell you how:
Import your schemas
var mongoose = require('mongoose');
var userSch = require('userSchema');
var postSch = require('postSchema');
var commSch = require('commentSchema');
Init all the necessary vars
var userModel = mongoose.model('User', userSch);
var postModel = mongoose.model('Post', postSch);
var commModel = mongoose.model('Comment', commSch);
And now, do the query
postModel.find({}).populate('User')
.exec(function (error, result) {
return callback(null, null);
});
commModel.find({}).populate('User')
.exec(function (error, result) {
return callback(null, null);
});
This way you get the user inside of your comment and your post, to get the post and comments inside of your user, you have to do 3 queries, one for the user, one for the comments and one for the post, and mix all together

you can consider using populate virtual with both comments and posts like docs (https://mongoosejs.com/docs/populate.html#populate-virtuals) in model User. And using like this:
User.find({filter}).populates('virtualComments').populate('virtualPosts')

Adding on to the answers above: What if we only want a few specific fields returned for the populated documents? This can be accomplished by passing the usual field name syntax as the second argument to the populate method:
Story.
findOne({ title: /casino royale/i }).
populate('author', 'name'). // only return the Persons name
exec(function (err, story) {
if (err) return handleError(err);
console.log('The author is %s', story.author.name);
// prints "The author is Ian Fleming"
console.log('The authors age is %s', story.author.age);
// prints "The authors age is null"
});
Reference: https://mongoosejs.com/docs/populate.html#field-selection

Use aggregate to do your work in a single query which is almost like a join.
var mongoose = require('mongoose');
var userModel = require('./user');//import user model file
let result = await userModel.aggregate([
{
$match: {
user: mongoose.Types.ObjectId(req.body.user_id),//pass the user id
}
},
{
$lookup: {
from: "comments",//your schema name from mongoDB
localField: "_id", //user_id from user(main) model
foreignField: "user_id",//user_id from user(sub) model
pipeline: [
{
$project:{ //use to select the fileds you want to select
comment:1, //:1 will select the field
is_active :1,
_id:0,//:0 will not select the field
}
}
],
as: "comments",//result var name
}
},
{
$lookup: {
from: "post",//your schema name from mongoDB
localField: "_id", //user_id from user(main) model
foreignField: "user_id",//user_id from user(sub) model
pipeline: [
{
$project:{//use to select the fileds you want to select
post:1,//:1 will select the field
is_active :1,
_id:0,//:0 will not select the field
}
}
],
as: "posts",//result var name
}
},
{
$project:{//use to select the fileds you want to select
nick_name:1,//:1 will select the field
email:1,
_id:0,//:0 will not select the field
comments:1,
posts:1
}
}
])

Related

How to find documents by a field from a ref?

Suppose I have the following two schemas:
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const schemaA = new Schema({
tag: [
{
type: mongoose.Schema.Types.ObjectId,
ref: 'schemaB',
},
],
});
module.exports = SchemaA = mongoose.model('schemaA', schemaA);
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const schemaB = new Schema({
name: {
type: String,
}
});
module.exports = SchemaB = mongoose.model('schemaB', schemaB);
How can I find all documents in SchemaA that have their tag field match the name of SchemaB?
I know that if I had the _id of SchemaB, I'd be able to do this as,
const schemaBId = 'some-_id-string'
const docs = await SchemaA.find({ tag: schemaBId })
But how can I do this, if I only have the name value?
first use lookup to get data of ModelB and then put these data as t after that make query with match to find where name of ModelB is test
ModelA.aggregate([
{
$lookup: {
from: 'collectionB',
localField: 'tag',
foreignField: '_id',
as: 't',
},
},
{
$match: { 't.name': 'test' },
},
]);
You can try using mongoose populate() function it might help you achieve exactly what you want. Here is the populate docs for quick reference.

I can't populate data using MongoDB and Node

these are the two models that I have.
user.js
const mongoose = require("mongoose");
const userSchema = new mongoose.Schema({
email: {
type: String,
},
passwordHashed: {
type: String,
},
role: {
type: String,
},
student: [{ type: mongoose.Schema.Types.ObjectId, ref: "Student" }],
});
const Users = mongoose.model("User", userSchema);
module.exports = Users;
student.js
const mongoose = require("mongoose");
const studentSchema = new mongoose.Schema({
email: {
type: String,
},
role: {
type: String,
},
});
const Students = mongoose.model("Student", studentSchema);
module.exports = Students;
My problem is I want to populate the data to user.js but when I am running this code:
const user = await Users.find().populate('student')
console.log(user)
It just returns me an empty array of the student. How can I fix this problem? Thank you in advance.
For any type population/join I all time use mongodb $lookup.
It's much powerful, flexible and covenant than using population.
According your userSchema, most probably you have store the _id of student document on userSchema's student array.
If I'm correct then you can follow this way:
await Users.aggregate([
{
"$lookup": {
"from": "students",
"localField": "student",
"foreignField": "_id",
"as": "studentList"
}
}
])
Though your student field of userSchema is objectId, then you don't further need to do anything, else you have to convert your localField as mongoose.Types.ObjectId(student)
here is an example: https://mongoplayground.net/p/fuw9Drld9M-

How to use $lookup in a project where there are multiple databases?

Here's my product.model.js
var mongoose = require('mongoose');
var Schema = mongoose.Schema; var ObjectId = mongoose.Schema.Types.ObjectId;
const ProductModel = new Schema(
{
name: {
type: String
},
//hidden code for better viewing
policyId: {
type: mongoose.Schema.Types.ObjectId
}
},
{
strict: false,
timestamps: true
}
);
const myDB = mongoose.connection.useDb('SampleDB');
module.exports = myDB.model('bf-product', ProductModel);
Here's my return-policy.model.js
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var ObjectId = mongoose.Schema.Types.ObjectId;
const ReturnPolicyModel = new Schema(
{
name: {
type: String
},
description: {
type: String
},
addedById: {
type: String
},
status: {type: Number, default: 1}
},
{
strict: false,
timestamps: true
}
);
const myDB = mongoose.connection.useDb('SampleDB');
module.exports = myDB.model('bf-return-pol', ReturnPolicyModel);
Here's the query I'm running
const Product = require('../models/product.model');
let products = await Product.aggregate([
{
$lookup:
{
from: "bf-return-pol",
localField: "policyId",
foreignField: "_id",
as: "policies"
}
}
])
console.log(products)
When I run the query, policies always returns empty. I think the issue is because of the project using multiple databases, as I had a similar issue with paths with the populate keyword. So my question is this, how can I run the query as both the collections are in the same db?
product collection
return-policy collection
EDIT: The issue seemed to be that the collection was named as "bf-return-pols" and once I changed the from field in lookup, I could get the results.

How to get user name from user id in React.js

I have created this schema with mongoose
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const QuestionSchema = new mongoose.Schema({
user: {
type: Schema.Types.ObjectId,
ref: 'users'
},
question:{
type:String
},
name:{
type:String,
},
answerd:[
{
user:{
type:mongoose.Schema.Types.ObjectId,
ref:'users'
}
}
]
})
module.exports = Question = mongoose.model('question',QuestionSchema);
In my global state (REDUX) I have the state
const initialState = {
questions:[],
question:null,
loading:true,
error:{}
};
The element questions store question object which contains the name of the user who made the question, the question itself, and people who have answered.
Some where in a .js file I can get the id of users who have answered by simply
question.buzzed.map(user=> <h1> {user._id} </h1>),
but how is possible to get this user name, I also have a schema for user which have attributes such as name, id, ... etc
You should use the populate method. It is like join in SQL for mongoose, because it connects your answer to the user collection. Based on your code, it could look like this :
Question.find({}).populate("answerd")
or something like this:
Question.find().populate({ path: 'answerd', select: 'username' });
For more information please read the populate documentation
U can use $lookup.
var aggregate = [
{
$unwind: "$answerd"
},
{
$lookup: {
from: "users",
localField: "user",
foreignField: "_id",
as: "user"
}
}
];
Questions.aggregate(aggregate, function(err, users) {
})

I want to combine values from two collections to get data

There are user collections and bulletin collections. The bulletin collection has the author's uid. I want to give data to the username matching the uid in the User collection, not the author's uid, before giving the bulletin board's information to the client.
User Schema
var userSchema = new Schema({
"userId": String,
"userPw": String,
"userName": String,
});
var User = mongoose.model('user', userSchema);
Board Schema
var baseBoardSchema = new Schema({
title: String,
authorUid: String,
});
var freeBoardSchema = new Schema({
baseBoard : { type: baseBoardSchema, default: baseBoardSchema}
});
var freeBoard = mongoose.model('freeBoard', freeBoardSchema);
So I did below. But it generate error. How I do?
FreeBoard.aggregate()
.lookup({
from: User,
localField: 'baseBoard.authorUid',
foreignField: '_id',
as : 'users'
})
.exec(function(error, freeBoards) {
if(error) {
//error
}
});

Resources