Populate new field when query documents - mongodb

I have two schemas (using mongoose) like below:
var PostSchema = new Schema({
name: String,
content: String,
likes: [{
type: String,
ref: 'User'
}],
...
})
var UserSchema = new Schema({
name: String,
pinnedPosts: [{
type: String,
ref: 'Post'
}],
...
})
Now I want to get all posts with two new populated fields: isLiked and isPinned depend on the auth state. If the user hasn't signed in (auth is null) then all these two fields are false. If user has signed in and had liked the post, isLiked is true... Can I do it when I query the post?

Related

How to use populate, within select, in MongoDB?

Suppose I have two schemas on MongoDB:
const personSchema = Schema({
_id: Schema.Types.ObjectId,
name: String,
email: String,
things: [{ type: Schema.Types.ObjectId, ref: 'Thing' }]
});
const thingSchema = Schema({
_id: Schema.Types.ObjectId,
title: String,
fans: [{ type: Schema.Types.ObjectId, ref: 'Person' }]
});
Every time a user logs in, I would like to show the things that they have posted, as well as the fans that are following each of the things. I am able to use populate and select to get to that:
const user = await personModel
.findOne({ _id: req.user._id })
.populate({
path: "things",
select: ["title", "fans"]
}),
However, I am only getting the id of each fan, and not the fan's name and email. I can't quite figure out how to use populate to reference the person collection again.
The outcome I am trying to achieve is that:
the user object would have an array of things
the thing object would have an array of fans
the fan object would have two values - name and email of the fan
You can do nested population with:
const user = await personModel
.findOne({ _id: req.user._id })
.populate({
path: 'things',
select: ['title', 'fans'],
populate: { path: 'fans' },
})
.exec();

Cannot access some properties from Mongoose document

I am doing a query on my database using Mongoose to retrieve all documents in a collection. Currently there is only one document in the collection. It returns the document and looks fine, but I cannot access some of the properties.
Code snippet:
User.find()
.then((response)=>{
console.log(response);
console.log();
console.log(response[0]._id);
console.log(response[0].name);
console.log(response[0].email);
console.log(response[0].zipCode);
console.log(response[0].dateTime);
console.log(response[0].ipAddr);
console.log(response[0].pageVisited);
}).catch((err)=>{console.log(err)});
Result:
[
{
_id: 5f6d4dc312c76000170c5c43,
name: 'Bob',
email: 'bob#mail.com',
zipCode: '12345',
pageVisited: 'p1m2549',
dateTime: 2020-09-25T01:54:11.152Z,
ipAddr: '111.111.111.111',
__v: 0
}
]
5f6d4dc312c76000170c5c43
Bob
bob#mail.com
undefined
undefined
undefined
undefined
What could be causing this bizarre behavior? It really doesn't make any sense that I can access some of the properties but not others.
That could be because these elements not be defined in the Schema
Define Schema as mentioned below
var Schema = mongoose.Schema;
var UserSchema = new Schema({
name: String,
email: String,
zipCode: String,
pageVisited: String,
dateTime: Date,
ipAddr: String,
__v: Number
});
var User = mongoose.model('users', UserSchema );
User.find()
.then((response)=>{
console.log(response);
console.log();
console.log(response[0]._id);
console.log(response[0].name);
console.log(response[0].email);
console.log(response[0].zipCode);
console.log(response[0].dateTime);
console.log(response[0].ipAddr);
console.log(response[0].pageVisited);
console.log(response[0].__v);
}).catch((err)=>{console.log(err)});

Mongoose Populate with Express, not working in production (Heroku)

