express.js mongoose populate 2 model - node.js

I'm want to join collection mongoDB but I've 2 model in project.
ADMINDETAIL and ADMINDETAIL get UID from member.model.js .
How I populate that.
queue.model.js
var mongoose = require("mongoose");
var Schema = mongoose.Schema;
var queueSchema = Schema(
{
QUEUE: String,
UID: String,
DATETIME: String,
ADMIN_ID: String,
USERDETAIL:{
type: Schema.Types.String,
ref:"MEMBER"
},
ADMINDETAIL:{
type: Schema.Types.String,
ref:"MEMBER"
},
},
{
collection: "QUEUE"
}
);
var QUEUE = mongoose.model("QUEUE", queueSchema);
module.exports = QUEUE;
member.model.js
var mongoose = require("mongoose");
var memberSchema = mongoose.Schema(
{
UID: {type: String},
NAME: {type: String},
SURNAME: {type: String},
IDNUMBER: {type: String},
PHONE: {type: String},
ADDRESS: {type: String},
},
{
collection: "MEMBER"
}
);
var MEMBER = mongoose.model("MEMBER", memberSchema);
module.exports = MEMBER;
queue.router.js
// GET QUEUE BY USER
router.get("/byuid/:UID", (req, res) => {
var {UID} = req.params;
Queue.find({UID})
.populate({Path:"USERDETAIL",model:"MEMBER"})
.populate({Path:"ADMINDETAIL",model:"MEMBER"})
.exec((err, data) => {
if (err) return res.status(400).send(err);
return res.status(200).send(data);
});
});
Error I got.
TypeError: utils.populate: invalid path. Expected string. Got typeof `object`

change the type of filed from String to ObjectId like this:
USERDETAIL:{
type: Schema.Types.ObjectId ,
ref:"MEMBER"
},
ADMINDETAIL:{
type: Schema.Types.ObjectId ,
ref:"MEMBER"
},
},
add your new data after that you can like this for population:
.populate("USERDETAIL ADMINDETAIL")
or
.populate([{
path: 'USERDETAIL ',
model: 'MEMBER'
}, {
path: 'ADMINDETAIL',
model: 'MEMBER'
}])
I think you are missing []

Related

How to poulate a a field in a mongoose schema which is refrenced to another schema , and this schema is further refrenced to another schema?

