Querying the Nested Documents after Populate - javascript

This is My schema:
var ActivityLog = new Schema({
activity_at: { type: Date, default: Date.now },
activity_by: { type: Schema.Types.ObjectId, ref:"User" },
team_id: { type: Schema.Types.ObjectId, ref:"Team" },
activity_type: String,
activity_value: String,
msg: Object,
});
Populating the activity_by field
ActivityLog.find({}).limit(200).populate('activity_by')
.then(activities => {
res.json({success:true,activities:activities})
})
RESULT
{
"_id": "616c6bace259ac5eafe333eb",
"activity_at": "2021-10-17T18:30:04.929Z",
"time": "2021-10-17T18:30:04.929Z",
"activity_by": {
"_id": "6107e5afce6a4a0b54be97b8",
"profilePicUrl": "https://secure.gravatar.com/avatar/daed28df98c99bdaa640956e643bef3e.jpg?s=512&d=http2F%2Fa.slack-edge.com%2Fdf10d%2Fimg%2Favatars%2Fava_0004-512.png",
"user_token": null,
"name": "akshavanthm",
"email": "akshavanthm#gmail.com",
"token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJlbWFpbCI6ImFrc2hhdmFudGhtQGdtYWlsLmNvbSIsIm5hbWUiOiJha3NoYXZhbnRobSIsIl9pZCI6IjYxMDdlNWFlMDcyZDBmNDk3YmQ0MDhkZCIsImlhdCI6MTYyNzkwNzUwMn0.S7NeE-M523i2kGX7BZo-ztHvFoQ61bvvPnXZ0vbbnKM",
"user_id": "6107e5ae072d0f497bd408dd",
"team_id": "T029FKKGT55",
"user_name": "akshavanthm",
"timezone": "Asia/Kolkata",
"user_slack_id": "U029TAAREF7",
"displayName": "",
"__v": 0,
"welcomeSent": true
},
"team_id": "612ddaa24846e87e0eda9ab6",
"activity_type": "highChart_reports",
"activity_value": "successfully sent High Chart report custom_report_2",
"msg": null,
"__v": 0
}
I am Unable to find query for matching the user_id present inside the activity_by
when i use:
ActivityLog.find({"activity_by.user_id":'6107e5ae072d0f497bd408dd'}).limit(200).populate('activity_by')
.then(activities => {
res.json({success:true,activities:activities})
})
it returns null.How to access the user_id inside the activity_by(which is populated).
I can only access the "_id" of the "activity_by" and not the other fields

referring to this answer on github this is not possible using populate.
you can however use aggregating with $lookup like so:
ActivityLog.aggregate([
{
$lookup: {
from:"User",
localField: "activity_by",
foreignField: "_id",
as: "yourDesiredFiledName"
}
}
])

Related

How can I count all category under productId?

