【问题标题】:How to check if an element exists in a document and return true or false depending on it?如何检查文档中是否存在元素并根据它返回true或false?
【发布时间】:2019-06-17 13:43:47
【问题描述】:

我有一个聚合查询,我在其中使用$lookup 从其他集合中获取数据。但是如果找到$match,我无法理解如何获得布尔值。

架构

const likesSchema    = new mongoose.Schema({
user: {
    id: {
        type: String,
        required: true,
    },
    name: {
        type: String,
        required: true,
    },
},
storyID: {
    type: String,
    required: true,
}
}, {
    timestamps: true
});

完整查询

const user_id         = req.authorizedUser.sub;

const stories = await Story.aggregate([
                {
                    $lookup: {
                        from: "comments",
                        localField: "storyID",
                        foreignField: "storyID",
                        as: "comments"
                    },
                },

                {
                    $lookup: {
                        from: "likes",
                        let: {storyID: "$storyID"},
                        pipeline: [
                            {
                                $match: {
                                    $expr: { $eq: ["$$storyID", "$storyID"] }
                                }
                            },
                            {
                                $facet: {
                                    "total": [{ $count: "count" }],
                                    "byMe": [{
                                        $match: {
                                            $expr: { $eq: ["$user.id", user_id] } // Need boolean value if found/ not found
                                        }
                                    }]
                                }
                            }
                        ],
                        as: "likes"
                    }
                },

响应片段

"likes": [
                {
                    "total": [
                        {
                            "count": 2
                        }
                    ],
                    "byMe": [
                        {
                            "_id": "5d04fe8e982bb50bbcbd2b48",
                            "user": {
                                "id": "63p6PpPyOh",
                                "name": "Ayan Dey"
                            },
                            "storyID": "b0g5GA6ZJFKkJcnJlp6w8qGR",
                            "createdAt": "2019-06-15T14:19:58.531Z",
                            "updatedAt": "2019-06-15T14:19:58.531Z",
                            "__v": 0
                        }
                    ]
                }
            ]

必填项

"likes": {
    "total": 2,
    "byMe": true
}

【问题讨论】:

    标签: mongodb mongoose mongodb-query aggregation-framework


    【解决方案1】:

    你可以使用下面的聚合

    { "$lookup": {
      "from": "likes",
      "let": { "storyID": "$storyID" },
      "pipeline": [
        { "$match": { "$expr": { "$eq": ["$$storyID", "$storyID"] }}}
      ],
      "as": "likes1"
    }},
    { "$addFields": {
      "likes.total": { "$size": "$likes1" },
      "likes.byMe": { "$ne": [{ "$indexOfArray": ["$likes1.user.id", user_id] }, -1] }
    }},
    { "$project": { "likes1": 0 }}
    

    或者

    { "$lookup": {
      "from": "likes",
      "let": { "storyID": "$storyID" },
      "pipeline": [
        { "$match": { "$expr": { "$eq": ["$$storyID", "$storyID"] }}},
        { "$facet": {
          "total": [{ "$count": "count" }],
          "byMe": [{ "$match": { "$expr": { "$eq": ["$user.id", user_id] }}}]
        }}
        { "$project": {
          "total": {
            "$ifNull": [{ "$arrayElemAt": ["$total.count", 0] }, 0 ]
          },
          "byMe": { "$ne": [{ "$size": "$byMe" }, 0] }
        }}
      ],
      "as": "likes"
    }},
    { "$unwind": "$likes" }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-06-29
      • 1970-01-01
      • 2019-09-16
      • 2019-12-17
      • 2019-02-28
      • 2021-09-12
      • 1970-01-01
      相关资源
      最近更新 更多