mongo 5 findOneAndupdate聚合管道用于默认计数器值?

发布于 2025-02-08 04:07:56 字数 1026 浏览 0 评论 0原文

有几个旧问题(2015/2016)关于使用具有默认值的$ INC进行UPSERT。共识似乎是多个DB调用。我希望现在有一种更现代的方法。我正在使用Mongo 5.0.9。

我发现此示例指出您现在可以在更新聚合管道中使用$ cond。我尝试了以下内容:

const result = await context.params.tenantDb.collection('counters').findOneAndUpdate({
    // Filter
    name: options.name,
  }, {
    // Data
    $set: {
      value: {
        $cond: {
          if: {$eq:[{$type:"$value"} ,  "missing"]},
          then: 1000,   // it's the upsert case
          else: { // it's the update case
             $inc: {
               value: 1
             }
          }    
       }
      },
    },
  }, {
    // Mongo Options
    returnDocument: 'after',
    name: options.name,
    upsert: true,
  });

https://stackoverflow.com/a/62819768/1795570 但是,只是将整个$ cond添加到我的文档中。

 value: {
    _id: new ObjectId("62ab8829d8ea984d839e10f3"),
    name: 'my_counter',
    value: { '$cond': [Array] }
  },

可以这样做吗?

Theres a several old questions (2015/2016) about doing an upsert with $inc that has a default value. The consensus seemed to be do multiple DB calls. I'm hoping there is a more modern approach to this now. I'm using Mongo 5.0.9.

I found this example that states you can now use $cond in update aggregation pipelines. I tried the following:
https://stackoverflow.com/a/62819768/1795570

Trying to convert it to my problem I came up with:

const result = await context.params.tenantDb.collection('counters').findOneAndUpdate({
    // Filter
    name: options.name,
  }, {
    // Data
    $set: {
      value: {
        $cond: {
          if: {$eq:[{$type:"$value"} ,  "missing"]},
          then: 1000,   // it's the upsert case
          else: { // it's the update case
             $inc: {
               value: 1
             }
          }    
       }
      },
    },
  }, {
    // Mongo Options
    returnDocument: 'after',
    name: options.name,
    upsert: true,
  });

This however just adds the entire $cond as the value to my document.

 value: {
    _id: new ObjectId("62ab8829d8ea984d839e10f3"),
    name: 'my_counter',
    value: { '$cond': [Array] }
  },

Is it possible to do this?

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

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

发布评论

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

评论(1

书信已泛黄 2025-02-15 04:07:56

您将能够实现这一目标,您缺少[],这将帮助您使用聚合框架,然后您应该

能够实现这一目标,而您丢失了[],这将有所帮助您要使用聚合框架,然后您应该有这个

const result = await context.params.tenantDb.collection('counters').findOneAndUpdate({
    // Filter
    name: options.name,
}, [{
    $set: {
        value: {
            $cond: {
                if: {
                    $eq: [{
                            $type: "$value"
                        },
                        "missing"
                    ]
                },
                then: 1000,
                // it's the upsert case
                else: {
                    $add: [
                        "$value",
                        1
                    ]
                }
            }
        },

    }
}], {
    // Mongo Options
    returnDocument: 'after',
    name: options.name,
    upsert: true,
    multi: true
});

You will be able to achieve that with this, you're missing [] that will help you to use the aggregation framework, then you should have this

You will be able to achieve that with this, you 're missing [] that will help you to use the aggregation framework, then you should have this

const result = await context.params.tenantDb.collection('counters').findOneAndUpdate({
    // Filter
    name: options.name,
}, [{
    $set: {
        value: {
            $cond: {
                if: {
                    $eq: [{
                            $type: "$value"
                        },
                        "missing"
                    ]
                },
                then: 1000,
                // it's the upsert case
                else: {
                    $add: [
                        "$value",
                        1
                    ]
                }
            }
        },

    }
}], {
    // Mongo Options
    returnDocument: 'after',
    name: options.name,
    upsert: true,
    multi: true
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文