javascript - 查询 MongoDB 中的嵌套数组

标签 javascript node.js mongodb mongoose

这是我在 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 是一个 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);

那么如何获取 userId 等于 req.user._id 的所有 applications

最佳答案

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

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

如果您想了解其工作原理,可以引用链接here

关于javascript - 查询 MongoDB 中的嵌套数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51463576/

相关文章:

javascript - meteor 黑客 :aggregate gives "MongoError: A pipeline stage specification object must contain exactly one field."

javascript - 如何根据特定条件更新 JSON 对象

javascript - 如何从 Angular 中的 http/async 调用为接口(interface)属性赋值?

node.js - 在 Express 中设置 HTTPS 服务器时遇到问题

javascript - 如何从Node.JS正确执行到Oracle数据库的sql语句?

javascript - 滑入 div 不会重排整个页面

javascript - 使用Express JS部署Elasticsearch

mongodb - Mongo Go 驱动程序 Count() 方法从 Azure CosmosDB MongoDB Api Count() 获取 "Invalid response from server, value field is not a number"

node.js - MongooseError [MongooseServerSelectionError] : connection <monitor> to 52. 6.250.237:27017已关闭

javascript - 使用 Javascript 从客户端使用 MongoDB