【问题标题】:MongoDB add new field by checking if key value is existsMongoDB 通过检查键值是否存在来添加新字段
【发布时间】:2023-02-05 12:29:01
【问题描述】:

我有一个看起来像这样的数据

[
  {
    "_id": "22435992",
    "address": "Merr Street",
    "contacted": [
      {
        "_id": "22435992",
        "date": "2022-12-27T04:58:47.306Z",
        "status": "Contacted",
        
      },
      {
        "_id": "22435992",
        "date": "2022-12-27T05:00:50.119Z",
        "status": "Contacted",
        
      },
      {
        "_id": "4f950467",
        "date": "2022-12-27T09:33:51.783Z",
        "status": "Contacted",
        
      },
      {
        "_id": "4f950467",
        "date": "2022-12-27T09:33:55.472Z",
        "status": "Contacted",
        
      },
      {
        "_id": "4f950467",
        "date": "2022-12-27T09:33:58.846Z",
        "status": "Contacted",
        
      },
      
    ],
    "fullName": "Waltz",
    
  },
  {
    "_id": "a2203725",
    "address": "Youm Street",
    "contacted": [
      {
        "_id": "22435992",
        "date": "2022-12-27T04:58:47.306Z",
        "status": "Contacted",
        
      },
      {
        "_id": "22435992",
        "date": "2022-12-27T05:00:50.119Z",
        "status": "Contacted",
        
      },
      {
        "_id": "4f950467",
        "date": "2022-12-27T09:33:51.783Z",
        "status": "Contacted",
        
      },
      {
        "_id": "4f950467",
        "date": "2022-12-27T09:33:55.472Z",
        "status": "Contacted",
        
      },
      {
        "_id": "4f950467",
        "date": "2022-12-27T09:33:58.846Z",
        "status": "Contacted",
        
      },
      
    ],
    "fullName": "Bono",
    
  },
  {
    "_id": "4f950467",
    "address": "Sesame Streetm",
    "contacted": [
      {
        "_id": "22435992",
        "date": "2022-12-27T04:58:47.306Z",
        "status": "Contacted",
        
      },
      {
        "_id": "22435992",
        "date": "2022-12-27T05:00:50.119Z",
        "status": "Contacted",
        
      },
      {
        "_id": "4f950467",
        "date": "2022-12-27T09:33:51.783Z",
        "status": "Contacted",
        
      },
      {
        "_id": "4f950467",
        "date": "2022-12-27T09:33:55.472Z",
        "status": "Contacted",
        
      },
      {
        "_id": "4f950467",
        "date": "2022-12-27T09:33:58.846Z",
        "status": "Contacted",
        
      },
      
    ],
    "fullName": "Axl",
    
  },
  {
    "_id": "3d338340",
    "address": "Reign Street",
    "contacted": [
      {
        "_id": "22435992",
        "date": "2022-12-27T04:58:47.306Z",
        "status": "Contacted",
        
      },
      {
        "_id": "22435992",
        "date": "2022-12-27T05:00:50.119Z",
        "status": "Contacted",
        
      },
      {
        "_id": "4f950467",
        "date": "2022-12-27T09:33:51.783Z",
        "status": "Contacted",
        
      },
      {
        "_id": "4f950467",
        "date": "2022-12-27T09:33:55.472Z",
        "status": "Contacted",
        
      },
      {
        "_id": "4f950467",
        "date": "2022-12-27T09:33:58.846Z",
        "status": "Contacted",
        
      },
      
    ],
    "fullName": "Jean",
    
  },
  
]

我想添加一个名为 contactStatus 的新字段,其值来自检查 userId 是否存在于已联系的数组中,因此它看起来像这样

