查询在Mongoshell中工作,但在nodejs中不起作用



嗨,以下是我面临的问题的描述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()

我在蒙戈谢尔得到的回应

{
"_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:节点JS控制器

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:经销商Id}3"nodejs路由器文件

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

邮递员身体和网址

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

邮递员回应:

{
"order": []
}

我也尝试过 just{ _id:dealerId}现在 mongodb 数据库包含多个集合,我已经运行了其他 API,所以连接的数据库是正确的,必须存在一些其他问题,即此查询在 nodejs 中不起作用,或者更确切地说,它返回一个空数组作为 order:[] 但查询在 shell 中工作"猫鼬":"5.7.4"和MongoDB版本是4.2

需要检查 nodejs 控制器文件

`const mongoose = require('mongoose ')` 

在顶部dealerId 没有转换为对象 ID,因为它丢失了,添加后 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": []
}
]

}

最新更新