Show documents from Array with MongoDB - node.js

I would like to "create" new documents, to keep only the contents of the activities.
I've looked at $project and $unwide, but I didn't find any appropriate result.
Here is my MongoDB query:
const activities = await friendModel.aggregate([
{
$match: {
$or: [
{
ReceiverId: ActorId,
Status: 1,
},
{
RequesterId: ActorId,
Status: 1,
},
],
},
},
{
$set: {
fieldResult: {
$cond: {
if: {
$eq: [ "$ReceiverId", ActorId ],
},
then: "$RequesterId",
else: "$ReceiverId",
},
},
},
},
{
$lookup: {
from: "activities",
localField: "fieldResult",
foreignField: "ActorId",
as: "activity",
},
},
{
$unwind: {
path: "$activity",
preserveNullAndEmptyArrays: true
}
},
{ $sort: { "activity._Date": -1 } },
{ $skip: request.pageindex * 3 },
{ $limit: 3 },
]);
Here is what I would like to do: https://sourceb.in/7u0tirIstt
Here is what I get for the moment: https://sourceb.in/Ped2xY5Ml8
Thank you in advance for your help!

You should be able to add $project as follows:
...
{
$project: {
"_id":"$_id",
"ActivityId": "$activity._id",
"ActorId": "$activity.ActorId",
"Type": "$activity.Type",
"_Date": "$activity._Date",
"MovieId": "$activity.MovieId",
"FriendId": "$activity.FriendId",
"ContestId": "$activity.ContestId",
"LookId": "$activity.LookId",
"__v": "$__v"
},
},
...
You can access values with $, and map nested values by accessing them directly
Also would recommend you doing a limit before you do lookup to speed it up a bit

Related

Mongoose - How to get unique data based on some fields using aggregation

I have these fields in the document,
doc: {
"id": "632ac8cba7723378033fef10",
"question": 1,
"text": "aasdfghjk,mnbvcxswertyuikmnbvcxsrtyuiknbvcdrtyujnbvcddtyjnbvfty",
"slug": "xcvbnrddfghjktdxjjydcvbyrsxcvbhytrsxggvbjkytrdgc",
"subject": 25866,
"tutorInfo": {
"tutorId": "632ac8cba7723378033fa0fe",
"tutorIncrementalId": 95947
}
}
the same tutorInfo can Occur in multiple documents.
const allQuestionBySubject = await QuestionParts.aggregate([
{
$match: {
$and: [
{
subject: subjectIncrementalId
},
{tutorInfo: {$exists: true}}
]
}
},
{ "$skip": page * limit },
{ "$limit": limit },
{
$lookup: {
from: "profiles",
localField: "tutorInfo.tutorIncrementalId",
foreignField: "incrementalId",
as: "tutorDetails"
}
}
])
Code to get a list of questions as per subject.
I am filtering documents based on subject and as I mentioned the same tutorInfo can be present in multiple documents so in the result same tutor can be present in multiple documents, How can I get a unique list of documents in which tutorInfo shouldn't be repeated.
Since the same tutorInfo is present in multiple records, You can use $group to group the document on the tutorInfo.tutorId field.
const allQuestionBySubject = await QuestionParts.aggregate(
[
{
$match: {
$and: [
{
subject: subjectIncrementalId
},
{ tutorInfo: { $exists: true } }
]
}
},
{ "$skip": page * limit },
{ "$limit": limit },
{
"$group": {
_id: "$tutorInfo.tutorId",
question: { $first: "$question" },
text: { $first: "$text" },
slug: { $first: "$slug" },
subject: { $first: "$orderId" },
tutorInfo: { $first: "$tutorInfo" },
}
},
{
$lookup: {
from: "profiles",
localField: "tutorInfo.tutorIncrementalId",
foreignField: "incrementalId",
as: "tutorDetails"
}
}
]
)

retrieve nested data from mongodb record depend on condition

I have a board record like this
{
_id:"6257d2edfbc4d4d1d53bb7b8"
tasks:["624acd4fce5fbb5ce20ddb88","624acd68ce5fbb5ce20de3b5"]
}
This is my tasks collection
[
{
_id:"624acd4fce5fbb5ce20ddb88",
status:"inProgress"
},
{
_id:"624acd68ce5fbb5ce20de3b5"
status:"done"
}
]
I am trying to retrieve the board data and the count of inProgress tasks and done tasks in aggregate like this but it show me this error
input to $filter must be an array not object
Board.aggregate([
{
$facet: {
totalDocs: [{ $match: { $and: [data] } }],
totalInProgress: [
{
$lookup: {
from: "tasks",
localField: "tasks",
foreignField: "_id",
as: "tasks",
},
},
{ $unwind: { path: "$tasks", preserveNullAndEmptyArrays: true } },
{
$project: {
tasks: {
$filter: {
input: "$tasks",
as: "task",
cond: { $eq: ["$$task.status", "inProgress"] },
},
},
},
},
],
},
},
]);