[
  {
    "_id": "22435992",
    "address": "Merr Street",
    "contactStatus": "Contacted",
    "contacted": [
      {
        "_id": "22435992",
        "date": "2022-12-27T04:58:47.306Z",
        "status": "Contacted"
      },
      {
        "_id": "22435992",
        "date": "2022-12-27T05:00:50.119Z",
        "status": "Contacted"
      },
      {
        "_id": "4f950467",
        "date": "2022-12-27T09:33:51.783Z",
        "status": "Contacted"
      },
      {
        "_id": "4f950467",
        "date": "2022-12-27T09:33:55.472Z",
        "status": "Contacted"
      },
      {
        "_id": "4f950467",
        "date": "2022-12-27T09:33:58.846Z",
        "status": "Contacted"
      }
    ],
    "fullName": "Waltz"
  },
  {
    "_id": "a2203725",
    "address": "Youm Street",
    "contactStatus": "Not Contacted",
    "contacted": [
      {
        "_id": "22435992",
        "date": "2022-12-27T04:58:47.306Z",
        "status": "Contacted"
      },
      {
        "_id": "22435992",
        "date": "2022-12-27T05:00:50.119Z",
        "status": "Contacted"
      },
      {
        "_id": "4f950467",
        "date": "2022-12-27T09:33:51.783Z",
        "status": "Contacted"
      },
      {
        "_id": "4f950467",
        "date": "2022-12-27T09:33:55.472Z",
        "status": "Contacted"
      },
      {
        "_id": "4f950467",
        "date": "2022-12-27T09:33:58.846Z",
        "status": "Contacted"
      }
    ],
    "fullName": "Bono"
  },
  {
    "_id": "4f950467",
    "address": "Sesame Streetm",
    "contactStatus": "Contacted",
    "contacted": [
      {
        "_id": "22435992",
        "date": "2022-12-27T04:58:47.306Z",
        "status": "Contacted"
      },
      {
        "_id": "22435992",
        "date": "2022-12-27T05:00:50.119Z",
        "status": "Contacted"
      },
      {
        "_id": "4f950467",
        "date": "2022-12-27T09:33:51.783Z",
        "status": "Contacted"
      },
      {
        "_id": "4f950467",
        "date": "2022-12-27T09:33:55.472Z",
        "status": "Contacted"
      },
      {
        "_id": "4f950467",
        "date": "2022-12-27T09:33:58.846Z",
        "status": "Contacted"
      }
    ],
    "fullName": "Axl"
  },
  {
    "_id": "3d338340",
    "address": "Reign Street",
    "contactStatus": "Not Contacted",
    "contacted": [
      {
        "_id": "22435992",
        "date": "2022-12-27T04:58:47.306Z",
        "status": "Contacted"
      },
      {
        "_id": "22435992",
        "date": "2022-12-27T05:00:50.119Z",
        "status": "Contacted"
      },
      {
        "_id": "4f950467",
        "date": "2022-12-27T09:33:51.783Z",
        "status": "Contacted"
      },
      {
        "_id": "4f950467",
        "date": "2022-12-27T09:33:55.472Z",
        "status": "Contacted"
      },
      {
        "_id": "4f950467",
        "date": "2022-12-27T09:33:58.846Z",
        "status": "Contacted"
      }
    ],
    "fullName": "Jean"
  }
]

我一直在尝试添加新字段,但它向所有数据返回“未联系”

db.collection.aggregate([
  {
    $addFields: {
      contactStatus: {
        $reduce: {
          input: "$contacted",
          initialValue: "$_id",
          in: {
            $cond: [
              {
                $eq: [
                  "$$this._id",
                  "$$value"
                ]
              },
              "$$this.status",
              "Not Contacted"
            ]
          }
        }
      }
    }
  }
])                          

这是我最近的尝试,它只给出 true false 作为值,但由于 status 中有超过 2 个状态,我认为这种方法不正确

db.collection.aggregate([
  {
    "$set": {
      "contactStatus": {
        "$in": [
          "$_id",
          "$contacted._id"
        ]
      }
    }
  },
])

我不确定是否使用 reduce 但我想不出任何其他方法来检查联系数组中的 userId,请帮助我

【问题讨论】:

    标签: mongodb mongodb-query aggregation-framework


    【解决方案1】:

    您选择$reduce 的做法是明智的。您可以将 initialValue 设置为未联系并将值更新为接触过只有满足两个条件。 (即 _id 匹配且状态为已联系)

    db.collection.update({},
    [
      {
        $set: {
          contactStatus: {
            "$reduce": {
              "input": "$contacted",
              "initialValue": "Not Contacted",
              "in": {
                "$cond": {
                  "if": {
                    $and: [
                      {
                        $eq: [
                          "$_id",
                          "$$this._id"
                        ]
                      },
                      {
                        $eq: [
                          "$$this.status",
                          "Contacted"
                        ]
                      }
                    ]
                  },
                  "then": "Contacted",
                  "else": "$$value"
                }
              }
            }
          }
        }
      }
    ])
    

    Mongo playground

    【讨论】:

      猜你喜欢
      • 2019-08-02
      • 1970-01-01
      • 1970-01-01
      • 2013-11-20
      • 1970-01-01
      • 2016-09-09
      相关资源
      最近更新 更多