代码之家  ›  专栏  ›  技术社区  ›  Alex Ironside

在MongoDB中查询嵌套数组

  •  0
  • Alex Ironside  · 技术社区  · 7 年前

    这是我的 posting 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
    }
    

    帖子是一个数组 邮寄 ,这看起来像上面的文档。 applications 是一个数组 邮寄 . 我想搜索所有 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);
    

    所以我怎么才能得到所有 应用 哪里 userId 等于 req.user._id ?

    2 回复  |  直到 7 年前
        1
  •  1
  •   nerdier.js    7 年前

    或许这可以作为一个解决方案(从@DSCH共享的SO链接获得 here ):

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

    如果你想了解它是如何工作的,你可以参考链接 here

        2
  •  1
  •   Alex Ironside    7 年前
    Posting.find({
        'visibile:': true,
        'applications': {
        $elemMatch: { userId: req.user._id }
        }
    });
    

    $elemMatch 是你可能需要的mongo接线员。 希望有人能帮得更好。