Group $lookup results within inner nested array, and keep outer nested array and its contents intact in MongoDB

I have a collection which comprises of three level array nesting as shown below
_id: ObjectID('abc'),
sections: [
{
sectionId: "sec0",
sectionName: "ABC",
contents: [
{
contentId: 0,
tasks: [
{
taskId: ObjectID('task1')
}
//May contain 1-100 tasks
],
contentDescription: "Content is etc",
}
]
}
]
Sections is an array of objects which contains an object each with sectionId, and contents array which is an array of objects comprising of contentId, contentDescription, and nested array of tasks which comprises of an object containing a taskId.
I am applying $lookup operator in order to join nested tasks array with tasks collection but I am facing a problem in document duplication as shown below.
_id: ObjectID('abc'),
sections: [
{
sectionId: "sec0",
sectionName: "ABC",
contents: [
{
contentId: 0,
tasks: [
{
//Task Document of ID 1
}
],
contentDescription: "Content is etc",
}
]
}
]
_id: ObjectID('abc'),
sections: [
{
sectionId: "sec0",
sectionName: "ABC",
contents: [
{
contentId: 0,
tasks: [
{
//Task Document of ID 2
}
],
contentDescription: "Content is etc",
}
]
}
]
Whereas the desired output is as follows
_id: ObjectID('abc'),
sections: [
{
sectionId: "sec0",
sectionName: "ABC",
contents: [
{
contentId: 0,
tasks: [
{
//Task Document of ID 1
},
{
//Task Document of ID 2
},
{
//Task Document of ID 3
}
],
contentDescription: "Content is etc",
}
]
}
]
In the collection, a sections array might contain multiple section object which might contain multiple contents and so on and so forth.
The schema in question is temporary as our company is currently migrating from an existing database to MongoDB, so architectural refactoring is not possible atm and I need to work with existing schema design from different database.
I tried the following way
const contents= await sections.aggregate([
{
$match: { _id: id},
},
{ $unwind: '$sections' },
{
$unwind: {
path: '$sections.contents',
preserveNullAndEmptyArrays: true,
},
},
{
$unwind: {
path: '$sections.contents.tasks',
preserveNullAndEmptyArrays: true,
},
},
{
$lookup: {
from: 'tasks',
let: { task_id: '$sections.contents.tasks.taskId' },
pipeline: [
{ $match: { $expr: { $eq: ['$_id', '$$task_id'] } } },
],
as: 'sections.contents.tasks',
},
},
{
$addFields: {
'sections.contents.tasks': {
$arrayElemAt: ['$sections.contents.tasks', 0],
},
},
},
{
$group: {
_id: '$_id',
exam: { $push: '$sections.contents.tasks' },
},
},
]);
And I am also unable to use $group aggregation operator like
$group: {
_id: '$_id',
sections: {
sectionId : { $first: '$sectionId' },
sectionName: { $first: '$sectionName' },
contents: {
contentId: { $first: '$contentId' },
task: { $push: $sections.contents.tasks }
}
},
},
Any help or directions will be appreciated, I also searched on SO, and found this but couldn't understand the following part
{"$group":{
"_id":{"_id":"$_id","mission_id":"$missions._id"},
"agent":{"$first":"$agent"},
"title":{"$first":"$missions.title"},
"clients":{"$push":"$missions.clients"}
}},
{"$group":{
"_id":"$_id._id",
"missions":{
"$push":{
"_id":"$_id.mission_id",
"title":"$title",
"clients":"$clients"
}
}
}}
So you're very close to the final solution, a good "rule" that's good to remember is if you unwind x times you need to group x to restore the original structure properly, like so:
db.collection.aggregate([
{
$match: {
_id: id
},
},
{
$unwind: "$sections"
},
{
$unwind: {
path: "$sections.contents",
preserveNullAndEmptyArrays: true,
},
},
{
$unwind: {
path: "$sections.contents.tasks",
preserveNullAndEmptyArrays: true,
},
},
{
$lookup: {
from: "tasks",
let: {
task_id: "$sections.contents.tasks.taskId"
},
pipeline: [
{
$match: {
$expr: {
$eq: [
"$_id",
"$$task_id"
]
}
}
},
],
as: "sections.contents.tasks",
},
},
{
$addFields: {
"sections.contents.tasks": {
$arrayElemAt: [
"$sections.contents.tasks",
0
],
},
},
},
{
$group: {
_id: {
contentId: "$sections.contents.contentId",
sectionId: "$sections.sectionId",
sectionName: "$sections.sectionName",
originalId: "$_id"
},
tasks: {
$push: "$sections.contents.tasks"
},
contentDescription: {
$first: "$sections.contents.contentDescription"
},
}
},
{
$group: {
_id: {
sectionId: "$_id.sectionId",
sectionName: "$_id.sectionName",
originalId: "$_id.originalId"
},
contents: {
$push: {
contentId: "$_id.contentId",
tasks: "$tasks",
contentDescription: "$contentDescription"
}
}
}
},
{
$group: {
_id: "$_id.originalId",
sections: {
$push: {
sectionId: "$_id.sectionId",
sectionName: "$_id.sectionName",
contents: "$contents"
}
}
}
}
])
Mongo Playground
However your pipeline could be made a little cleaner as it has 1 redundant $unwind stage that also adds a redundant $group stage. I won't post the entire fixed pipeline here as it's already a long post but feel free to check it out here: Mongo Playground fixed

