MongoDB/Pymongo:UpSert阵列元件

发布于 2025-01-29 16:11:00 字数 681 浏览 3 评论 0 原文

我有以下文档:

    {'software_house': 'k1',
     'client_id': '1234',

     'transactions': [
      {'antecedents': 12345,
       'consequents': '015896018',
       'antecedent support': 0.0030889166727954697},

      {'antecedents': '932696735',
       'consequents': '939605046',
       'antecedent support': 0.0012502757961314996}
      ...
                     ]}

每个项目中的键“交易”存储在数组3个功能中。

我想更新“交易”数组中包含的每个项目,这些项目与相同的“ software_house”,“ client_id”,“ transactions.antecedents”和“ transactions.constequents”匹配;因此:

  • 如果数组中的元素确实存在,则覆盖元素
  • 在“交易”中附上一个新值,如果我该

如何使用pymongo实现这一目标?

I have the following document:

    {'software_house': 'k1',
     'client_id': '1234',

     'transactions': [
      {'antecedents': 12345,
       'consequents': '015896018',
       'antecedent support': 0.0030889166727954697},

      {'antecedents': '932696735',
       'consequents': '939605046',
       'antecedent support': 0.0012502757961314996}
      ...
                     ]}

In which key 'transactions' stores within an array 3 features, for each item.

I would like to update each item contained in the 'transactions' array, that matches with the same 'software_house', 'client_id', 'transactions.antecedents' and 'transactions.consequents'; and thus:

  • Overwriting the element within the array if it does exist
  • Appending a new value within 'transactions' if it doesn't

How could I achieve that using pymongo?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

总攻大人 2025-02-05 16:11:00

您可以使用Contregation Pipeline进行更新来执行此操作。您可以首先 $ filter 匹配元素。然后 $ setunion 带有您要upsert

pymongo的项目:

db.collection.update_many(filter = {
  // the criteria you want to match outside array
  "software_house": "k1",
  "client_id": "1234"
},
update = [
  {
    "$addFields": {
      "transactions": {
        "$filter": {
          "input": "$transactions",
          "as": "t",
          "cond": {
            $not: {
              $and: [
                // the criteria you want to match in array
                {
                  $eq: [
                    "$t.antecedents",
                    12345
                  ]
                },
                {
                  $eq: [
                    "$t.consequents",
                    "015896018"
                  ]
                }
              ]
            }
          }
        }
      }
    }
  },
  {
    "$addFields": {
      "transactions": {
        "$setUnion": [
          "$transactions",
          [
            {
              "antecedents": 12345,
              "consequents": "the entry you want to upsert",
              "antecedent support": -1
            }
          ]
        ]
      }
    }
  }
])

本机mongodb查询:

db.collection.update({
  // the criteria you want to match outside array
  "software_house": "k1",
  "client_id": "1234"
},
[
  {
    "$addFields": {
      "transactions": {
        "$filter": {
          "input": "$transactions",
          "as": "t",
          "cond": {
            $not: {
              $and: [
                // the criteria you want to match in array
                {
                  $eq: [
                    "$t.antecedents",
                    12345
                  ]
                },
                {
                  $eq: [
                    "$t.consequents",
                    "015896018"
                  ]
                }
              ]
            }
          }
        }
      }
    }
  },
  {
    "$addFields": {
      "transactions": {
        "$setUnion": [
          "$transactions",
          [
            {
              "antecedents": 12345,
              "consequents": "the entry you want to upsert",
              "antecedent support": -1
            }
          ]
        ]
      }
    }
  }
],
{
  multi: true
})

这是供您参考。

You can do this with an update with aggregation pipeline. You can first $filter the element matched. Then $setUnion with the item you want to upsert

PyMongo:

db.collection.update_many(filter = {
  // the criteria you want to match outside array
  "software_house": "k1",
  "client_id": "1234"
},
update = [
  {
    "$addFields": {
      "transactions": {
        "$filter": {
          "input": "$transactions",
          "as": "t",
          "cond": {
            $not: {
              $and: [
                // the criteria you want to match in array
                {
                  $eq: [
                    "$t.antecedents",
                    12345
                  ]
                },
                {
                  $eq: [
                    "$t.consequents",
                    "015896018"
                  ]
                }
              ]
            }
          }
        }
      }
    }
  },
  {
    "$addFields": {
      "transactions": {
        "$setUnion": [
          "$transactions",
          [
            {
              "antecedents": 12345,
              "consequents": "the entry you want to upsert",
              "antecedent support": -1
            }
          ]
        ]
      }
    }
  }
])

Native MongoDB query:

db.collection.update({
  // the criteria you want to match outside array
  "software_house": "k1",
  "client_id": "1234"
},
[
  {
    "$addFields": {
      "transactions": {
        "$filter": {
          "input": "$transactions",
          "as": "t",
          "cond": {
            $not: {
              $and: [
                // the criteria you want to match in array
                {
                  $eq: [
                    "$t.antecedents",
                    12345
                  ]
                },
                {
                  $eq: [
                    "$t.consequents",
                    "015896018"
                  ]
                }
              ]
            }
          }
        }
      }
    }
  },
  {
    "$addFields": {
      "transactions": {
        "$setUnion": [
          "$transactions",
          [
            {
              "antecedents": 12345,
              "consequents": "the entry you want to upsert",
              "antecedent support": -1
            }
          ]
        ]
      }
    }
  }
],
{
  multi: true
})

Here is the Mongo playground for your reference.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文