【问题标题】:Merge documents from 2 collections in MongoDB & preserve property of a field在 MongoDB 中合并来自 2 个集合的文档并保留字段的属性
【发布时间】:2022-01-18 14:06:51
【问题描述】:

我有两个集合,1.temporaryCollection,2.permanentCollection,我想从temporaryCollection 中获取数据并在permanentCollection 中更新。要查看预期结果,请参阅下面的 updatedPermanentCollection。

从临时集合中获取并在永久集合中更新的字段是:

  1. 电子邮件地址
  2. 电话号码
  3. 联系人姓名
  4. 联系电话

临时集合中更改的字段供您参考

  1. 联系人[0]['emailAddresses']
  2. 联系人[0]['ContactName']
  3. 联系人[0]["phoneNumbers"]
  4. 联系人[0]["ContactNumber"]

UpdatedPermanentCollection 中更新后不应更改的字段是

contacts._id

注意:contacts 是一个对象数组,为简单起见,我只展示了一个对象。

我目前正在使用以下查询,该查询更新了 PermanentCollection,但也覆盖了 contacts._id 字段。我不希望 contacts._id 字段被覆盖。

这是我的 MongoDB 查询

db.temporaryCollection.aggregate([
  {
    $match: {
      userID: ObjectId("61d1efea2c0fab00340f47c8"),
    },
  },
  {
    $merge: {
      into: "permanentCollection",
      on: "userID",
      whenMatched: "merge",
      whenNotMatched: "insert",
    },
  },
]);

1.临时集合

{
  "_id": { "$oid": "61d1f04266289f003452d705" },
  "userID": { "$oid": "61d1efea2c0fab00340f47c8" },
  "contacts": [
    {
      "emailAddresses": [
        { "id": "6884", "label": "email1", "email": "addedemail@gmail.com" }
      ],
      "phoneNumbers": [
        {
          "label": "other",
          "id": "4594",
          "number": "+918984292930"
        },
        {
          "label": "other",
          "id": "4595",
          "number": "+911234567890"
        }
      ],
      "_id": { "$oid": "61d1f04266289f003452d744" },
      "ContactName": "Sample User 1 Name Changed",
      "ContactNumber": "+918984292930",
      "recordID": "833"
    }
  ],
  "userNumber": "+911234567890",
  "__v": 7
}

2。永久收藏

    {
  "_id": { "$oid": "61d1f04266289f003452d701" },
  "userID": { "$oid": "61d1efea2c0fab00340f47c8" },
  "contacts": [
    {
      "emailAddresses": [],
      "phoneNumbers": [
        {
          "label": "other",
          "id": "4594",
          "number": "+918984292929"
        },
        {
          "label": "other",
          "id": "4595",
          "number": "+911234567890"
        }
      ],
      "_id": { "$oid": "61d1f04266289f003452d722" },
      "ContactName": "Sample User 1",
      "ContactNumber": "+918984292929",
      "recordID": "833"
    }
  ],
  "userNumber": "+911234567890",
  "__v": 7
}

3。 updatedPermanentCollection(预期结果)

    {
  "_id": { "$oid": "61d1f04266289f003452d701" },
  "userID": { "$oid": "61d1efea2c0fab00340f47c8" },
  "contacts": [
    {
      "emailAddresses": [
        { "id": "6884", "label": "email1", "email": "addedemail@gmail.com" }
      ],
      "phoneNumbers": [
        {
          "label": "other",
          "id": "4594",
          "number": "+918984292930"
        },
        {
          "label": "other",
          "id": "4595",
          "number": "+911234567890"
        }
      ],
      "_id": { "$oid": "61d1f04266289f003452d722" },
      "ContactName": "Sample User 1 Name Changed",
      "ContactNumber": "+918984292930",
      "recordID": "833"
    }
  ],
  "userNumber": "+911234567890",
  "__v": 7
}

【问题讨论】:

    标签: mongodb mongoose mongodb-query


    【解决方案1】:

    试试这个聚合查询。

    db.temporarCollection.aggreagate(
    [
      {
        "$lookup": {
          "from": "permanantCollection", 
          "let": {
            "user_id": "$userID"
          }, 
          "pipeline": [
            {
              "$match": {
                "$expr": {
                  "$eq": [
                    "$$user_id", "$userID"
                  ]
                }
              }
            }
          ], 
          "as": "pcontacts"
        }
      }, {
        "$unwind": {
          "path": "$pcontacts", 
          "preserveNullAndEmptyArrays": true
        }
      }, {
        "$project": {
          "contacts": {
            "$map": {
              "input": "$contacts", 
              "as": "contact", 
              "in": {
                "tcontact": "$$contact", 
                "pcontact": {
                  "$first": {
                    "$filter": {
                      "input": "$pcontacts.contacts", 
                      "as": "pcontact", 
                      "cond": {
                        "$eq": [
                          "$$pcontact.recordID", "$$contact.recordID"
                        ]
                      }
                    }
                  }
                }
              }
            }
          }, 
          "userNumber": 1, 
          "userID": 1, 
          "_id": 0
        }
      }, {
        "$project": {
          "contacts": {
            "$map": {
              "input": "$contacts", 
              "as": "contact", 
              "in": {
                "emailAddresses": "$$contact.tcontact.emailAddresses", 
                "phoneNumbers": "$$contact.tcontact.phoneNumbers", 
                "ContactName": "$$contact.tcontact.ContactName", 
                "ContactNumber": "$$contact.tcontact.ContactNumber", 
                "recordID": {
                  "$let": {
                    "vars": {}, 
                    "in": {
                      "$cond": {
                        "if": "$$contact.pcontact.recordID", 
                        "then": "$$contact.pcontact.recordID", 
                        "else": "$$contact.tcontact.recordID"
                      }
                    }
                  }
                }, 
                "_id": {
                  "$let": {
                    "vars": {}, 
                    "in": {
                      "$cond": {
                        "if": "$$contact.pcontact._id", 
                        "then": "$$contact.pcontact._id", 
                        "else": "$$contact.tcontact._id"
                      }
                    }
                  }
                }
              }
            }
          }, 
          "userNumber": 1, 
          "userID": 1
        }
      }, {
        "$merge": {
          "into": "pc", 
          "on": "userID", 
          "whenMatched": "replace", 
          "whenNotMatched": "insert"
        }
      }
    ])
    

    这不是一个完全优化的查询,但它可以工作。

    【讨论】:

      【解决方案2】:

      尝试将 $unset 添加到数据库查询中。

      db.temporaryCollection.aggregate([
        {
          $unset: "_id"
        },
        {
          $match: {
            userID: ObjectId("61d1efea2c0fab00340f47c8"),
          },
        },
        {
          $merge: {
            into: "permanentCollection",
            on: "userID",
            whenMatched: "merge",
            whenNotMatched: "insert",
          },
        },
      ]);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-10-30
        • 2017-10-17
        • 2015-02-17
        • 2017-07-13
        • 2015-05-03
        • 1970-01-01
        • 2020-12-12
        • 1970-01-01
        相关资源
        最近更新 更多