This is a MERN app, hosted on github, and it works perfectly on localhost. Unfortunately, it does not work on Heroku.
The issue is the API request, it must return an object and populate an array of OIDs (see Department Model). API request is working. I'm getting the data from MLab, but it doesn't populate... instead returns: "surveys":[]
API File
router.get('/department_data/:d_oid', function(req, res) {
Department.findOne({_id: req.params.d_oid}).populate("surveys").exec(function(err,doc){
if(err) throw(err)
res.send(doc)
})
});
Department Model
**Department Model**
var mongoose = require("mongoose");
var Schema = mongoose.Schema;
// Create the survey schema
var departmentSchema = new Schema({
department_name: {
type: String,
trim: true,
required: true
},
surveys: [{
type: Schema.Types.ObjectId,
ref: 'Surveys'
}],
participants: [{
type: String
}],
create_date: {
type: Date,
default: Date.now
},
created_by: {
type: Schema.Types.ObjectId,
ref: 'Created_By'
},
});
departmentSchema.index({ department_name: 1, created_by: 1}, {unique: true});
const Department = mongoose.model('Departments', departmentSchema);
module.exports = Department;
Survey Model
var mongoose = require("mongoose");
var Schema = mongoose.Schema;
// Create the survey schema
var surveySchema = new Schema({
survey_name: {
type: String,
trim: true,
required: true
},
questions: [{
type: Schema.Types.ObjectId,
ref: 'Questions'
}],
created_date: {
type: Date,
default: Date.now
}
});
const Survey = mongoose.model('Surveys', surveySchema);
module.exports = Survey;
Solved.
The problem was in the database: the ref OIDs got scrambled with a previous update, so when it was trying to populate, Mongoose couldn't find any matching OIDs.
Solution: we had to purge and re-seed. When the correct OID references exist, this code works as expected in localhost & Heroku.

how to retrieve other details of a schema based on the reference key

I have a project am working on. I have a comment schema, a likes schema and a blog schema. All three are declared as separate schemas but the likes and comments schema are then nested into the blogs schema as children i.e comments: [CommentSchema]. Now i have a page where when someone clicks on a blog it displays all the comments together with the blog. This the code that gets the blog Blog.findById(id).populate('user', 'username').exec(function(err, blog). Now in the comments array there is a key called commOwner that refers to the objectid from another schema called user just like the blog schema has a reference key too as u can see from the code. I am trying to display the gravatar and username of each person that made a comment based on the reference key commOwner that's in the comments schema but i don't know how to go about it. I want to also be able to populate the usernames and gravatar of those who commented on the blog in that same code where i did it for the blog. Please can someone help me with this. below is the code for all my schemas
'use strict';
/**
* Module dependencies.
*/
var mongoose = require('mongoose'),
Schema = mongoose.Schema;
/*Comment schema*/
var CommentSchema = new Schema({
commOwner: {
type: Schema.ObjectId,
ref: 'User'
},
commbody: {
type: String,
default: '',
trim: true,
//required: 'Comment cannot be blank'
},
updated: {
type: Date,
default: Date.now
}
});
/**
* Likes Schema
*/
var LikeSchema = new Schema({
score : {
type: Number,
default: 0
},
user: {
type: Schema.ObjectId,
ref: 'User'
}
});
/**
* Blog Schema
*/
var BlogSchema = new Schema({
created: {
type: Date,
default: Date.now
},
title: {
type: String,
default: '',
trim: true,
required: 'Title cannot be blank'
},
content: {
type: String,
default: '',
trim: true
//required: 'Content cannot be blank'
},
user: {
type: Schema.ObjectId,
ref: 'User'
},
comments: [CommentSchema],
likes: [LikeSchema]
});
mongoose.model('Blog', BlogSchema);
Blog.findById(id).
populate(
[{path:'user', select:'username'},
{path:'comments.commOwner',select:'username profilepic ...'}])
.exec(function(err, blog){...})
In the select field specify other fields( separated by spaces ) to be populated in commOwner.
Have a look at the docs

How to set a expires for a subdocument in mongoose

This is my schemes:
var authUserScheme = mongoose.Schema({
token: String,
ip: String,
valid: {type: Date, default: Date.now(), expires: '1m' },
}, {_id: false});
var usersSchema = mongoose.Schema({
// OTHER THINGS
auth : [ authUserScheme ],
// other things
});
When i set an 'auth' path, mongodb deletes the entire document, but i want to delete only the auth row when expire date... It is possible?
Sorry for my english, i speak spanish.
You can't use a TTL index to delete a portion of a document on expiry.
However, it looks like your authUserScheme is really more of a session concept than an embedded document.
A better approach would be to use a reference from the authUserScheme to the related user, eg:
var authUserSchema = mongoose.Schema({
token: String,
ip: String,
valid: {type: Date, default: Date.now(), expires: '1m' },
user: { type: Number, ref: 'User' }
});
var userSchema = mongoose.Schema({
name: String,
// Other fields
})
var AuthUser = mongoose.model('AuthUser', authUserSchema);
var User = mongoose.model('User', userSchema);