Я новичок в mongodb
программировании запросов.Мне просто нужно получить последние 3 комментария для постов, упорядоченных по datetime (desc).Вот моя структура коллекций:
post = {
text : "string",
imgs: "array",
video: "string",
videoImage: "string",
datetime: "datetime",
user: "object",
tags: "array",
status: "string",
category:'string',
tags:'array'
}
post_comments = {
text : "string",
datetime: "string",
user: "object",
post: "array"
}
Я записал агрегатные параметры следующим образом
[
{"$match": {"status":"online"}},
{"$lookup": {"from":"users","localField":"user","foreignField":"_id","as":"user"}},
{"$lookup":{"from":"post_comments","localField":"_id","foreignField":"post","as":"comments"}},
{"$sort":{"datetime":-1}},
{"$project":{
"text":1,
"imgs":1,
"video":1,
"tags":1,
"videoImage":1,
"datetime":1,
"user": {
"_id":1,
"name":1,
"username":1,
"city":1,
"location":1,
"accountType":1,
"language":1,
},
"category":1,
"three_comments":{
{$reverseArray: '$comments'},
{$slice: ['$three_comments', 3]}
}
}}
]
Но результат не тот, который ожидался.Массив tree_comments
правильно упорядочен, но не возвращает 3 комментария.он возвращает все комментарии.
[{
"_id": "5af94b4d71f33811513b4b79",
"category": "music",
"video": "",
"imgs": [],
"videoImage": false,
"text": "post",
"tags": [],
"datetime": 1526621841973,
"user": [{
"_id": "FuRRTBEYBmCSDPJkN",
"username": "user",
"language": "it",
}],
"three_comments": [
[{
"_id": "5afe669169130aeecc9c9ac8",
"user": "FuRRTBEYBmCSDPJkN",
"post": "5af94b4d71f33811513b4b79",
"text": "33",
"datetime": 1526621841844
}, {
"_id": "5af94b96119abe115ad7aff6",
"user": "FuRRTBEYBmCSDPJkN",
"post": "5af94b4d71f33811513b4b79",
"text": "22",
"timestamp": 1526287254793
}, {
"_id": "5af94b94119abe115ad7aff5",
"user": "FuRRTBEYBmCSDPJkN",
"post": "5af94b4d71f33811513b4b79",
"text": "11",
"timestamp": 1526287252138
}, {
"_id": "5af94b76119abe115ad7aff4",
"user": "FuRRTBEYBmCSDPJkN",
"post": "5af94b4d71f33811513b4b79",
"text": "00",
"timestamp": 1526287222566
}, {
"_id": "5af94b5271f33811513b4b7a",
"user": "FuRRTBEYBmCSDPJkN",
"post": "5af94b4d71f33811513b4b79",
"text": null,
"timestamp": 1526287186595
}], null
]
}, {
"_id": "5afd1cff2fc69da9adb3c973",
"category": "music",
"video": "https://www.youtube.com/embed/123",
"imgs": ["http://localhost:3002/uploads/images/post/42018/1526537471269.jpg", "http://localhost:3002/uploads/images/post/42018/1526537471403.jpg", "http://localhost:3002/uploads/images/post/42018/1526537471521.jpeg"],
"videoImage": "https://i.ytimg.com/vi/123/321.jpg",
"text": "second post",
"tags": [],
"datetime": 1526621816648,
"user": [{
"_id": "FuRRTBEYBmCSDPJkN",
"username": "user",
"language": "it",
}],
"three_comments": [
[{
"_id": "5afe667869130aeecc9c9ac7",
"user": "FuRRTBEYBmCSDPJkN",
"post": "5afd1cff2fc69da9adb3c973",
"text": "5",
"datetime": 1526621816517
}, {
"_id": "5afd1dcd2fc69da9adb3c977",
"user": "FuRRTBEYBmCSDPJkN",
"post": "5afd1cff2fc69da9adb3c973",
"text": "4",
"timestamp": 1526537677768
}, {
"_id": "5afd1dc72fc69da9adb3c976",
"user": "FuRRTBEYBmCSDPJkN",
"post": "5afd1cff2fc69da9adb3c973",
"text": "3",
"timestamp": 1526537671316
}, {
"_id": "5afd1dc12fc69da9adb3c975",
"user": "FuRRTBEYBmCSDPJkN",
"post": "5afd1cff2fc69da9adb3c973",
"text": "2",
"timestamp": 1526537665091
}, {
"_id": "5afd1da42fc69da9adb3c974",
"user": "FuRRTBEYBmCSDPJkN",
"post": "5afd1cff2fc69da9adb3c973",
"text": "1",
"timestamp": 1526537636677
}], null
]
}]
Я попытался (как предлагалось) нарезать массив $ comments вместо $ three_comments, и результат странный (см. ниже)
[{
"_id": "5afd1cff2fc69da9adb3c973",
"category": "music",
"video": "https://www.youtube.com/embed/123",
"imgs": ["http://localhost:3002/uploads/images/post/42018/1526537471269.jpg", "http://localhost:3002/uploads/images/post/42018/1526537471403.jpg", "http://localhost:3002/uploads/images/post/42018/1526537471521.jpeg"],
"videoImage": "https://i.ytimg.com/vi/321/123.jpg",
"text": "text",
"tags": [],
"datetime": 1526735767676,
"user": [{
"_id": "FuRRTBEYBmCSDPJkN",
"username": "user",
"language": "it",
}],
"marketplace": [],
"three_comments": [
[{
"_id": "5b00239771dd135081e1b3e1",
"user": "FuRRTBEYBmCSDPJkN",
"post": "5afd1cff2fc69da9adb3c973",
"text": "3",
"datetime": 1526735767500
}, {
"_id": "5afe667869130aeecc9c9ac7",
"user": "FuRRTBEYBmCSDPJkN",
"post": "5afd1cff2fc69da9adb3c973",
"text": "2",
"datetime": 1526621816517
}, {
"_id": "5afd1dcd2fc69da9adb3c977",
"user": "FuRRTBEYBmCSDPJkN",
"post": "5afd1cff2fc69da9adb3c973",
"text": "1",
"timestamp": 1526537677768
}, {
"_id": "5afd1dc72fc69da9adb3c976",
"user": "FuRRTBEYBmCSDPJkN",
"post": "5afd1cff2fc69da9adb3c973",
"text": "11",
"timestamp": 1526537671316
}, {
"_id": "5afd1dc12fc69da9adb3c975",
"user": "FuRRTBEYBmCSDPJkN",
"post": "5afd1cff2fc69da9adb3c973",
"text": "111",
"timestamp": 1526537665091
}, {
"_id": "5afd1da42fc69da9adb3c974",
"user": "FuRRTBEYBmCSDPJkN",
"post": "5afd1cff2fc69da9adb3c973",
"text": "111",
"timestamp": 1526537636677
}],
[{
"_id": "5afd1da42fc69da9adb3c974",
"user": "FuRRTBEYBmCSDPJkN",
"post": "5afd1cff2fc69da9adb3c973",
"text": "3",
"timestamp": 1526537636677
}, {
"_id": "5afd1dc12fc69da9adb3c975",
"user": "FuRRTBEYBmCSDPJkN",
"post": "5afd1cff2fc69da9adb3c973",
"text": "2",
"timestamp": 1526537665091
}, {
"_id": "5afd1dc72fc69da9adb3c976",
"user": "FuRRTBEYBmCSDPJkN",
"post": "5afd1cff2fc69da9adb3c973",
"text": "1",
"timestamp": 1526537671316
}]
]
}, {
"_id": "5af94b4d71f33811513b4b79",
"category": "music",
"video": "",
"imgs": [],
"videoImage": false,
"text": "post",
"tags": [],
"datetime": 1526723943037,
"user": [{
"_id": "FuRRTBEYBmCSDPJkN",
"username": "user",
"language": "it",
}],
"three_comments": [
[{
"_id": "5afff566b52b3447548fa0d1",
"user": "FuRRTBEYBmCSDPJkN",
"post": "5af94b4d71f33811513b4b79",
"text": "33",
"datetime": 1526723942934
}, {
"_id": "5afff527b52b3447548fa0d0",
"user": "FuRRTBEYBmCSDPJkN",
"post": "5af94b4d71f33811513b4b79",
"text": "22",
"datetime": 1526723879879
}, {
"_id": "5afff4cedc81f44731af6da1",
"user": "FuRRTBEYBmCSDPJkN",
"post": "5af94b4d71f33811513b4b79",
"text": "11",
"datetime": 1526723790513
}, {
"_id": "5afff4a26f0138470d7b48e8",
"user": "FuRRTBEYBmCSDPJkN",
"post": "5af94b4d71f33811513b4b79",
"text": "111",
"datetime": 1526723746622
}, {
"_id": "5afff4886f0138470d7b48e7",
"user": "FuRRTBEYBmCSDPJkN",
"post": "5af94b4d71f33811513b4b79",
"text": "1111",
"datetime": 1526723720873
}, {
"_id": "5afff43fd4ef4445101e8bfc",
"user": "FuRRTBEYBmCSDPJkN",
"post": "5af94b4d71f33811513b4b79",
"text": "11111",
"datetime": 1526723647463
}, {
"_id": "5afe669169130aeecc9c9ac8",
"user": "FuRRTBEYBmCSDPJkN",
"post": "5af94b4d71f33811513b4b79",
"text": "111111",
"datetime": 1526621841844
}, {
"_id": "5af94b96119abe115ad7aff6",
"user": "FuRRTBEYBmCSDPJkN",
"post": "5af94b4d71f33811513b4b79",
"text": "1111111",
"timestamp": 1526287254793
}, {
"_id": "5af94b94119abe115ad7aff5",
"user": "FuRRTBEYBmCSDPJkN",
"post": "5af94b4d71f33811513b4b79",
"text": "11111111",
"timestamp": 1526287252138
}, {
"_id": "5af94b76119abe115ad7aff4",
"user": "FuRRTBEYBmCSDPJkN",
"post": "5af94b4d71f33811513b4b79",
"text": "111111111",
"timestamp": 1526287222566
}, {
"_id": "5af94b5271f33811513b4b7a",
"user": "FuRRTBEYBmCSDPJkN",
"post": "5af94b4d71f33811513b4b79",
"text": null,
"timestamp": 1526287186595
}],
[{
"_id": "5af94b5271f33811513b4b7a",
"user": "FuRRTBEYBmCSDPJkN",
"post": "5af94b4d71f33811513b4b79",
"text": null,
"timestamp": 1526287186595
}, {
"_id": "5af94b76119abe115ad7aff4",
"user": "FuRRTBEYBmCSDPJkN",
"post": "5af94b4d71f33811513b4b79",
"text": "111",
"timestamp": 1526287222566
}, {
"_id": "5af94b94119abe115ad7aff5",
"user": "FuRRTBEYBmCSDPJkN",
"post": "5af94b4d71f33811513b4b79",
"text": "1111",
"timestamp": 1526287252138
}]
]
}]