search from child collection contains the keyword from parent and child collection

I am facing a problem with getting data from database using parent child relationship collections.
Here is my collection structure --
-post
---post cloth : brand id from brands collections
-----brand
Now I am getting data from post and post cloth with keyword search from post cloth and brand table if any key matches from post cloth and brand. Till post cloth it is working fine along with keyword search in or condition, Now I also need to search from brand and return the result if keyword contains in brand as well.
here are my cases --
data returned : if any of post_cloths keys matches the keyword searched
data returned : if any of the post_cloths keys matches the keyword OR lookup with brand name matches the keyword
data returned : if all keys from post_cloths not matches the keyword but lookup with brand name matches the keyword
data not returned : if no keys from post_cloths matches the keyword and also lookup with brand name not matches the keyword
Here is my code :
var page = 0;
if (req.query.page >= 0) {
page = req.query.page;
}
let filter = { 'totalCloth': { $gte: 1 } };
if (req.query.user != null && req.query.user != '') {
filter.createdBy = ObjectID(req.query.user);
}
console.log(filter);
var searchQuery = [];
var brandSearchQuery = [];
if (req.query.keyword != null && req.query.keyword != '') {
console.log(req.query.keyword);
keyword = req.query.keyword;
searchQuery = [
{
$regexFind: {
input: '$category',
regex: new RegExp(keyword),
options: 'i',
},
},
{
$regexFind: {
input: '$color',
regex: new RegExp(keyword),
options: 'i',
},
},
{
$regexFind: {
input: '$country',
regex: new RegExp(keyword),
options: 'i',
},
},
{
$regexFind: {
input: '$size',
regex: new RegExp(keyword),
options: 'i',
},
},
{
$regexFind: {
input: '$clothMaterial',
regex: new RegExp(keyword),
options: 'i',
},
},
];
brandSearchQuery = [
{
$regexFind: {
input: '$name',
regex: new RegExp(keyword),
options: 'i',
},
},
];
} else {
searchQuery = [{}];
brandSearchQuery = [{}];
}
// get the post details
// PostModel.find(filter).countDocuments().then(countPosts => {
PostModel.aggregate([
{
$lookup: {
from: 'post_cloths',
let: { postId: '$_id' },
pipeline: [
//lookup for brand
{
$lookup: {
from: 'brands',
let: { brandId: '$brandId' },
pipeline: [
{
$match: {
$expr:
{
$and:
[
{ $eq: ['$_id', '$$brandId'] },
{ $or: brandSearchQuery },
],
},
},
},
],
as: 'brand',
},
},
//end of brand lookup
{
$match: {
$expr: {
$and:
[
{ $eq: ['$postId', '$$postId'] },
{
$or: searchQuery,
},
],
},
},
},
{
$project: {
totalBrands: { $size: '$brand' },
},
},
{
$match: {
$expr:
{ $or: [{ $match: { totalBrands: { $gte: 1 } } }] },
},
},
],
as: 'postCloth',
},
},
{
$project: {
image: 1,
createdAt: 1,
createdBy: 1,
mediaUrl: {
$concat: [process.env.PROJECT_URL + '/files/', '$image'],
},
totalCloth: { $size: '$postCloth' },
},
},
//check for post cloth object if length is greater than equals to 1
{
$match: filter,
},
{ $skip: 12 * page },
{ $limit: 12 },
{ $sort: { createdAt: -1 } },
]).exec(function(err, post) {
return apiResponse.successResponseWithData(res, 'Successful', post);
});
I am getting data properly, but not while searching from brand. Please suggest how we can search the data from the cases given. there is simple keyword search.
Thanks in advance
The problem is with you're main's $lookup's pipeline:
first you start with the brand $lookup, which i'll assume works ( if you provide schema's for your collections it would be easy to verify), however right after that $lookup you do this:
{
$match: {
$expr:
{
$and:
[
{ $eq: ['$postId', '$$postId'] },
{
$or: searchQuery,
},
],
},
},
},
This means if the searchQuery fails even if a brand exists the document will be filtered out, you should change it to:
{
$match: {
$expr:
{
$and:
[
{ $eq: ['$postId', '$$postId'] },
{
$or: [
{
$or: searchQuery
},
{
$gt: [{$size: "$brand"}, 0]
}
],
},
],
},
},
},
Now this will also matched documents that have any brands in the brand field, meaning the brand matched the nested $lookup, you can then drop the next 2 stages that check for the brand size.
I also recommend that you move the $eq for the postId to the start of the $lookup, this will improve performance immensely, after all the changes the entire pipeline would look like:
PostModel.aggregate([
{
$lookup: {
from: 'post_cloths',
let: { postId: '$_id' },
pipeline: [
{
$match: { $eq: ['$postId', '$$postId'] },
},
{
$lookup: {
from: 'brands',
let: { brandId: '$brandId' },
pipeline: [
{
$match: {
$expr:
{
$and:
[
{ $eq: ['$_id', '$$brandId'] },
{ $or: brandSearchQuery },
],
},
},
},
],
as: 'brand',
},
},
{
$match: {
$expr: {
$and:
[
{
$or: [
{
$or: searchQuery,
},
{
$gt: [{ $size: '$brand' }, 0],
},
],
},
],
},
},
},
],
as: 'postCloth',
},
},
{
$project: {
image: 1,
createdAt: 1,
createdBy: 1,
mediaUrl: {
$concat: [process.env.PROJECT_URL + '/files/', '$image'],
},
totalCloth: { $size: '$postCloth' },
},
},
{
$match: filter,
},
{ $skip: 12 * page },
{ $limit: 12 },
{ $sort: { createdAt: -1 } },
])

