Mongoose / mongoDb search where I need values of unpopulated property
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
So I'm doing a search with mongodb / mongoose. I have a post document and this post document has a comments property which consists of comment IDS. The goal of this search is to gather all the user's posts where a comment was added today.
I can't find any posts because I am searching for comments.created_date and this property doesn't exist yet since I only have comment IDS in the array of the comments property.
How can I 'populate' this array so that I can find the posts that had comments added?
Code: first we generate the Date object of the start of this day. Then we search for the posts. Here, I am talking about the first $or case: comments.created_date is not working because this property consists of comment IDs. How can I get the user's posts that had comments posted today?
// Generate the actual time
const todayForEvent = moment().startOf('day')
.utc().toDate();
const posts = await Post.find({
$or: [{
// From this user...
$and: [
// Find normal posts that has comments (recent interactions)
{ _posted_by: userId },
{ comments: { $exists: true, $ne: } },
{ 'comments.created_date': { $gte: todayForEvent } }
]
}, {
$and: [
// Find tasks due to today
{ 'task._assigned_to': userId },
{ 'task.due_to': { $in: [today, tomorrow] } }
]
}, {
$and: [
// Find events due to today
{ 'event._assigned_to': userId },
{ 'event.due_to': { $gte: todayForEvent, $lt: todayPlus48ForEvent } }
]
}]
})
.sort('event.due_to task.due_to -comments.created_date')
.populate('_posted_by', 'first_name last_name profile_pic')
.populate('task._assigned_to', 'first_name last_name')
.populate('event._assigned_to', 'first_name last_name')
.populate('_group', 'group_name group_avatar')
.populate('_liked_by', 'first_name last_name');
console.log('POSTS', posts);
post model
const mongoose = require('mongoose');
const { Schema } = mongoose;
const PostSchema = new Schema({
content: {
type: String,
trim: true
},
_content_mentions: [{
type: Schema.Types.ObjectId,
ref: 'User'
}],
type: {
type: String,
required: true,
enum: ['normal', 'event', 'task']
},
_liked_by: [{
type: Schema.Types.ObjectId,
ref: 'User'
}],
comments_count: {
type: Number,
default: 0
},
comments: [{
type: Schema.Types.ObjectId,
ref: 'Comment'
}],
_group: {
type: Schema.Types.ObjectId,
ref: 'Group',
required: true
},
_posted_by: {
type: Schema.Types.ObjectId,
ref: 'User',
required: true
},
task: {
due_to: {
type: String,
default: null
},
_assigned_to: {
type: Schema.Types.ObjectId,
ref: 'User'
},
status: {
type: String,
enum: ['to do', 'in progress', 'done']
}
},
event: {
due_to: {
type: Date,
default: null
},
_assigned_to: [{
type: Schema.Types.ObjectId,
ref: 'User'
}]
},
created_date: {
type: Date,
default: Date.now
},
files: [{
orignal_name: {
type: String,
default: null
},
modified_name: {
type: String,
default: null
}
}]
});
const Post = mongoose.model('Post', PostSchema);
module.exports = Post;
Comment model
const mongoose = require('mongoose');
const moment = require('moment');
const { Schema } = mongoose;
const CommentSchema = new Schema({
content: {
type: String,
trim: true
},
_content_mentions: [{
type: Schema.Types.ObjectId,
ref: 'User'
}],
created_date: {
type: Date,
default: moment().utc().toDate()
},
_commented_by: {
type: Schema.Types.ObjectId,
ref: 'User',
required: true
},
_post: {
type: Schema.Types.ObjectId,
ref: 'Post',
required: true
}
});
const Comment = mongoose.model('Comment', CommentSchema);
module.exports = Comment;
javascript node.js mongodb mongoose aggregation-framework
add a comment |
So I'm doing a search with mongodb / mongoose. I have a post document and this post document has a comments property which consists of comment IDS. The goal of this search is to gather all the user's posts where a comment was added today.
I can't find any posts because I am searching for comments.created_date and this property doesn't exist yet since I only have comment IDS in the array of the comments property.
How can I 'populate' this array so that I can find the posts that had comments added?
Code: first we generate the Date object of the start of this day. Then we search for the posts. Here, I am talking about the first $or case: comments.created_date is not working because this property consists of comment IDs. How can I get the user's posts that had comments posted today?
// Generate the actual time
const todayForEvent = moment().startOf('day')
.utc().toDate();
const posts = await Post.find({
$or: [{
// From this user...
$and: [
// Find normal posts that has comments (recent interactions)
{ _posted_by: userId },
{ comments: { $exists: true, $ne: } },
{ 'comments.created_date': { $gte: todayForEvent } }
]
}, {
$and: [
// Find tasks due to today
{ 'task._assigned_to': userId },
{ 'task.due_to': { $in: [today, tomorrow] } }
]
}, {
$and: [
// Find events due to today
{ 'event._assigned_to': userId },
{ 'event.due_to': { $gte: todayForEvent, $lt: todayPlus48ForEvent } }
]
}]
})
.sort('event.due_to task.due_to -comments.created_date')
.populate('_posted_by', 'first_name last_name profile_pic')
.populate('task._assigned_to', 'first_name last_name')
.populate('event._assigned_to', 'first_name last_name')
.populate('_group', 'group_name group_avatar')
.populate('_liked_by', 'first_name last_name');
console.log('POSTS', posts);
post model
const mongoose = require('mongoose');
const { Schema } = mongoose;
const PostSchema = new Schema({
content: {
type: String,
trim: true
},
_content_mentions: [{
type: Schema.Types.ObjectId,
ref: 'User'
}],
type: {
type: String,
required: true,
enum: ['normal', 'event', 'task']
},
_liked_by: [{
type: Schema.Types.ObjectId,
ref: 'User'
}],
comments_count: {
type: Number,
default: 0
},
comments: [{
type: Schema.Types.ObjectId,
ref: 'Comment'
}],
_group: {
type: Schema.Types.ObjectId,
ref: 'Group',
required: true
},
_posted_by: {
type: Schema.Types.ObjectId,
ref: 'User',
required: true
},
task: {
due_to: {
type: String,
default: null
},
_assigned_to: {
type: Schema.Types.ObjectId,
ref: 'User'
},
status: {
type: String,
enum: ['to do', 'in progress', 'done']
}
},
event: {
due_to: {
type: Date,
default: null
},
_assigned_to: [{
type: Schema.Types.ObjectId,
ref: 'User'
}]
},
created_date: {
type: Date,
default: Date.now
},
files: [{
orignal_name: {
type: String,
default: null
},
modified_name: {
type: String,
default: null
}
}]
});
const Post = mongoose.model('Post', PostSchema);
module.exports = Post;
Comment model
const mongoose = require('mongoose');
const moment = require('moment');
const { Schema } = mongoose;
const CommentSchema = new Schema({
content: {
type: String,
trim: true
},
_content_mentions: [{
type: Schema.Types.ObjectId,
ref: 'User'
}],
created_date: {
type: Date,
default: moment().utc().toDate()
},
_commented_by: {
type: Schema.Types.ObjectId,
ref: 'User',
required: true
},
_post: {
type: Schema.Types.ObjectId,
ref: 'Post',
required: true
}
});
const Comment = mongoose.model('Comment', CommentSchema);
module.exports = Comment;
javascript node.js mongodb mongoose aggregation-framework
add a comment |
So I'm doing a search with mongodb / mongoose. I have a post document and this post document has a comments property which consists of comment IDS. The goal of this search is to gather all the user's posts where a comment was added today.
I can't find any posts because I am searching for comments.created_date and this property doesn't exist yet since I only have comment IDS in the array of the comments property.
How can I 'populate' this array so that I can find the posts that had comments added?
Code: first we generate the Date object of the start of this day. Then we search for the posts. Here, I am talking about the first $or case: comments.created_date is not working because this property consists of comment IDs. How can I get the user's posts that had comments posted today?
// Generate the actual time
const todayForEvent = moment().startOf('day')
.utc().toDate();
const posts = await Post.find({
$or: [{
// From this user...
$and: [
// Find normal posts that has comments (recent interactions)
{ _posted_by: userId },
{ comments: { $exists: true, $ne: } },
{ 'comments.created_date': { $gte: todayForEvent } }
]
}, {
$and: [
// Find tasks due to today
{ 'task._assigned_to': userId },
{ 'task.due_to': { $in: [today, tomorrow] } }
]
}, {
$and: [
// Find events due to today
{ 'event._assigned_to': userId },
{ 'event.due_to': { $gte: todayForEvent, $lt: todayPlus48ForEvent } }
]
}]
})
.sort('event.due_to task.due_to -comments.created_date')
.populate('_posted_by', 'first_name last_name profile_pic')
.populate('task._assigned_to', 'first_name last_name')
.populate('event._assigned_to', 'first_name last_name')
.populate('_group', 'group_name group_avatar')
.populate('_liked_by', 'first_name last_name');
console.log('POSTS', posts);
post model
const mongoose = require('mongoose');
const { Schema } = mongoose;
const PostSchema = new Schema({
content: {
type: String,
trim: true
},
_content_mentions: [{
type: Schema.Types.ObjectId,
ref: 'User'
}],
type: {
type: String,
required: true,
enum: ['normal', 'event', 'task']
},
_liked_by: [{
type: Schema.Types.ObjectId,
ref: 'User'
}],
comments_count: {
type: Number,
default: 0
},
comments: [{
type: Schema.Types.ObjectId,
ref: 'Comment'
}],
_group: {
type: Schema.Types.ObjectId,
ref: 'Group',
required: true
},
_posted_by: {
type: Schema.Types.ObjectId,
ref: 'User',
required: true
},
task: {
due_to: {
type: String,
default: null
},
_assigned_to: {
type: Schema.Types.ObjectId,
ref: 'User'
},
status: {
type: String,
enum: ['to do', 'in progress', 'done']
}
},
event: {
due_to: {
type: Date,
default: null
},
_assigned_to: [{
type: Schema.Types.ObjectId,
ref: 'User'
}]
},
created_date: {
type: Date,
default: Date.now
},
files: [{
orignal_name: {
type: String,
default: null
},
modified_name: {
type: String,
default: null
}
}]
});
const Post = mongoose.model('Post', PostSchema);
module.exports = Post;
Comment model
const mongoose = require('mongoose');
const moment = require('moment');
const { Schema } = mongoose;
const CommentSchema = new Schema({
content: {
type: String,
trim: true
},
_content_mentions: [{
type: Schema.Types.ObjectId,
ref: 'User'
}],
created_date: {
type: Date,
default: moment().utc().toDate()
},
_commented_by: {
type: Schema.Types.ObjectId,
ref: 'User',
required: true
},
_post: {
type: Schema.Types.ObjectId,
ref: 'Post',
required: true
}
});
const Comment = mongoose.model('Comment', CommentSchema);
module.exports = Comment;
javascript node.js mongodb mongoose aggregation-framework
So I'm doing a search with mongodb / mongoose. I have a post document and this post document has a comments property which consists of comment IDS. The goal of this search is to gather all the user's posts where a comment was added today.
I can't find any posts because I am searching for comments.created_date and this property doesn't exist yet since I only have comment IDS in the array of the comments property.
How can I 'populate' this array so that I can find the posts that had comments added?
Code: first we generate the Date object of the start of this day. Then we search for the posts. Here, I am talking about the first $or case: comments.created_date is not working because this property consists of comment IDs. How can I get the user's posts that had comments posted today?
// Generate the actual time
const todayForEvent = moment().startOf('day')
.utc().toDate();
const posts = await Post.find({
$or: [{
// From this user...
$and: [
// Find normal posts that has comments (recent interactions)
{ _posted_by: userId },
{ comments: { $exists: true, $ne: } },
{ 'comments.created_date': { $gte: todayForEvent } }
]
}, {
$and: [
// Find tasks due to today
{ 'task._assigned_to': userId },
{ 'task.due_to': { $in: [today, tomorrow] } }
]
}, {
$and: [
// Find events due to today
{ 'event._assigned_to': userId },
{ 'event.due_to': { $gte: todayForEvent, $lt: todayPlus48ForEvent } }
]
}]
})
.sort('event.due_to task.due_to -comments.created_date')
.populate('_posted_by', 'first_name last_name profile_pic')
.populate('task._assigned_to', 'first_name last_name')
.populate('event._assigned_to', 'first_name last_name')
.populate('_group', 'group_name group_avatar')
.populate('_liked_by', 'first_name last_name');
console.log('POSTS', posts);
post model
const mongoose = require('mongoose');
const { Schema } = mongoose;
const PostSchema = new Schema({
content: {
type: String,
trim: true
},
_content_mentions: [{
type: Schema.Types.ObjectId,
ref: 'User'
}],
type: {
type: String,
required: true,
enum: ['normal', 'event', 'task']
},
_liked_by: [{
type: Schema.Types.ObjectId,
ref: 'User'
}],
comments_count: {
type: Number,
default: 0
},
comments: [{
type: Schema.Types.ObjectId,
ref: 'Comment'
}],
_group: {
type: Schema.Types.ObjectId,
ref: 'Group',
required: true
},
_posted_by: {
type: Schema.Types.ObjectId,
ref: 'User',
required: true
},
task: {
due_to: {
type: String,
default: null
},
_assigned_to: {
type: Schema.Types.ObjectId,
ref: 'User'
},
status: {
type: String,
enum: ['to do', 'in progress', 'done']
}
},
event: {
due_to: {
type: Date,
default: null
},
_assigned_to: [{
type: Schema.Types.ObjectId,
ref: 'User'
}]
},
created_date: {
type: Date,
default: Date.now
},
files: [{
orignal_name: {
type: String,
default: null
},
modified_name: {
type: String,
default: null
}
}]
});
const Post = mongoose.model('Post', PostSchema);
module.exports = Post;
Comment model
const mongoose = require('mongoose');
const moment = require('moment');
const { Schema } = mongoose;
const CommentSchema = new Schema({
content: {
type: String,
trim: true
},
_content_mentions: [{
type: Schema.Types.ObjectId,
ref: 'User'
}],
created_date: {
type: Date,
default: moment().utc().toDate()
},
_commented_by: {
type: Schema.Types.ObjectId,
ref: 'User',
required: true
},
_post: {
type: Schema.Types.ObjectId,
ref: 'Post',
required: true
}
});
const Comment = mongoose.model('Comment', CommentSchema);
module.exports = Comment;
javascript node.js mongodb mongoose aggregation-framework
javascript node.js mongodb mongoose aggregation-framework
edited Jan 3 at 13:59


