0

This is my posting document in MongoDB:

{
    "_id": {
    "$oid": "5b4e60ab24210138f5746402"
},
    "type": [
    "full",
    "temp"
],
    "applications": [
    {
        "_id": {
            "$oid": "5b52113d1123631744fa9f39"
        },
        "applicationDate": {
            "date": 20,
            "day": 5,
            "hours": 18,
            "minutes": 43,
            "month": 6,
            "seconds": 41,
            "time": 1532105021753,
            "timezoneOffset": -120,
            "year": 2018
        },
        "userId": {
            "$oid": "5b51fb6f9686430cee31a0d9"
        },
        "resume": {
            "fieldname": "resume",
            "originalname": "resume_acc.pdf",
            "encoding": "7bit",
            "mimetype": "application/pdf",
            "destination": "./public/resumes/",
            "filename": "765d650b9014cc3ddadb801d10d495a5",
            "path": "public/resumes/765d650b9014cc3ddadb801d10d495a5",
            "size": 8
        },
        "coverLetter": {
            "fieldname": "docs",
            "originalname": "cover letter.pdf",
            "encoding": "7bit",
            "mimetype": "application/pdf",
            "destination": "./public/resumes/",
            "filename": "e5892869b24f3fc5e72d3e057b4dd61d",
            "path": "public/resumes/e5892869b24f3fc5e72d3e057b4dd61d",
            "size": 5
        }
    }
],
    "creatorId": {
    "$oid": "5b4b95cc16778c325010a55d"
},
    "title": "Developer",
    "salary": "50/h",
    "timeLine": "One year",
    "description": "You'll be building apps",
    "duties": "Building apps",
    "experience": "2 years",
    "province": "ON",
    "visible": true,
    "__v": 0
}

Postings is an array of posting, which look like the above document. applications is an array which is in every posting. I want to search all postings.applications to see get all postings the user applied to. For now I tried to do it like this:

var Posting = require('../models/posting');
var postings = await Posting
    .find({'visible': true});
console.log('posts', postings);
var applications = await Posting
    .find({'visible': true})
    .where(postings
        .map(posting => posting.applications
            .map(application => application.userId.equals(req.user._id)))
    );

But obviously this failed.

I tried this as well:

var postings = await Posting
    .find({'visible': true, 'applications[$].userId': req.user._id});

or

var postings = await Posting
    .find({'visible': true, 'applications.$.userId': req.user._id});

But no luck. They both return an empty array.

Posting model:

var mongoose = require('mongoose');

jobPostingSchema = mongoose.Schema({
    "creatorId": mongoose.Schema.Types.ObjectId, //ObjectID('aaaa'), // ID of the User or Account
    "eventId": {'type': mongoose.Schema.Types.ObjectId, 'default': undefined},
    "title": String,
    "type": [], //"Full", // What this means? I did not understand.
    "salary": String,
    "timeLine": String, // What this means? I did not understand.
    "description": String,
    "duties": String,
    "experience": String,
    "province": String, // Employer will post job postings based on the province and region
    // Applications means, how many people applied for this job post?
    "applications": [
        // {
        //     ObjectID: cccc,
        //     userId: dddd,
        //     Resume: {},
        //     coverLetter: String,
        // },
    ],
    "visible": Boolean,
});

module.exports = mongoose.model('posting', jobPostingSchema);

So how can I get all applications where userId equals req.user._id?

1
  • As I said at the end "... get all applications where userId equals req.user._id" Commented Jul 23, 2018 at 7:09

2 Answers 2

1

Maybe this works as a solution ( sourcing from the SO link shared by @DSCH here ):

Posting.find({
    'applications': {
        $elemMatch: { userId: req.user._id }
    },
    'visible:': true
});

If you wish to seek clarification on how it works, you may refer to the link here

Sign up to request clarification or add additional context in comments.

1 Comment

visible should be outside of application
1
Posting.find({
    'visibile:': true,
    'applications': {
    $elemMatch: { userId: req.user._id }
    }
});

$elemMatch is the mongo operator that you probably need. Hope that one helps better.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.