in Education by
Here is my data: { { "_id" : ObjectId("623a92139e19f99295167786"), "headline" : "headline1", "comments" : [ {"userID" : ObjectId("623a7b199e19f99295167776"), "time" : ISODate("2022-03-24T10:20:23Z")}, {"userID" : ObjectId("623a7b199e19f99295167776"), "time" : ISODate("2022-03-25T10:20:23Z")}, ] }, { "_id" : ObjectId("623be3ce9e19f99295167787"), "headline" : "headline2", "comments" : [ ] } } The inner array comments may or may not contain some elements. I want to find an object with the _id that matches the string variable my_id, with its inner array comments sorted in the reverse order of time. I currently have: col.aggregate([ {$match: {_id: monk.id(my_id)}}, {$unwind: "$comments"}, {$sort: {"comments.time":-1}}, {$group: {_id: "$_id", headline: {$first:"$headline"}, comments:{$push:"$comments"}}} ]).then((result) => { console.log(JSON.stringify(result)); }); This works fine when the comments array has some elements but returns [] for the entire object if the array is empty. May I get some help on how to return the content even when the array is empty? JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
Use preserveNullAndEmptyArrays for $unwind stage. preserveNullAndEmptyArrays If true, if the path is null, missing, or an empty array, $unwind outputs the document. { $unwind: { path: "$comments", preserveNullAndEmptyArrays: true } } Sample Mongo Playground

Related questions

0 votes
    I'm trying to make a form-maker with Node.Js and MongoDB but confused about the data structure. ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 21, 2022 in Education by JackTerrance
0 votes
    What I'm trying to do here is use the 2 functions but I don't know how to use both of them ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 7, 2022 in Education by JackTerrance
0 votes
    What I'm trying to do here is use the 2 functions but I don't know how to use both of them ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 4, 2022 in Education by JackTerrance
0 votes
    I'm getting unexpected results for getting the collections between two columns. I have this simple collection: ... for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 2, 2022 in Education by JackTerrance
0 votes
    I'm getting unexpected results for getting the collections between two columns. I have this simple collection: ... for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 2, 2022 in Education by JackTerrance
0 votes
    I have a Post model that has a likes array that contains the ObjectId of the user that liked the ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 2, 2022 in Education by JackTerrance
0 votes
    I'm trying to debug my nodejs app using node-inspector. But Google Chrome doesn't show the code. ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 11, 2022 in Education by JackTerrance
0 votes
    I have been looking at using the ssh2 module for sftp shipping of logs. However the cloud service hosting ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 18, 2022 in Education by JackTerrance
0 votes
    i'm trying to test with jest this route on my node microservice. At the initalisation of my microservice ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 26, 2022 in Education by JackTerrance
0 votes
    Does server-side rendering with react-loadable work with Webpack 4 and Babel 7? I've been unable to ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 17, 2022 in Education by JackTerrance
0 votes
    I've been putzing with trying to call the jobs.Insert bigquery rest api endpoint with node (the jobs. ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 16, 2022 in Education by JackTerrance
0 votes
    Hi I'm using ffmpeg to concatenate several mp4 files that I downloaded. Usually they don't have the ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 16, 2022 in Education by JackTerrance
0 votes
    so i'm reviewing and practicing making rest api with node mongoose and express. I'm having problem making ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 14, 2022 in Education by JackTerrance
0 votes
    Step 1: server I've created a simple server with Node & Socket.io which declares a namespace under ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 3, 2022 in Education by JackTerrance
0 votes
    I'm trying to use node to print the Serial from Arduino uno connected to USB I have a file script ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 30, 2022 in Education by JackTerrance
...