in Education by
I have a requirement to return the same length of the array provided to mongo. usernames = ['user1', 'user2', 'user3'] Now lets say user3 doesn't exist, is there a way to return null when mongo doesn't find the document ? If i'm using the $in operator i only get the matched documents ex. User.find({ username: { $in: usernames }}) result = [user1Document, user2Document] I can loop over the array and query the database for every username but it's not efficient. const userPromises = usernames.map(username => User.findOne({ username })); return Promise.all(userPromises); 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
You can start with initial filtering using $in and then to map all the values from input array into final result you have to $group them $push-ing $$ROOT (enitre document), then you can just merge input usernames with docs using $map and $filter operators: db.col.aggregate([ { $match: { username: { $in: ['user1', 'user2', 'user3'] } } }, { $group: { _id: null, docs: { $push: "$$ROOT" } } }, { $project: { _id: 0, results: { $map: { input: ['user1', 'user2', 'user3'], as: 'username', in: { $let: { vars: { filtered: { $filter: { input: "$docs", as: "doc", cond: { $eq: [ "$$doc.username", "$$username" ] } } } }, in: { $arrayElemAt: [ "$$filtered", 0 ] } } } } } } } ]) Mongo playground

Related questions

0 votes
    I have two collection vmdetails and rsc_installtion_status , i want to select the documents from rsc_installtion_status ... Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 2, 2022 in Education by JackTerrance
0 votes
    Input: {_id:ObjectID(123),ip:'123.123.123.123'} {_id:ObjectID(123),ip:'123.123.123.123'} ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 7, 2022 in Education by JackTerrance
0 votes
    Input: {_id:ObjectID(123),ip:'123.123.123.123'} {_id:ObjectID(123),ip:'123.123.123.123'} ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 6, 2022 in Education by JackTerrance
0 votes
    Input: {_id:ObjectID(123),ip:'123.123.123.123'} {_id:ObjectID(123),ip:'123.123.123.123'} ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 26, 2022 in Education by JackTerrance
0 votes
    Following query is perfectly working in mongo shell. db.collection.find({ "_id" : UUID("87aa9ed6-8485-4517 ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 21, 2022 in Education by JackTerrance
0 votes
    I tried mongo replica sets for the first time. I am using ubuntu on ec2 and I booted up three instances ... replicate across machines? Select the correct answer from above options...
asked Feb 1, 2022 in Education by JackTerrance
0 votes
    I want to query something with SQL's like query: SELECT * FROM users WHERE name LIKE '%m%' How to do I ... like in the documentation. Select the correct answer from above options...
asked Jan 26, 2022 in Education by JackTerrance
0 votes
    ANSI-standard SQL allows the use of special operators in conjunction with the WHERE clause. A special ... Schemes topic in chapter Concurrency Control of Database Management...
asked Oct 10, 2021 in Education by JackTerrance
0 votes
    I have an graphql/apollo-server/graphql-yoga endpoint. This endpoint exposes data returned from a database ( ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 7, 2022 in Education by JackTerrance
0 votes
    When I have started with the project using Laravel 5.7 and PHP 7.3, $info->id returned correctly the last insert id. Now, I did ... :1 [ 0 => "*" ] } This is my GfTableModel.php:...
asked Apr 29, 2022 in Education by JackTerrance
0 votes
    When I have started with the project using Laravel 5.7 and PHP 7.3, $info->id returned correctly the last insert id. Now, I did ... :1 [ 0 => "*" ] } This is my GfTableModel.php:...
asked Apr 23, 2022 in Education by JackTerrance
0 votes
    I have an integer field person_id with the default value of NULL. I am using a stored procedure and ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 25, 2022 in Education by JackTerrance
0 votes
    What is the output of the below program? #include int main() { printf("%u",sizeof(NULL)); return 0; }...
asked Jan 23, 2021 in Technology by JackTerrance
+1 vote
    What it the return value of a relational operator if it returns any in C Programming?...
asked Nov 9, 2020 in Technology by JackTerrance
0 votes
    In one Google sheet workbook, lets say I have Sheet1 with 5 rows as Sheet1 And I have Sheet2 as ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 6, 2022 in Education by JackTerrance
...