I want to display only one product image

This is Code in node js
const result = await OrderDB.aggregate([
{ $match: { _id: mongoose.Types.ObjectId(id) } },
{
$lookup: {
from: 'products',
localField: 'product',
foreignField: '_id',
as: 'productDetail',
},
},
{
$project: {
productDetail: {
name: 1,
price: 1,
productImage: 1,
},
},
},
])
This is the response of code
{
"message": "Get Order Successfully",
"result": [
{
"_id": "5ff47348db5f5917f81871aa",
"productDetail": [
{
"name": "Camera",
"productImage": [
{
"_id": "5fe9b8a26720f728b814e246",
"img": "uploads\\product\\7Rq1v-app-7.jpg"
},
{
"_id": "5fe9b8a26720f728b814e247",
"img": "uploads\\product\\FRuVb-app-8.jpg"
}
],
"price": 550
}
]
}
]
}
I want to display only one productImage from the response using nodejs and mongoose
This is using in aggregate projection
I was use $arrayElemAt but it is don't work
I also use $first but it is don't work
so projection method I use to display only one *productImage*
Data looks like multi level nested.
You have array of results, each result contains array of productDetails
play
You need to unwind the data to get the first productImage
db.collection.aggregate([
{
"$unwind": "$result"
},
{
"$unwind": "$result.productDetail"
},
{
$project: {
pImage: {
"$first": "$result.productDetail.productImage"
}
}
}
])
With the above response
db.collection.aggregate([
{
"$project": {
productDetails: {
$map: {
input: "$productDetail",
in: {
"$mergeObjects": [
"$$this",
{
productImage: {
"$arrayElemAt": [
"$$this.productImage",
0
]
}
}
]
}
}
}
}
}
])
Working Mongo playground

Resources