【问题标题】:query working in mongoshell but not in nodejs查询在 mongoshell 中工作,但在 nodejs 中不工作
【发布时间】:2020-05-14 15:50:44
【问题描述】:

您好,下面是我面临的问题的描述 mongoShell 查询

db.masters.aggregate([
        {
          $match: {
               _id: ObjectId("5e2554ec3405363bc4bf86c0")
            }
        }, {
            $lookup: {
                from: 'masters',
                localField: 'mappedVendors',
                foreignField: '_id',
                as: 'mappedVendors'
            }
        }, { $unwind: '$mappedVendors'}, { $replaceRoot: { newRoot: "$mappedVendors" } },
        { 
           $lookup:
            {
                from: "orders",
                let: { mappedVendorId: "$_id" },
                pipeline: [
                    {
                        $match: { $expr: { $eq: ["$orderCreatedBy", "$$mappedVendorId"] } }
                    },
                    { $project: { orderCreatedOn: 1, isApproved: 1 } }
                ],
                as: "orders"
            }
        },{
           $lookup:
            {
                from: "payments",
                let: { mappedVendorId: "$_id" },
                pipeline: [
                    {
                        $match: { $expr: { $eq: ["$paymentDoneBy", "$$mappedVendorId"] } }
                    },
                    { $project: { outstanding: 1 } }
                ],
                as: "payments"
            }
        },
        { $project: { name: 1, phoneNo: 1, address: 1, depotCode: 1, orders: 1, payments: 1 } }
    ]).pretty()

我在 mongoshell 中得到响应

{
        "_id" : ObjectId("5e2555643405363bc4bf86c4"),
        "phoneNo" : 9992625541,
        "name" : "vendor4",
        "address" : "4 vendor address 4",
        "depotCode" : "D3139",
        "orders" : [ ],
        "payments" : [
                {
                        "_id" : ObjectId("5dd7aa6c31eb913a4c4a487c"),
                        "outstanding" : 300
                }
        ]
}

    {
            "_id" : ObjectId("5e2555783405363bc4bf86c5"),
            "phoneNo" : 9992625542,
            "name" : "vendor5",
            "address" : "5 vendor address 5",
            "depotCode" : "D3139",
            "orders" : [
                    {
                            "_id" : ObjectId("5e2564323405363bc4bf86c6"),
                            "isApproved" : false,
                            "orderCreatedOn" : ISODate("2020-01-20T08:26:26.812Z")
                    },
                    {
                            "_id" : ObjectId("5e27fd3da42d441fe8a89580"),
                            "isApproved" : false,
                            "orderCreatedOn" : ISODate("2020-01-15T18:30:00Z")
                    }
            ],

shell 中的此查询在 shell 中按预期工作,但是当我在 nodejs 中尝试此操作时,它返回空 []。 下面是我的nodejs文件的描述 1:MongoDB连接字符串

const mongoose = require('mongoose')
mongoose.connect('mongodb://127.0.0.1:27017/#####App', {
    useNewUrlParser: true,
    useCreateIndex: true,
    useFindAndModify:false,
    useUnifiedTopology: true
})

注意: ##### 不是我的代码 2:nodejs 控制器

exports.vendorWiseIndent = async (req, res) => {
const { dealerId } = req.body
try {
    const order = await Master.aggregate([
        {
            $match: {
                _id: mongoose.Types.ObjectId(dealerId)
            }
        }, {
            $lookup: {
                from: "masters",
                localField: "mappedVendors",
                foreignField: "_id",
                as: "mappedVendors"
            },
        },
        { $unwind: "$mappedVendors" }, { $replaceRoot: { newRoot: "$mappedVendors" } },
        {
            $lookup:
            {
                from: "orders",
                let: { mappedVendorId: "$_id" },
                pipeline: [
                    {
                        $match: { $expr: { $eq: ["$orderCreatedBy", "$$mappedVendorId"] } }
                    },
                    { $project: { orderCreatedOn: 1, isApproved: 1 } }
                ],
                as: "orders"
            }
        }, {
            $lookup:
            {
                from: "payments",
                let: { mappedVendorId: "$_id" },
                pipeline: [
                    {
                        $match: { $expr: { $eq: ["$paymentDoneBy", "$$mappedVendorId"] } }
                    },
                    { $project: { outstanding: 1 } }
                ],
                as: "payments"
            }
        },
        { $project: { name: 1, phoneNo: 1, address: 1, depotCode: 1, orders: 1, payments: 1 } }
    ])


    console.log(order)
    return res.status(200).json({
        order
    });
} catch (error) {
    res.send(error);
}}

我也试过只用 {_id:dealerId} 3"nodejs路由文件

router.post("/vendorwiseindent", vendorWiseIndent.vendorWiseIndent);

邮递员正文和网址

POST: http://localhost:5002/vendorwiseindent
{
    "dealerId": "5e2554ec3405363bc4bf86c0"

}

邮递员回复:

{
    "order": []
}

我也试过了{_id:dealerId} 现在 mongodb 数据库包含多个集合,并且我已经运行了其他 API,因此连接的 db 是正确的,必须存在其他问题,即该查询在 nodejs 中不起作用,或者它返回一个空数组作为 order:[] 但是查询在 shell 中工作 "mongoose": "5.7.4" & mongodb版本为4.2

【问题讨论】:

  • 请告诉我猫鼬版本?
  • @Mahesh Bhatnagar its "mongoose": "5.7.4" & mongodb version is 4.2 你 r ryt 这个信息应该在我的问题中我也在我的问题中编辑过
  • 请在查询前console.log(dealerId) 发布您在这里得到的信息
  • 我发现了我忘记的问题 const mongoose = require('mongoose ') 因此dealerId 没有被转换为objectID

标签: node.js mongodb mongoose postman


【解决方案1】:

需要检查nodejs控制器文件添加

`const mongoose = require('mongoose ')` 

在顶部 由于缺少dealerId,因此没有将其转换为objectID,在添加POSTMAN响应后如下所述:

{
"order": [
    {
        "_id": "5e2555643405363bc4bf86c4",
        "phoneNo": 9992625541,
        "name": "vendor4",
        "address": "4 vendor address 4",
        "depotCode": "D3139",
        "orders": [],
        "payments": [
            {
                "_id": "5dd7aa6c31eb913a4c4a487c",
                "outstanding": 300
            }
        ]
    },
    {
        "_id": "5e2555783405363bc4bf86c5",
        "phoneNo": 9992625542,
        "name": "vendor5",
        "address": "5 vendor address 5",
        "depotCode": "D3139",
        "orders": [
            {
                "_id": "5e2564323405363bc4bf86c6",
                "isApproved": false,
                "orderCreatedOn": "2020-01-20T08:26:26.812Z"
            },
            {
                "_id": "5e27fd3da42d441fe8a89580",
                "isApproved": false,
                "orderCreatedOn": "2020-01-15T18:30:00.000Z"
            }
        ],
        "payments": []
    }
]

}

【讨论】:

    猜你喜欢
    • 2015-05-17
    • 2023-04-05
    • 2012-08-12
    • 2018-07-19
    • 2018-06-19
    • 1970-01-01
    • 1970-01-01
    • 2018-04-12
    • 2014-10-04
    相关资源
    最近更新 更多