The field (viewed_posts) i want to populate in User Schema:
viewed_posts: [
{
type: mongoose.Schema.Types.ObjectId,
ref: "Viewed"
}
]
Viewed Schema :
var viewedSchema = new mongoose.Schema({
hitsByUser: {type: Number, default: 0},
viewsByUser: {type: Number, default: 0},
post: {
type: mongoose.Schema.Types.ObjectId,
ref: "Post"
}
});
Post Schema :
var mongoose = require('mongoose');
var passportLocalMongoose = require("passport-local-mongoose");
var uniqueValidator = require('mongoose-unique-validator');
var marked = require('marked');
var slugify = require('slugify');
// this is done for sanitizing html so that user cannot write a script in the input
const createDomPurify = require('dompurify')
const {JSDOM} = require('jsdom')
const dompurify = createDomPurify(new JSDOM().window)
var postSchema = new mongoose.Schema({
postNumber: Number,
title: String,
content: String,
subject: String, // currently we have 4 subjects so one out of 4 subjects will be stored here
likes: {type:Number,default:0},
// likes: {
// id:{
// type: mongoose.Schema.Types.ObjectId,
// ref: "User"
// }
// },
views: {type:Number,default:0},
actualViews: {type:Number,default:0},
shares: Number,
isReviewedByAdmin: {type: Boolean, default: false},
isReviewedByAuditor: {type: Boolean, default: false},
author: {
id:{
type: mongoose.Schema.Types.ObjectId,
ref: "User"
},
username: {
type: String
}
},
publish_date: {
type: String,
default: Date.now
},
publishDay: String,
slug: {
type: String,
required: true,
},
sanitizedHtml: {
type: String,
required: true
},
imagename: String //2002-12-09
});
I wish to see whole structure printed , but i can only populate viewed_posts, how can i populate "post"
which is inside viewed Schema and see here:
User.findById(req.user._id).populate("viewed_posts").exec((err,ans)=>{
if(err) console.log(err)
else{
console.log("this is the answer ",ans)
}})
The output i get:
},
{
hitsByUser: 0,
viewsByUser: 0,
_id: 5f9e85aeec37700f54a4d029,
post: 5f9a93d38d7cf8544ce9cc21,
__v: 0
},
{
hitsByUser: 0,
viewsByUser: 0,
_id: 5f9e85d61841000478c85f8a,
post: 5f82773f1998150024d4c8fc,
__v: 0
},
But i expect this post to be expanded too, instead of just showing id , How can i achieve it. Any Help Would be appreciated.
Mongoose supports nested populating (see in the docs: https://mongoosejs.com/docs/populate.html#deep-populate).
Note that you have to specify your model name of post schema where I´ve put the "post-model-name" placeholder.
So you could try something like this:
User.findById(req.user._id)
.populate({
path: 'viewed_posts',
populate: {
path: 'post',
model: 'post-model-name'
}
})
.exec();

Promise block my reference on MongoDB/Node.js

I use Mongoose to MongoDb with Node.js/React/GraphQL.
I have a document Article who is related to another document Event who is related to several documents Tags. When I try to save my documents I always have a pending promise to into my tags in the Event document.
Result :
- Article is save related to Event
- Event is saved but not related to Tags
- Tags are saved but not related to Event
Expecting :
- Article is save related to Event
- Event is saved and related to Tags
- Tags are saved and related to Event
Two time, when my server was on the beginning is working without pending and error. So I think my problem is a time problem, but I don't know how to resolve it. I try to put some timeout but without success.
I have the following schema in Mongoose/MongoDb
//mode/event.js
'use strict';
//import dependency
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
//create new instance of the mongoose.schema. the schema takes an object that shows
//the shape of your database entries.
var EventSchema = new Schema({
createdAt: {
type: Date,
default: Date.now
},
name: {
type: String,
required: 'Kindly enter the name of the event'
},
description: String,
site_web: String,
themes: {
type: String,
enum: ['Economics', 'Politics', 'Bitcoins', 'Sports'],
default: 'Economics'
},
picture: String,
event_date_start: Date,
event_date_end: Date,
type_event: {
type: String,
enum: ['Confrontation','Standard'],
default: 'Standard'
},
teams: [{
type: mongoose.Schema.Types.ObjectId,
ref: 'Team'
}],
tags: [{
type: mongoose.Schema.Types.ObjectId,
ref: 'Tag'
}]
});
//export our module to use in server.js
module.exports = mongoose.model('Event', EventSchema);
//model/tag.js
'use strict';
//import dependency
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
//create new instance of the mongoose.schema. the schema takes an object that shows
//the shape of your database entries.
var TagSchema = new Schema({
name: {
type: String,
required: 'Kindly enter the name of the tag'
},
});
//export our module to use in server.js
module.exports = mongoose.model('Tag', TagSchema);
//model/article.js
'use strict';
//import dependency
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
//create new instance of the mongoose.schema. the schema takes an object that shows
//the shape of your database entries.
var ArticleSchema = new Schema({
// _id: String,
createdAt: {
type: Date,
default: Date.now
},
event: {
type: mongoose.Schema.Types.ObjectId,
ref: 'Event',
required: 'Kindly enter the event'
},
body: String,
type: {
type: String,
enum: ['Confrontation','Standard'],
default: 'Standard'
},
url_source: String,
themes: {
type: String,
enum: ['Economics', 'Politics', 'Bitcoins', 'Sports'],
default: 'Economics'
},
type_media: {
type: String,
enum: ['video', 'web', 'podcast'],
default: 'web'
},
article_date: Date,
});
//export our module to use in server.js
module.exports = mongoose.model('Article', ArticleSchema);
In my schema Node.js/GraphQL I have the resolve function
createArticle: {
type: ArticleType,
args: {
event: {type: EventCreateType},
body: {type: GraphQLString},
type: {type: articleType},
url_source: {type: GraphQLString},
themes: {type: themesType},
//type_media: {type: new GraphQLList(mediaType)}
type_media: {type: mediaType},
article_date : {type: GraphQLString}
},
resolve: async (source, params) => {
if (params.event) {
var eventparams = params.event;
var tagparams = params.event.tags;
params.event.tags = null;
params.event = null;
var tagIds = [];
//traitement des tags
var inEvent = await EventsModel.findOne({'name':eventparams.name});
if(!inEvent){
inEvent = new EventsModel(eventparams);
if(tagparams){
if(tagparams.length !=0){
tagIds = await tagparams.map(async function(c) {
var inTag = await TagsModel.findOne(c);
if(!inTag){
inTag = new TagsModel(c);
inTag.save(function(err) {
if (err) {
console.log(err);
}});
}
return inTag;
});
console.log('******************************Le tableau**************************');
console.dir(tagIds);
console.log('********************************************************');
//inEvent.tags = tagIds;
Promise.all(tagIds).then(function(savedObjects) {
console.log('********************************************************');
console.log('Le Inside Tab:',savedObjects);
console.log('********************************************************');
// Do something to celebrate?
inEvent.tags = savedObjects;
}).catch(function(err) {
// one or both errored
console.log(err);
});
}
}
inEvent.save(function(err) {
if (err) {
console.log(err);
}});
}
console.log('*******************propriete inEvent*****************************');
console.dir(inEvent);
console.log('********************************************************');
var articleModel = new ArticlesModel(params);
articleModel.event = inEvent;
console.log('***********************propriete Article before save****************');
console.dir(articleModel);
console.log('********************************************************');
articleModel.save(function(err, article) {
if (err) {
console.log(err);
}
if (article) {
return ArticlesModel.findById(article._id)
.populate('article')
.populate('event')
.exec(function(error, articles) {
console.log('article saved: succes')
articles.article.articles.push(articles);
articles.article.save(function(err, article) {
if (err) {
console.log(err);
}
});
return articles;
})
}
});
return articleModel;
}
else{
console.log('verif 3');
}
console.log('verif 4');
}
},

How to implement partial document embedding in Mongoose?

I have a simple relation between topics and categories when topic belongs to a category.
So schema looks like this:
const CategorySchema = new mongoose.Schema({
name: String,
slug: String,
description: String
});
And topic
const TopicSchema = new mongoose.Schema({
category: {
type: mongoose.Schema.Types.ObjectId,
ref: 'Category'
},
title: String,
slug: String,
body: String,
created: {type: Date, default: Date.now}
});
I want to implement particular embedding of category into topic
{
category: {
_id: ObjectId('abc'),
slug: 'catslug'
},
title: "Title",
slug: "topictitle",
...
}
It will help me avoid unnecessary population and obtain performance bonuses.
I don't want to embed whole document because I want to changes categories sometimes (it is a rare operation) and maintain references.
Hope this helps, done it in my own project to save some RTTs in common use cases. Make sure you're taking care of both copies on update.
parent.model.js:
const mongoose = require('mongoose');
const childEmbeddedSchema = new mongoose.Schema({
_id: {type: mongoose.Schema.Types.ObjectId, ref: 'Child', auto: false, required: true, index: true},
someFieldIWantEmbedded: {type: String}
});
const parentSchema = new mongoose.Schema({
child: { type: childEmbeddedSchema },
moreChildren: { type: [{type: childEmbeddedSchema }] }
});
module.exports = mongoose.model('Parent', parentSchema);
child.model.js:
const mongoose = require('mongoose');
const childSchema = new mongoose.Schema({
someFieldIWantEmbedded: {type: String},
someFieldIDontWantEmbedded: {type: Number},
anotherFieldIDontWantEmbedded: {type: Date}
});
module.exports = mongoose.model('Child', childSchema);
parent.controller.js:
const mongoose = require('mongoose');
const Parent = require('path/to/parent.model');
exports.getAll = (req, res, next) => {
const query = Parent.find();
// only populate if requested! if true, will replace entire sub-document with fetched one.
if (req.headers.populate === 'true') {
query.populate({
path: 'child._id',
select: `someFieldIWantEmbedded ${req.headers.select}`
});
query.populate({
path: 'moreChildren._id',
select: `someFieldIWantEmbedded ${req.headers.select}`
});
}
query.exec((err, results) => {
if (err) {
next(err);
} else {
res.status(200).json(results);
}
});
};

Assign _id or Object to mongoose.Schema.Types.ObjectId

I have the schema which refers to another object.
model/message.js
var moogoose = require('mongoose');
var Schema = moogoose.Schema;
var schema = new Schema({
.
user: {type: Schema.Types.ObjectId, ref: 'User'}
.
});
module.exports = moogoose.model('Message', schema);
Suppose user is the object extracted from database.
Should I assign user._id or user object to user property of message model. I have tried both, they yield same result that is _id as value of user property.
var message = new Message({
user: user._id,
});
or:
var message = new Message({
user: user
});
Here is one of the schema that I use.
var mongoose=require('mongoose');
var config=require('./config'); //configuarations and APIS keys stored in this file
mongoose.connect(config.MONGO_URL, {server:{poolSize:10}});
var Schema = mongoose.Schema, ObjectId = Schema.ObjectId;
var users = new mongoose.Schema({
mobile: { type: String, unique: true },
hash: String,
location:{ type:[Number, Number], index:'2d'},
locName: String,
created_at: Number,
image: {type: String, default: 'NONE'},
role: {type: String, default: 'USER'},
name: {type: String, default: 'NONE'},
});
exports.users = mongoose.model('users', users);
var comments = new mongoose.Schema({
uid: { ObjectId, ref: 'users'},
liked_by: {type: [ObjectId], default: []},
like_count: {type: Number, default: 0},
created_at: Number
});
comments.index({spark_id:1, created_at:1});
exports.comments = mongoose.model('comments', comments);
This is one of the functions
dbase.comments.find({uid: req.body.user._id, function(err, response){
if(!err){
//do what you want
}else{
//handle error
}
}).sort({created_at: -1}).skip(count).limit(50).lean();

How to populate data for sub document id which is used in other collection

I am using following collection :
var collection1 = mongoose.Schema({
name: { type: String},
subdoc1: [subdoc1],
subdoc2: [subdoc2]
};
module.exports = mongoose.model('collection1', collection1);
var subdoc1 = mongoose.Schema({
name: { type: String},
address : { type: String}
};
module.exports = mongoose.model('subdoc1', subdoc1);
var subdoc2 = mongoose.Schema({
name: { type: String},
address : { type: String}
};
module.exports = mongoose.model('subdoc2', subdoc2);
var collcetion2 = mongoose.Schema({
name: { type: String},
subdoc1_id: {
type: Schema.ObjectId,
ref: 'subdoc1'
},
subdoc2_id: {
type: Schema.ObjectId,
ref: 'subdoc2'
},
};
module.exports = mongoose.model('collcetion2', collcetion2);
Here i am using a collection 1 which contains sub documents (subdoc1 and subdoc2) and i am using another collection named as collcetion2.
Now i want to fetch data for subdoc1 and subdoc2 into collcetion2.
How can i do this?
Currently i am using following code but that does not work:
Collection2.find({})
.select('name subdoc1_id subdoc2_id')
.populate('subdoc1_id', 'name address')
.populate('subdoc2_id', 'name address')
.exec(function(err, result) {
console.log(result);
});
but it is not giving data (name and address) for subdoc1 and subdoc2.

Resources