【问题标题】:Searching in Nested MongoDB Document using Express, Mongoose使用 Express、Mongoose 在嵌套的 MongoDB 文档中搜索
【发布时间】:2020-09-03 11:49:01
【问题描述】:

我想搜索邮递员发送的具有 stepPath = url 的 _id。 下面是嵌套的文档结构

[
{
    "_id": "5ebf028e989a2e2628d9d3f6",
    "funnelName": "Mortgage Leads1",
    "group": "Mortgage",
    "category": "Finance",
    "funnelStep": [
        {
            "viewsStorage": [
                {
                    "_id": "5ebf028e989a2e2628d9d3f8",
                    "viewsCounter": {
                        "currentDate": "2000-12-31T18:30:00.000Z",
                        "viewsNo": 0
                    }
                }
            ],
            "_id": "5ebf028e989a2e2628d9d3f7",
            "stepType": "Advertorial",
            "stepName": "Angle Ver 1",
            "stepPath": "google12.com",
            "isTracking": true
        }
    ],
    "__v": 0
},
{
    "_id": "5ec15febb8a83019bc966932",
    "funnelName": "Mortgage Leads",
    "group": "Mortgage",
    "category": "Finance",
    "funnelStep": [
        {
            "viewsStorage": [
                0,
                0
            ],
            "_id": "5ec15febb8a83019bc966933",
            "stepType": "Advertorial",
            "stepName": "Angle Ver 1",
            "stepPath": "google.com",
            "isTracking": false
        },
        {
            "viewsStorage": [
                1,
                0
            ],
            "_id": "5ec15febb8a83019bc966934",
            "stepType": "Optin",
            "stepName": "21 Day Plan",
            "stepPath": "fb.com",
            "isTracking": true
        },
        {
            "viewsStorage": [
                2,
                0
            ],
            "_id": "5ec15febb8a83019bc966935",
            "stepType": "Checkout",
            "stepName": "Checkout Ver 1",
            "stepPath": "google.com",
            "isTracking": true
        }
    ],
    "__v": 0
}]

我正在使用的结构模型 -

const mongoose = require('mongoose')
const funnel = new mongoose.Schema({
funnelName:{
    type:String,
    unique:true
},
group: String,
category: String,
funnelStep: [{
    stepType: String,
    stepName: String,
    stepPath: String,
    isTracking: Boolean,
    viewsStorage: []
}] })
mongoose.model('Funnel',funnel)

邮递员发送的 JSON -

{
"funnelName":"Mortgage",
"url" : "google12.com",
"user_id" : "BATR0001",
"Date": 1 }

在邮递员中收到响应 -

[]

获取我正在使用的方法 -

router.get('/',function(req,res){
Funnel.find({"Funnel.funnelStep.stepPath":req.body.url},function(err,funnels){
    if(err){
        console.log(err)
    }else{
        console.log(req.body.url)
        console.log(funnels)

        return res.json(funnels) 
    }
}) })

除此之外,我也尝试过关注 -

Funnel.find({stepPath:req.body.url},function(err,funnels){.....}
Funnel.find({stepPath:req.params.url},function(err,funnels){....}

以上都不起作用,只返回 NULL 或空数组。

有人可以帮忙解决这个问题吗?

【问题讨论】:

    标签: mongodb express search mongoose nested


    【解决方案1】:

    find 方法中,将Funnel.funnelStep.stepPath 替换为funnelStep.stepPath

    Funnel.find({ "funnelStep.stepPath": req.body.url }, function (err, funnels) { 
    
    })
    

    【讨论】:

      猜你喜欢
      • 2011-05-24
      • 2020-10-04
      • 1970-01-01
      • 2012-10-17
      • 1970-01-01
      • 2011-09-06
      • 2017-04-01
      • 1970-01-01
      • 2011-11-10
      相关资源
      最近更新 更多