本文介绍了过滤公式中的字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要过滤包含条件中的集合字段的集合.

I want to filter a collection including collection's fields in the criteria.

- myCollection
|- name (string)
|- field1 (int)
|- field2 (int)

我想获取field1> field2的所有名称.我认为它可以这样工作:

I want to get all names where field1 > field2. I thought it could work this way:

db.collection('myCollection').where('field1','>','field2')
  .get().then(function(snapshot){
    //doing something
  });

但是,它被理解为field1>"field2",而不是field1> field2.

However, it was understood as field1 > 'field2', instead field1 > field2.

我想走得更远,做类似field1 + field2< 1000的事情,但是以下两个查询都没有结果

I would like to go further, doing something like field1+field2<1000, but both of following queries got no result

db.collection('myCollection').where('field1'+'field2','<',1000)
  .get().then(function(snapshot){
    //doing something
  });

db.collection('myCollection').where('field1+field2','<',1000)
  .get().then(function(snapshot){
    //doing something
  });

谢谢

推荐答案

无法在表达式的 value 中指定字段引用.

There is no way to specify field references in the value of an expression.

我能想到的最好的方法是将field2 - field1的值存储在单独的字段中,以便可以将其与常量进行比较.例如.如果将字段2和字段1的增量存储在delta_field2_field2中,则会得到以下查询:

The best approach I can think of is to store the value of field2 - field1 in a separate field, so that you can compare that against a constant. E.g. if you store the delta of field2 and field 1 in delta_field2_field2, you'd get this query:

db.collection('myCollection').where('delta_field2_field2','<', 0)

然后,如果将字段1和2的总和存储在sum_field1_field2中,则会得到:

Then if you store the sum of field 1 and 2 in sum_field1_field2 you'd get:

db.collection('myCollection').where('sum_field1_field2','<',1000)

您将看到这是NoSQL数据库中的一个共同主题:如果所需的运算符不是内置的,则通常可以在模型中添加数据以适应用例.

You'll see this is a common theme across NoSQL databases: if an operator you want isn't built-in, you can often add data to your model to allow for the use-case.

这篇关于过滤公式中的字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-10 23:14