在MongoDB中查询嵌套数组

时间:2018-07-22 09:11:04

标签: javascript node.js mongodb mongoose

这是我在MongoDB中的posting文档:

{
    "_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
}

帖子是posting的数组,看起来像上面的文档。 applications是每个posting中的数组。我想搜索所有postings.applications,以查看该用户应用到的所有帖子。现在,我尝试这样做:

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)))
    );

但是显然这失败了。

我也尝试过:

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

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

但是没有运气。它们都返回一个空数组。

发布模型:

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);

那么我怎么能得到applications等于userId的所有req.user._id

2 个答案:

答案 0 :(得分:1)

也许这可以作为一种解决方案(从@DSCH here共享的SO链接中采购):

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

如果您想澄清其工作原理,可以参考链接here

答案 1 :(得分:1)

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

$elemMatch是您可能需要的mongo运算符。 希望能有更好的帮助。