在mongodb

时间:2018-05-19 09:17:44

标签: mongodb mongodb-query

我是mongodb查询编程的新手。我只需要获取datetime(desc)订购的帖子的最后3条评论。这是我的馆藏结构:

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
        }]
    ]
}]

1 个答案:

答案 0 :(得分:1)

如果你有 mongodb版本3.6

db.collection.aggregate([
  { "$lookup": {
    "from": "post_comments",
    "let": { "post_id": "$_id" },
    "pipeline": [
      { "$match": {
        "$expr": { "$eq": [ "$post", "$$post_id" ] }
      }},
      {"$sort":{"datetime":-1}},
      {"$limit": 3}
    ],
    "as": "three_comments"
  }},
  { "$lookup": {
       "from":"users",
       "localField":"user",
       "foreignField":"_id",
       "as":"user"
    }
  }
])