如何在 MONGODB 中删除数组中的重复值?

Posted

技术标签:

【中文标题】如何在 MONGODB 中删除数组中的重复值?【英文标题】:How to remove duplicate values inside an array in MONGODB? 【发布时间】:2021-11-24 16:45:46 【问题描述】:

个人架构:

[
    
      "_id": "e6f32800-240e-11ec-b291-51abbaa8f015",
      "firstName": "A",
      "lastName": "Cris",
      "phoneNumber": "111-222-3333",
      "socialMedia": "FaceBook",
      "DOB": "01/01/1990",
      "Theater": 1,
      "__v": 0
    ,
    
      "_id": "e7092b90-240f-11ec-8812-d375202a89ac",
      "firstName": "A",
      "lastName": "Cris",
      "phoneNumber": "111-222-3333",
      "socialMedia": "FaceBook",
      "DOB": "01/01/1990",
      "Theater": 2,
      "__v": 0
    ,
    
      "_id": "e8e78880-240f-11ec-8812-d375202a89ac",
      "firstName": "A",
      "lastName": "Cris",
      "phoneNumber": "111-222-3333",
      "socialMedia": "Twitter",
      "DOB": "01/01/1990",
      "Theater": 3,
      "__v": 0
    ,
    
      "_id": "ee20f750-240f-11ec-8812-d375202a89ac",
      "firstName": "B",
      "lastName": "Hood",
      "phoneNumber": "333-444-5555",
      "socialMedia": "Friends",
      "DOB": "05/05/1993",
      "Theater": 1,
      "__v": 0
    ,
    
      "_id": "76d6ad60-2410-11ec-bc7f-8dbab8f2c871",
      "firstName": "B",
      "lastName": "Hood",
      "phoneNumber": "333-444-5555",
      "socialMedia": "Radio 900 AM",
      "DOB": "05/05/1993",
      "Theater": 2,
      "__v": 0
    ,
    
      "_id": "f053b5d0-240f-11ec-8812-d375202a89ac",
      "firstName": "B",
      "lastName": "Hood",
      "phoneNumber": "333-444-5555",
      "socialMedia": "Radio 900 AM",
      "DOB": "05/05/1993",
      "Theater": 3,
      "__v": 0
    ,
    
      "_id": "79946dd0-2410-11ec-bc7f-8dbab8f2c871",
      "firstName": "C",
      "lastName": "Mohammad",
      "phoneNumber": "555-666-7777",
      "socialMedia": "Radio 104.2 PM",
      "DOB": "10/10/1995",
      "Theater": 1,
      "__v": 0
    ,
    
      "_id": "7b4244e0-2410-11ec-bc7f-8dbab8f2c871",
      "firstName": "C",
      "lastName": "Mohammad",
      "phoneNumber": "555-666-7777",
      "socialMedia": "News",
      "DOB": "10/10/1995",
      "Theater": 2,
      "__v": 0
    ,
    
      "_id": "7d097050-2410-11ec-bc7f-8dbab8f2c871",
      "firstName": "C",
      "lastName": "Mohammad",
      "phoneNumber": "555-666-7777",
      "socialMedia": "News",
      "DOB": "10/10/1995",
      "Theater": 3,
      "__v": 0
    
]

电影院架构:

[
    
      "_id": 1,
      "TheaterName": "AMC Katy Mill 20",
      "location": "Katy, TX",
      "__v": 0
    ,
    
      "_id": 2,
      "TheaterName": "AMC First Colony",
      "location": "Sugar Land, TX",
      "__v": 0
    ,
    
      "_id": 3,
      "TheaterName": "AMC Deer***",
      "location": "Humble, TX",
      "__v": 0
    
]

注意:

    Individuals 架构中的剧院字段是 _id 的标识符 MovieTheater 架构。 socialMedia 字段是个人了解MovieTheater 的方式

我可以做多少次(计数)个人加入电影院,将所有社交媒体推入一个数组,使用以下方法进行投影:

db.collection.aggregate([
  
    "$group": 
      "_id": 
        "firstName": "$firstName",
        "lastName": "$lastName",
        "phoneNumber": "$phoneNumber",
        "DOB": "$DOB"
      ,
      "count": 
        "$sum": 1
      ,
      "socialMedia": 
        "$push": "$socialMedia"
      
    
  ,
 
    "$project": 
        "_id.firstName": 1,
        "_id.lastName": 1,
        "Count": 1,
        "socialMedia": 1
    
 
])

输出:

[
  
    "_id": 
      "firstName": "A",
      "lastName": "Cris"
    ,
    "Count": 3,
    "socialMedia": [
      "FaceBook",
      "FaceBook",
      "Twitter"
    ]
  ,
  
    "_id": 
      "firstName": "B",
      "lastName": "Hood"
    ,
    "Count": 3,
    "socialMedia": [
      "Radio 900 AM",
      "Radio 900 AM",
      "Friends"
    ]
  ,
  
    "_id": 
      "firstName": "C",
      "lastName": "Mohammad"
    ,
    "Count": 3,
    "socialMedia": [
      "News",
      "News",
      "Radio 104.2 PM"
    ]
  
]

但我不知道要删除 socialMedia 中的重复值。

我的预期结果如下:


    "_id": 
      "firstName": "A",
      "lastName": "Cris"
    ,
    "Count": 3,
    "socialMedia": [
      "FaceBook",
      "Twitter"
    ]

非常感谢。

【问题讨论】:

【参考方案1】:

使用$addToSet 代替$push 代替socialMedia

$addToSet

$addToSet 返回一个包含所有唯一值的数组,这些值是通过将表达式应用于组中的每个文档而产生的。

db.collection.aggregate([
  
    "$group": 
      "_id": 
        "firstName": "$firstName",
        "lastName": "$lastName",
        "phoneNumber": "$phoneNumber",
        "DOB": "$DOB"
      ,
      "count": 
        "$sum": 1
      ,
      "socialMedia": 
        "$addToSet": "$socialMedia"
      
    
  ,
  
    "$project": 
      "_id.firstName": 1,
      "_id.lastName": 1,
      "Count": 1,
      "socialMedia": 1
    
  
])

Sample Mongo Playground

【讨论】:

以上是关于如何在 MONGODB 中删除数组中的重复值?的主要内容,如果未能解决你的问题,请参考以下文章

如何从js中的数组中删除元素[元素来自mongodb] [重复]

如何从 PHP 中的数组中删除重复值

如何从 PHP 中的多维数组中删除重复值

如何从 PHP 中的多维数组中删除重复值 [重复]

如何从添加到字典中的数组中删除重复值

如何删除MongoDB一个document中数组的一项