【发布时间】:2021-11-26 12:31:20
【问题描述】:
虽然我使用过 MySQL,但我是 MongoDB 新手。
我的用户集合具有以下形状的用户数据 -
我正在创建api in nodejs,它会获取给定用户的全部数据,但应该排除每种数据类型中的transactions 字段。
我的获取数据 api:localhost:5000/api/users/id/:id/data
请帮我写查询。我已经创建了 api 来获取所有用户、创建用户、更新用户,但我只坚持这个。我也尝试过使用聚合,但它会返回包括交易在内的整个数据。我的尝试-
getUserData = async (req, res) => {
const { id } = req.params;
try {
const userData = await UserModel.findById(id).aggregate([
{ $project: { "data.v.transactions": 0 } }
]);
if (!userData ) res.status(400).json({ success: false, message: `No data found` });
res.status(200).send(userData);
} catch (error) {
console.error(error);
res.status(400).send({ status: false, error });
}
}
我在我的 nodejs 应用程序中使用 mongoose 包。
用户集合中的一个用户对象 --
{
"_id": "615d6a2afabf089b94a116e6",
"email": "john@doe.com",
"data": {
"TYPE_1": [
{
"type": "type_1",
"transactions": {
"endDate": "2020-09-17T14:25:33.440Z",
"startDate": "2019-04-11T11:39:57.153Z",
"transaction": [
{
"mode": "A",
"type": "AB",
"amount": "0"
},
{
"mode": "A",
"type": "AB",
"amount": "0"
},
{
"mode": "A",
"type": "AB",
"amount": "0"
},
{
"mode": "A",
"type": "AB",
"amount": "0"
}
]
},
"profile": {
"full_name": "John Doe"
},
"summary": {
"note": "Lorem Ipsum"
}
},
{
"type": "type_1",
"transactions": {
"endDate": "2020-09-17T14:25:33.440Z",
"startDate": "2019-04-11T11:39:57.153Z",
"transaction": [
{
"mode": "A",
"type": "AB",
"amount": "0"
},
{
"mode": "A",
"type": "AB",
"amount": "0"
},
{
"mode": "A",
"type": "AB",
"amount": "0"
},
{
"mode": "A",
"type": "AB",
"amount": "0"
}
]
},
"profile": {
"full_name": "John Doe"
},
"summary": {
"note": "Lorem Ipsum"
}
},
{
"type": "type_1",
"transactions": {
"endDate": "2020-09-17T14:25:33.440Z",
"startDate": "2019-04-11T11:39:57.153Z",
"transaction": [
{
"mode": "A",
"type": "AB",
"amount": "0"
},
{
"mode": "A",
"type": "AB",
"amount": "0"
},
{
"mode": "A",
"type": "AB",
"amount": "0"
},
{
"mode": "A",
"type": "AB",
"amount": "0"
}
]
},
"profile": {
"full_name": "John Doe"
},
"summary": {
"note": "Lorem Ipsum"
}
}
],
"TYPE_2": [
{
"type": "type_2",
"transactions": {
"endDate": "2020-09-17T14:25:33.440Z",
"startDate": "2019-04-11T11:39:57.153Z",
"transaction": [
{
"mode": "A",
"type": "AB",
"amount": "0"
},
{
"mode": "A",
"type": "AB",
"amount": "0"
},
{
"mode": "A",
"type": "AB",
"amount": "0"
}
]
},
"profile": {
"full_name": "John Doe"
},
"summary": {
"note": "Lorem Ipsum"
}
},
{
"type": "type_2",
"transactions": {
"endDate": "2020-09-17T14:25:33.440Z",
"startDate": "2019-04-11T11:39:57.153Z",
"transaction": [
{
"mode": "A",
"type": "AB",
"amount": "0"
}
]
},
"profile": {
"full_name": "John Doe"
},
"summary": {
"note": "Lorem Ipsum"
}
},
{
"type": "type_2",
"transactions": {
"endDate": "2020-09-17T14:25:33.440Z",
"startDate": "2019-04-11T11:39:57.153Z",
"transaction": [
{
"mode": "A",
"type": "AB",
"amount": "0"
},
{
"mode": "A",
"type": "AB",
"amount": "0"
}
]
},
"profile": {
"full_name": "John Doe"
},
"summary": {
"note": "Lorem Ipsum"
}
}
]
},
"createdAt": "2021-10-06T09:19:39.378Z",
"updatedAt": "2021-10-06T09:19:39.378Z",
"__v": 0
}
【问题讨论】:
-
你想要得到什么结果?
-
@TobiasS。没有交易字段的集合中的整个用户对象。
标签: node.js mongodb mongoose mongodb-query aggregation-framework