regards, suppose we have the following documents in my collection called inventory
and I'm trying to show only the incoming movements inventory:
[
{
"_id": "638150ee268d890e9983881f",
"movements": {
"entries": [
{
"code": 4,
"created_at": "2022-11-25T23:31:38.523Z",
"created_by": "userId",
"type": "Ingreso",
"observation": "It is a long established fact that a reader will be distracted",
"old_quantity": 0,
"new_quantity": 10,
"total_quantity": 10
},...
]
}
},
{
"_id": "63810f179db70fea1263dea5",
"movements": {
"entries": [
{
"code": 1,
"created_at": "2022-11-25T18:52:54.528Z",
"created_by": "userId",
"type": "Ingreso",
"observation": "It is a long established fact that a reader will be distracted",
"old_quantity": 0,
"new_quantity": 100,
"total_quantity": 100
},...
]
}
}
]
This is the response request from my api, until here its ok, but I would like to show something like this:
[
{
"code": 4,
"created_at": "2022-11-25T23:31:38.523Z",
"created_by": "userId",
"type": "Ingreso",
"observation": "It is a long established fact that a reader will be distracted",
"old_quantity": 0,
"new_quantity": 10,
"total_quantity": 10
},
{
"code": 5,
"created_at": "2022-11-25T23:31:38.523Z",
"created_by": "userId",
"type": "Ingreso",
"observation": "It is a long established fact that a reader will be distracted",
"old_quantity": 10,
"new_quantity": 20,
"total_quantity": 30
},
{
"code": 1,
"created_at": "2022-11-25T18:52:54.528Z",
"created_by": "userId",
"type": "Ingreso",
"observation": "It is a long established fact that a reader will be distracted",
"old_quantity": 0,
"new_quantity": 100,
"total_quantity": 100
},
{
"code": 2,
"created_at": "2022-11-25T18:52:54.528Z",
"created_by": "userId",
"type": "Ingreso",
"observation": "It is a long established fact that a reader will be distracted",
"old_quantity": 100,
"new_quantity": 100,
"total_quantity": 200
},...
]
I don't know if it's possible to do it this way. If it is not possible at least I would expect to suppress the embedded elements of the document; leaving in a first level entries
array. Something like this:
[
{
"_id": "638150ee268d890e9983881f",
"entries": [
{
"code": 4,
"created_at": "2022-11-25T23:31:38.523Z",
"created_by": "userId",
"type": "Ingreso",
"observation": "It is a long established fact that a reader will be distracted",
"old_quantity": 0,
"new_quantity": 10,
"total_quantity": 10
}...
]
},
{
"_id": "63810f179db70fea1263dea5",
"entries": [
{
"code": 1,
"created_at": "2022-11-25T18:52:54.528Z",
"created_by": "userId",
"type": "Ingreso",
"observation": "It is a long established fact that a reader will be distracted",
"old_quantity": 0,
"new_quantity": 100,
"total_quantity": 100
}...
]
}
];
As each document contains other information, for the purposes of this example I am only focusing on displaying the incoming inventory movement data so I have implemented the following and it works, but not as I would expect (mentioned above):
return this.connect().then((db) => {
return db
.collection(collection)
.find({ is_deleted: false })
.project({ movements: { entries: 1 } })
.sort({ _id: -1 })
.toArray();
});
CodePudding user response:
In the projection, set entries
field with the value of movements.entries
.
return this.connect().then((db) => {
return db
.collection(collection)
.find({ is_deleted: false })
.project({
entries: "$movements.entries"
})
.sort({ _id: -1 })
.toArray();
});