So I'm still new using MongoDB, so what I'm trying to do here is count all category under productId who have same category. So the expected output should be 7. I used populate first but got stuck on how can I use the $count. Instead I use aggregate and then use $lookup, but i only empty array of product
CartSchema.js
const CartSchema = new mongoose.Schema({
productId: {type: mongoose.Schema.Types.ObjectId, ref: 'Product'}
})
export default mongoose.model('Cart', CartSchema)
ProductSchema.js
const ProductSchema = new mongoose.Schema({
category: {type: String, required: true},
})
export default mongoose.model('Product', ProductSchema)
I used this code to show the information under productId.
router.get('/categories', async (req, res) => {
try {
const cart = await Cart.find()
.populate([
{path: 'productId', select: 'category' },
]).exec()
res.status(200).json(cart);
} catch (error) {
res.status(500).json({error: error.message})
}
})
The result of populate method.
[
{
"_id": "63b410fdde61a124ffd95a51",
"productId": {
"_id": "63b410d6de61a124ffd9585b",
"category": "CASE"
},
},
{
"_id": "63b41a679950cb7c5293bf12",
"productId": {
"_id": "63b41637e3957a541eb59e81",
"category": "CASE"
},
},
{
"_id": "63b433ef226742ae6b30b991",
"productId": {
"_id": "63b41637e3957a541eb59e81",
"category": "CASE"
},
},
{
"_id": "63b670dc62b0f91ee4f8fbd9",
"productId": {
"_id": "63b410d6de61a124ffd9585b",
"category": "CASE"
},
},
{
"_id": "63b6710b62b0f91ee4f8fc13",
"productId": {
"_id": "63b410d6de61a124ffd9585b",
"category": "CASE"
},
},
{
"_id": "63b671bc62b0f91ee4f8fc49",
"productId": {
"_id": "63b410d6de61a124ffd9585b",
"category": "CASE"
},
},
{
"_id": "63b6721c62b0f91ee4f8fcc5",
"productId": {
"_id": "63b410d6de61a124ffd9585b",
"category": "CASE"
},
]
So I used this method, but instead, I just get an empty array
router.get('/categories', async (req, res) => {
try {
const cart = await Cart.aggregate([
{
$lookup: {
from: 'product',
localField: 'productId',
foreignField: '_id',
as: 'product'
}
},
{
$unwind: "$product"
},
{
$group: {
_id: "$product.category",
total: {
$sum: 1
}
}
},
{
$sort: {total: -1}
},
{
$project: {
_id: 0,
category: "$_id",
total: 1
}
},
])
res.status(200).json(cart);
} catch (error) {
res.status(500).json({error: error.message})
}
})
In the aggregation, the collection to perform the $lookup on should be products (with an s) rather than product.
The name of the collection that Mongoose creates in your database is the same as the name of your model, except lowercase and pluralized, as documented in the documentation.
Mongoose automatically looks for the plural, lowercased version of your model name. Thus, for the example above, the model Tank is for the tanks collection in the database.
(emphasis theirs)
When using the aggregation framework, your aggregation pipeline is sent to the database as-is. Mongoose doesn't do any sort of coercion or casting on it. So when writing aggregation pipelines you should more or less forget you're using Mongoose. What's important is the name of the underlying collection in Mongo, which is generated from your model name based on the mentioned rule.
You can also override the collection name yourself if desired, for example:
export default mongoose.model('Product', ProductSchema, 'xyz');
This will override Mongoose's default naming behavior and will name the collection xyz.

Mongoose, updated nested array

My question is:
How can I query in the nested arrays?
I want to change value in key "likeUp" which is nested inside object in array "usersWhoLiked". Where "usersWhoLiked" is nested in array "comments"
How Can I do that with mongoose ?
Request that I wrote beneath... do not work, but is very similar to answer given in StackOverflow post: Mongoose update update nested object inside an array
This is my request to db with updateOne:
try {
const response = await Comments.updateOne(
{
productId,
comments: { $elemMatch: { usersWhoLiked: { $elemMatch: { userId } } } },
},
{
$set: { 'comments.$[outer].usersWhoLiked.$[inner].likeUp': likes.up },
},
{
arrayFilters: [{ 'outer._id': commentId }, { 'inner._userId': userId }],
}
).exec();
return res.status(201).json({ response });
} catch (err) {
console.log(err);
return res.send(err);
}
This is the collection, that I am trying to update:
{
"_id": {
"$oid": "6307569d2308b78b378cc802"
},
"productId": "629da4b6634d5d11a859d729",
"comments": [
{
"userId": "62f29c2c324f4778dff443f6",
"userName": "User",
"date": "2022.08.25",
"confirmed": true,
"likes": {
"up": 0,
"down": 0
},
"content": {
"rating": 5,
"description": "Nowy komentarz"
},
"_id": {
"$oid": "630756b22308b78b378cc809"
},
"usersWhoLiked": [
{
"userId": "62f29c2c324f4778dff443f1",
"likeUp": true,
"_id": {
"$oid": "6307572d2308b78b378cc80e"
}
},
{
"userId": "62f29c2c324f4778dff443f2",
"likeUp": true,
"_id": {
"$oid": "6307572d2308b78b378cc80c"
}
}
]
}
],
"__v": 0
}
Mongooes schema for comment collection:
const commentSchema = new Schema({
productId: String,
comments: [
{
userId: String,
userName: String,
date: String,
confirmed: Boolean,
likes: {
up: {
type: Number,
default: 0,
},
down: {
type: Number,
default: 0,
},
},
content: {
rating: Number,
description: String,
},
usersWhoLiked: [{ userId: String, likeUp: Boolean }],
},
],
});
I guess the problem is with your arrayFilters operator, because you are trying to filter by field _userId which does not exist:
arrayFilters: [{ 'outer._id': commentId }, { 'inner._userId': userId }],
I managed to update the likeUp value using the following query:
db.collection.update({
_id: ObjectId("6307569d2308b78b378cc802")
},
{
$set: {
"comments.$[user].usersWhoLiked.$[like].likeUp": false
}
},
{
arrayFilters: [
{
"user._id": ObjectId("630756b22308b78b378cc809")
},
{
"like.userId": "62f29c2c324f4778dff443f1"
}
]
})
Try it on MongoDB playground: https://mongoplayground.net/p/XhQMNBgEdhp

How would I reference my question model in Mongoose and Express

I would like to reference my Question model text which is referenced in the Survey model. I am able to get the ID of the question but I cannot get the QuestionText if I write
SurveyList[count].Questions.QuestionText
This works:
SurveyList[count].Questions._id
Full code for front end:
<!-- All Surveys -->
<% for (let count = 0; count < SurveyList.length; count++) { %>
<tr>
<!-- Display title -->
<td class="text-center text-white"><%= SurveyList[count].Title %></td>
<!-- Display type -->
<td class="text-center text-white"><%= SurveyList[count].Type %></td>
<td class="text-center text-white"><%= SurveyList[count].Questions._id %></td>
<% } %>
My Question Model Schema:
// create a question model
let questionModel = mongoose.Schema(
{
QuestionText: String,
Options: String,
},
{
collection: "questions",
}
);
My Survey model schema:
let surveyModel = mongoose.Schema(
{
Title: String,
Type: [String],
Questions: { type: mongoose.Schema.Types.ObjectId, ref: "questions" },
Answered: { type: Number, default: 0 }, // how many times users answered
DateCreated: { type: Date, default: Date.now }, // date created
Lifetime: { type: Date, default: Date.now }, // Survey expiry
},
{
collection: "surveys",
}
);
Controller:
module.exports.displayLiveSurveys = (req, res, next) => {
Survey.find((err, surveyList) => {
if (err) {
return console.error(err);
} else {
res.render("content/survey/live-surveys", {
title: "Live Surveys",
page: "live-surveys",
username: req.user ? req.user.username : "",
SurveyList: surveyList,
});
}
});
};
If there is a way to reference Question.find inside Survey.find and add QuestionList to res.render that might work too? I tried that with no luck.
Survey Payload:
{
"_id": {
"$oid": "60fd0c7ecd479a846f1f0fe5"
},
"Type": ["TF"],
"Answered": {
"$numberInt": "0"
},
"Title": "hello",
"Questions": {
"$oid": "60fd067d736566143839e3fd"
},
"DateCreated": {
"$date": {
"$numberLong": "1627195005136"
}
},
"Lifetime": {
"$date": {
"$numberLong": "1627195005136"
}
},
"__v": {
"$numberInt": "0"
}
}
Question Payload:
{
"_id": {
"$oid": "60fd0cbacd479a846f1f0fe6"
},
"QuestionText": "test",
"Options": "tester",
"__v": {
"$numberInt": "0"
}
}
Here, you can use $lookup to simply join the documents of two collections as you are using manual referencing.
Query will look like this:
db.survey.aggregate([
{
"$match": {
_id: ObjectId("60fd0c7ecd479a846f1f0fe5")
}
},
{
$lookup: {
from: "question",
localField: "Questions",
foreignField: "_id",
as: "Questions"
}
}
])
Here is the link to playground to test your use case: Mongo Playground
OR
You can use DBRefs where your driver will resolve the reference automatically in that your survey document will look like this
{
"_id": ObjectId("60fd0c7ecd479a846f1f0fe5"),
"Type": ["TF"],
"Answered": 0,
"Title": "hello",
"Questions": {
// this is DBRef
"$ref" : "question",
"$id" : ObjectId("60fd0cbacd479a846f1f0fe6"),
"$db" : "sample"
},
"DateCreated": 1627195005136,
"Lifetime": 1627195005136,
"__v": 0
}
For more details on $lookup and $ref do check the official documentation

Mongoose populate does not populate array

I have struggled with the mongoose.model.populate function for hours now. I have even tried directly copying and pasting several solutions without luck.
I have a User model which is supposed to contain an array of 'Dilemmas' which he/she has created, but I have been unable to populate it.
Here are the models as well as the implementation of populate().
User.js
const mongoose = require("mongoose");
const Schema = mongoose.Schema;
// Create Schema
const UserSchema = new Schema({
username: {
type: String,
required: true
},
email: {
type: String,
required: true
},
password: {
type: String,
required: true
},
date: {
type: Date,
default: Date.now
},
dilemmas: [
{
type: Schema.Types.ObjectId,
ref: "Dilemma"
}
]
});
module.exports = User = mongoose.model("User", UserSchema, "users");
Dilemma.js
const mongoose = require("mongoose");
const slug = require("mongoose-slug-generator");
const Schema = mongoose.Schema;
mongoose.plugin(slug);
// Create Schema
const DilemmaSchema = new Schema({
creator: {
type: mongoose.Schema.Types.ObjectId,
ref: "User"
},
title: {
type: String
},
slug: {
type: String,
slug: "title"
},
red: {
type: String,
required: true
},
blue: {
type: String,
required: true
},
red_votes: {
type: Number,
default: 0,
required: true
},
blue_votes: {
type: Number,
default: 0,
required: true
},
likes: [
{
user: {
type: Schema.Types.ObjectId,
ref: "User"
}
}
],
comments: [
{
user: {
type: Schema.Types.ObjectId,
ref: "User"
},
text: {
type: String,
required: true
},
author: {
type: String
},
avatar: {
type: String
},
date: {
type: Date,
default: Date.now
}
}
],
date: {
type: Date,
default: Date.now
}
});
module.exports = Dilemma = mongoose.model("Dilemma", DilemmaSchema, "dilemmas");
Routes.js
// #route GET api/users/profile
// #desc Gets logged in user's profile
// #access Private
router.get(
"/profile",
passport.authenticate("jwt", { session: false }),
(req, res) => {
User.find({ username: req.user.username })
.populate("dilemmas")
.then(user => {
if (!user) {
errors.nouser = "There is no user";
return res.status(404).json(errors);
}
res.json(user);
})
.catch(err => res.status(400).json(err));
}
);
JSON Response
[
{
"_id": "5b807beef770e7c7e6bf7ce0",
"dilemmas": [],
"username": "Jonas",
"email": "Mohrdevelopment#gmail.com",
"password": "$2a$10$QaqljS9x08YQ9N9EuCBTpO114ZJUFuVxAV80xMzImNi8eW2frPg0C",
"date": "2018-08-24T21:43:10.411Z",
"__v": 0
}
]
JSON Dilemmas response
[
{
"red_votes": 0,
"blue_votes": 0,
"_id": "5b80975f6e47fecba621f295",
"user": "5b807beef770e7c7e6bf7ce0",
"title": "Am i the real author asdsdasd?",
"red": "This is the red dilemma",
"blue": "This is the blue dilemma",
"likes": [],
"comments": [],
"date": "2018-08-24T23:40:15.381Z",
"slug": "am-i-the-real-author-asdsdasd",
"__v": 0
},
{
"red_votes": 0,
"blue_votes": 0,
"_id": "5b808e789bc36bcae8c6c3ad",
"creator": "5b807beef770e7c7e6bf7ce0",
"title": "Am i the real author?",
"red": "This is the red dilemma",
"blue": "This is the blue dilemma",
"likes": [],
"comments": [],
"date": "2018-08-24T23:02:16.565Z",
"slug": "am-i-the-real-author",
"__v": 0
}
]
JSON Users response
{
"_id": {
"$oid": "5b807beef770e7c7e6bf7ce0"
},
"dilemmas": [],
"username": "Jonas",
"email": "Mohrdevelopment#gmail.com",
"password": "$2a$10$QaqljS9x08YQ9N9EuCBTpO114ZJUFuVxAV80xMzImNi8eW2frPg0C",
"date": {
"$date": "2018-08-24T21:43:10.411Z"
},
"__v": 0
}
I just encountered a similar issue myself. Populating a ref worked, but populating an array of refs did not. I was able to get the array populate to work by explicitly specifying the model name in the populate call, e.g.:
User.find({ ... }).populate({
path: 'dilemmas',
model: 'Dilemma',
});
I don't know why this makes a difference, when the name of the referenced model is already specified in the schema.
Have you tried this?
User.find({ username: req.user.username })
.populate("dilemmas")
.exec() // <-- add exec() to perform the search
.then(user => {
...
})
Did you check the documentation here?
https://mongoosejs.com/docs/populate.html#refs-to-children
It shows a similar setup (with Authors and Stories.) It mentions 'pushing' stories to be able to use a find / populate combo.

How collection referencing works in mongoose?

First time i am trying to use refrence in mongoose , so i am trying to understand if i want to save template with user id , Do we need to get createdBy value from client or how it will be inserted into templateSchema. basically i want to save user id _id when user save template.I was able to save template but i did not get any error and createdBy property did not saved to template collection. Any layman explanation to understand refrencing in mongoose and how i can make it work with below code.
user.js
var UserSchema = new mongoose.Schema({
_id: { type: String, required: true, index: {unique: true}},
firstName: String,
lastName: String,
type: String,
groups:[{type: String, ref: 'Group', required: false}]
},
{
toObject: {
virtuals: true
},
toJSON: {
virtuals: true
}
});
template.js
var User = require('../user/user.model.js');
var TemplateSchema = new mongoose.Schema({
_id: { type: String, required: true},
name: String,
id: String,
appliesTo: [],
properties: [],
createdBy: { type: mongoose.Schema.Types.ObjectId, ref: 'User'}
});
templateCtrl.js
var eTemplate = require('./template.model');
var templatesJavaClass = {
"_id": 12586,
"name": "Java Class",
"id": "javaClass",
"appliesTo": [
"bpmn:UserTask"
],
"properties": [{
"label": "Java Package Name",
"type": "String",
"editable": true,
"binding": {
"type": "property",
"name": "camunda:class"
}
}],
"createdBy": "user1"
}
var template = new eTemplate(templatesJavaClass);
template.save(function(error){
console.log("successfully saved template");
if (error){
console.log(error);
}
});
You need to put user _id in createdBy field of template document while creating/saving it.
Also, make sure it is of Type ObjectId and not string. Otherwise you might get Cast Error.
Try this:
var templatesJavaClass = {
"_id": 12586,
"name": "Java Class",
"id": "javaClass",
"appliesTo": [
"bpmn:UserTask"
],
"properties": [{
"label": "Java Package Name",
"type": "String",
"editable": true,
"binding": {
"type": "property",
"name": "camunda:class"
}
}],
//save users _id in createdBy field
"createdBy": user1._id;//assuming user1 is the users document
}
var template = new eTemplate(templatesJavaClass);
template.save(function(error){
console.log("successfully saved template");
if (error){
console.log(error);
}
});

Categories

Resources