Node.js MongoDb:我想返回数组中多次出现的所有值。我不知道该怎么办

Node.js MongoDb:我想返回数组中多次出现的所有值。我不知道该怎么办,node.js,mongodb,Node.js,Mongodb,我希望能够在“hashtag1”和“hashtag3”中搜索重复的帖子ID 哪个应该返回“posted2” 这可能吗?是的,您可以使用分组和计数来完成 我的回答有用吗?如果是,你能批准吗? [ { "postsWith": [ "postid1", "postid2", "postid3" ], "name": "hash

我希望能够在“hashtag1”和“hashtag3”中搜索重复的帖子ID

哪个应该返回“posted2”


这可能吗?

是的,您可以使用分组和计数来完成


我的回答有用吗?如果是,你能批准吗?
[
  {
    "postsWith": [
      "postid1",
      "postid2",
      "postid3"
    ],
    "name": "hashtag1",
  },
 {
    "postsWith": [
      "postid4",
      "postid5",
      "postid6"
    ],
    "name": "hashtag2",
  },
 {
    "postsWith": [
      "postid2"
    ],
    "name": "hashtag3",
  }
]
db.collection.aggregate([
  {//Reshape the array
    $unwind: "$postsWith"
  },
  {//Group and get the count
    $group: {
      "_id": "$postsWith",
      "sum": {
        $sum: 1
      }
    }
  },
  {//Definitely you can combine project and the following match
    $project: {
      "duplicate": {
        $cond: {
          if: {//Just adding a flag
            $gt: [
              "$sum",
              1
            ]
          },
          then: 1,
          else: 0
        }
      }
    }
  },
  {
    $match: {//returning only duplicate
      duplicate: 1
    }
  }
])