gpt4 book ai didi

mongodb - 如何选择字段总和大于MongoDB中的值的位置

转载 作者:可可西里 更新时间:2023-11-01 09:59:05 24 4
gpt4 key购买 nike

使用 MongoDB,我将如何编写此常规 SQL 语句?

SELECT * FROM table WHERE (field1+field2+field3) > 1

我一直在搞乱 $group、$project、$add 等。我觉得我在围绕解决方案跳舞,但无法弄清楚。

最佳答案

最简单的方法是使用 $where (我并不是说不能通过聚合来做到这一点)

db.table.find({$where: function() {
return this.field1 + this.field2 + this.field3 > 1
// most probably you have to handle additional cases if some of the fields do not exist.
}}

它的优点是简单直观,而缺点:

requires that the database processes the JavaScript expression or function for each document in the collection.

如果您需要经常执行此类搜索,我会继续创建一个新字段,其中将存储 3 个字段的总和并在其上放置一个索引。缺点是您必须增加应用逻辑。

关于mongodb - 如何选择字段总和大于MongoDB中的值的位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29860348/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com