【问题标题】:Mongodb querying find subdodument and queryMongodb查询查找子目录和查询
【发布时间】:2018-02-15 20:04:15
【问题描述】:

我有一个作业架构和一个应用程序架构。应用程序架构已嵌入作业架构中。这就像申请此特定工作的候选人

{
        "_id" : ObjectId("5a745a49e4a4d9203cf506ed"),
        "active" : true,
        "applications" : [ 
            {
                "applied" : true,
                "shortlisted" : true,
                "interviewed" : false,
                "offered" : false,
                "hired" : false,
                "rejected" : false,
                "candId":1
            }, 
            {
                "applied" : true,
                "shortlisted" : false,
                "interviewed" : false,
                "offered" : false,
                "hired" : false,
                "rejected" : false,
                "candId":2
            }, 
            {
                "applied" : true,
                "shortlisted" : false,
                "interviewed" : false,
                "offered" : false,
                "hired" : false,
                "rejected" : false,
                "candId":3
            }, 
            {
                "applied" : true,
                "shortlisted" : false,
                "interviewed" : false,
                "offered" : false,
                "hired" : false,
                "rejected" : false,
                "candId":4
            }
        ],
        "job_title" : "MERN Stack developer",
        "job_description" : "<p>If more than one component needs to make use of this, we have to either duplicate the function, or extract it into a shared helper and import it in multiple places - both are less than ideal.</p><p>Vuex allows us to define \"getters\" in the store. You can think of them as computed properties for stores. Like computed properties, a getter's result is cached based on its dependencies, and will only re-evaluate when some of its dependencies have changed.</p><p>Getters will receive the state as their 1st argument:</p>",
        "max_experience" : 3,
        "max_salary" : 4,
        "min_experience" : 2,
        "min_salary" : 3
    }

在我的申请中,我有候选人经历的阶段(申请、入围、面试、提供...)

所以我想写一个查询,它会在应用阶段给我一个结果

           {
                "applied" : true,
                "shortlisted" : false,
                "interviewed" : false,
                "offered" : false,
                "hired" : false,
                "rejected" : false,
                "candId":2
            }, 
            {
                "applied" : true,
                "shortlisted" : false,
                "interviewed" : false,
                "offered" : false,
                "hired" : false,
                "rejected" : false,
                "candId":3
            }, 
            {
                "applied" : true,
                "shortlisted" : false,
                "interviewed" : false,
                "offered" : false,
                "hired" : false,
                "rejected" : false,
                "candId":4

            }

基本上只有applyed应该设置为true,其他设置为false

并在入围阶段

           {
                "applied" : true,
                "shortlisted" : true,
                "interviewed" : false,
                "offered" : false,
                "hired" : false,
                "rejected" : false,
                "candId":1
            } 

有工作或无工作的查询结果无所谓

编辑

Job.find({
    _id : req.params.id,
    'applications.applied': req.body.applied,
    'applications.shortlisted' : req.body.shortlisted,
    'applications.interviewed' : req.body.interviewed,
    'applications.offered' : req.body.offered,
    'applications.hired' : req.body.hired,
    'applications.rejected' : req.body.rejected
  }).populate('applications.talentId').then((job, err) => {
    if(err){
      return res.status(500).send({error: "There was some error"})
    }
    res.status(200).json(job)
  })

我正在获取每个阶段的所有应用程序的整个作业对象

【问题讨论】:

    标签: mongodb mongoose mongodb-query


    【解决方案1】:

    您可以使用以下聚合

    db.jobs.aggregate([
        { $match: { _id : ObjectId("5a745a49e4a4d9203cf506ed") } },
        { $unwind: "$applications" },
        {
            $replaceRoot: {
                newRoot: "$applications"
            }
        },
        {
            $match: {
                applied: true
            }
        }
    ])
    

    基本上要过滤嵌套集合,您可以首先使用$unwindapplications 数组的每个元素创建单独的文档。然后,您可以使用$replaceRoot 重塑它们以最终只有单个应用程序,然后您可以使用 $match 过滤它们,就像它们是单个文档一样。您可以根据自己的业务需求为 $match 添加更多条件。

    编辑:

    • $uwind 获取 n 文档的嵌套数组并返回 n 文档,每个文档都包含该嵌套数组的一个元素。
    • $replaceRoot 只是对您的每个文档进行整形,以便嵌套的子文档成为新的根(此处用于去除不必要的属性)

    【讨论】:

    • 嗨@mickl,我是 mongodb 的新手,你能告诉我这是什么放松,replaceRoot 做什么
    • 当然,编辑了我的答案,也请查看指向 mongodb 文档的链接以查看示例
    • @hi mickl ,我无法解决这个问题,我必须先通过 id 找到工作,然后通过应用程序 $unwind 然后 replaceRoot ,最后匹配应用程序,shrortlist .... 对吗?
    • @waq true,这应该是你需要的
    • 它成功了,我遇到了 objectId 问题,我使用的是 mongoose 模式,我从来没有指定 ObjectId,因为 mongoose 默认会这样做,但是在使用聚合时我必须指定,谢谢
    【解决方案2】:

    对于applied:检查applied 是否为真,shortlisted 是否为假。

    对于shortlisted:检查shortlisted 是否为真,interviewed 是否为假。

    等等……

    您检查该阶段是否为true,下一个阶段是否为false

    例如,对于处于applied 阶段的人,查询将是:

    Job.find({"_id" : ObjectId("5a745a49e4a4d9203cf506ed"),
              applications: {$elemMatch: {applied: true, shortlisted: false}}}, 
             {applications: 1})
    

    要查看已申请的阶段候选人,您必须使用聚合:

    Job.aggregate([{$match: {_id: ObjectId("5a745a49e4a4d9203cf506ed") /* first match the job Id */ }},
                   {$unwind: '$applications' /* expand the array */}, 
                   {$match: {'applications.applied': true, 'applications.shortlisted': false}}])
    

    【讨论】:

    • 你能看看我的问题吗?我试过这种方法,但没用
    • 我添加了一个示例查询。试试看。
    • 此查询适用于哪个阶段或入围名单?
    • 那是applied。我写的第一个查询将帮助您更新文档,当然您必须使用update 而不是find。第二个aggregate 查询仅供阅读。
    【解决方案3】:
    db.Job.aggregate(
    
        // Pipeline
        [
            // Stage 1
            {
                $unwind: {
                    path: "$applications",
                    preserveNullAndEmptyArrays: true // optional
                }
            },
    
            // Stage 2
            {
                $match: {
                    'applications.applied': true,
                    'applications.shortlisted': false,
                    'applications.interviewed': false,
                    'applications.offered': false,
                    'applications.hired': false,
                    'applications.hired': false,
                    'applications.rejected': false
                }
            },
    
        ]
    
    
    
    );
    

    【讨论】:

      猜你喜欢
      • 2015-06-24
      • 2021-07-19
      • 2016-08-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多