'MongoDB aggregate Lookup remove array [duplicate]
I am currently using an aggregate lookup method from mongoose and it works:
const data = await this.messageModel.aggregate([
{
$match: { channel_id: channel_id },
},
{
$limit: limit ? limit : 1000,
},
]).lookup({
from: 'users',
localField: 'author',
foreignField: 'id',
as: 'author',
pipeline: [
{ $project: { _id: 0, password: 0, email: 0, __v: 0 } }
],
});
The data returns an array of object like this
{
"_id": "622861fe264eaed05a32bb2d",
"channel_id": "5850473746686541647",
"author": [
{
"username": "UnusualAbsurd",
"status": "Working...",
"createdAt": "2022-03-08T14:02:53.728Z",
"id": "1"
}
],
"id": "5850638795510120271",
"createdAt": "2022-03-09T08:14:54.228Z",
"attachments": [],
"content": "zazaza",
"__v": 0
}
Now the problem is, the author object that I used the lookup() method on returns as an array. How do I make it not return as an array?
Solution 1:[1]
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Harry |