Anthony Winzlet
18.2k42346
18.2k42346
asked Jan 3 at 3:10
tillytilly
399315
399315
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
Since you have to filter your documents with the comments created_date
field then you must start your aggregation with the comments collection instead of post and join the posts
with the $lookup
aggregation
const todayForEvent = moment().startOf('day').utc().toDate()
db.comments.aggregate([
{ "$match": { "created_date": { "$gte": todayForEvent } }},
{ "$lookup": {
"from": "posts",
"localField": "_id",
"foreignField": "comments",
"as": "posts"
}},
{ "$unwind": "$posts" },
{ "$replaceRoot": { "newRoot": "$posts" }}
])
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54015831%2fmongoose-mongodb-search-where-i-need-values-of-unpopulated-property%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Since you have to filter your documents with the comments created_date
field then you must start your aggregation with the comments collection instead of post and join the posts
with the $lookup
aggregation
const todayForEvent = moment().startOf('day').utc().toDate()
db.comments.aggregate([
{ "$match": { "created_date": { "$gte": todayForEvent } }},
{ "$lookup": {
"from": "posts",
"localField": "_id",
"foreignField": "comments",
"as": "posts"
}},
{ "$unwind": "$posts" },
{ "$replaceRoot": { "newRoot": "$posts" }}
])
add a comment |
Since you have to filter your documents with the comments created_date
field then you must start your aggregation with the comments collection instead of post and join the posts
with the $lookup
aggregation
const todayForEvent = moment().startOf('day').utc().toDate()
db.comments.aggregate([
{ "$match": { "created_date": { "$gte": todayForEvent } }},
{ "$lookup": {
"from": "posts",
"localField": "_id",
"foreignField": "comments",
"as": "posts"
}},
{ "$unwind": "$posts" },
{ "$replaceRoot": { "newRoot": "$posts" }}
])
add a comment |
Since you have to filter your documents with the comments created_date
field then you must start your aggregation with the comments collection instead of post and join the posts
with the $lookup
aggregation
const todayForEvent = moment().startOf('day').utc().toDate()
db.comments.aggregate([
{ "$match": { "created_date": { "$gte": todayForEvent } }},
{ "$lookup": {
"from": "posts",
"localField": "_id",
"foreignField": "comments",
"as": "posts"
}},
{ "$unwind": "$posts" },
{ "$replaceRoot": { "newRoot": "$posts" }}
])
Since you have to filter your documents with the comments created_date
field then you must start your aggregation with the comments collection instead of post and join the posts
with the $lookup
aggregation
const todayForEvent = moment().startOf('day').utc().toDate()
db.comments.aggregate([
{ "$match": { "created_date": { "$gte": todayForEvent } }},
{ "$lookup": {
"from": "posts",
"localField": "_id",
"foreignField": "comments",
"as": "posts"
}},
{ "$unwind": "$posts" },
{ "$replaceRoot": { "newRoot": "$posts" }}
])
answered Jan 3 at 5:39


Anthony WinzletAnthony Winzlet
18.2k42346
18.2k42346
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54015831%2fmongoose-mongodb-search-where-i-need-values-of-unpopulated-property%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown