I have a users collection in MongoDB with this sample row:
[
{
"_id": 1,
"name": "John Doe",
"comments": [
{
"_id": 100,
"content": "Comment 100",
"post": "1000"
},
{
"_id": 101,
"content": "Comment 101",
"post": "1000"
}
]
}
]
I want to project users and comments data after converting _id fields into id. So I used the following query:
db.users.aggregate([
{
$project: {
_id: 0,
id: '$_id',
name:1
comments: {
_id: 0,
id: '$_id',
content: 1,
},
}
}
])
Now the users _id field is successfully converted. But the comments id field is equal to _id of users collection instead of comments.
[
{
"id": 1,
"name": "john Doe",
"comments": [
{
"id": 1,
"content": "comment 100"
},
{
"id": 1,
"content": "comment 101"
},
]
}
]
How can I achieve the right result.