查询在mongoshell中工作,但不在nodejs中工作 [英] query working in mongoshell but not in nodejs

查看:102
本文介绍了查询在mongoshell中工作,但不在nodejs中工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Hi below is the description of the issue i am facing mongoShell query

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中尝试此查询时,返回的为empty []. 下面是我的nodejs文件的描述 1:Mongodb连接字符串

This query in shell is working as expected in shell but when i am trying this in nodejs its returning empty[]. below is the description of my nodejs file 1: Mongodb Connection string

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

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

NOTE: ##### is not my code 2:nodejs controller

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);

POSTMAN BODY&网址

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

}

邮递员响应:

{
    "order": []
}

我也尝试过使用{_id:DealerId} 现在mongodb数据库包含多个集合,并且我已经在运行其他API,因此连接的数据库是正确的,还存在其他一些问题,该查询在nodejs中不起作用,或者按顺序返回空数组:[]但是该查询在外壳中工作 猫鼬":"5.7.4"和mongodb版本是4.2

I have also tried it with just{ _id: dealerId} now mongodb database contains multiple collections and i have already other API's running so the db which is connected is right,there has to be some other issue that this query is not working in nodejs or rather its returning an empty array as order:[] but the query is working in shell "mongoose": "5.7.4" & mongodb version is 4.2

推荐答案

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

`const mongoose = require('mongoose ')` 

在顶部 添加了POSTMAN响应后,发牌人没有因为缺少它而转换为objectID,如下所述:

at the top dealerId was not getting converted to objectID as it was missing, after its addition POSTMAN response is mentioned below:

{
"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": []
    }
]

}

这篇关于查询在mongoshell中工作,但不在nodejs中工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