根据数组中的条件从数组中获取不同的值 [英] Get distinct values from array based on conditions within the array

查看:64
本文介绍了根据数组中的条件从数组中获取不同的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含以下文档的集合:

I have a collection with documents like this:

{
"_id" : ObjectId("59df91dae4b03289d79efb4e"),
  "name" : "TEST",
  "payload" : [ 
    {
      "_id" : "ABC",
      "status": "FALSE"
    },
    {
      "_id":"DEF",
     "status": "TRUE"
    },
    {
      "_id" : "XYZ",
     "status": "NULL"
    }
  ]
}

我正在尝试查找状态为TRUE/FALSE的所有payload._id.预期结果是

I am trying to find all the payload._id which have status as TRUE/FALSE. Expected result is

["ABC","DEF"]

到目前为止,我有这样的事情.

So far, I have something like this.

db.collection.distinct('payload._id',{"_id" : "TEST"})

不确定如何为数组中的元素添加条件.

Not sure how to add conditions for elements within array.

推荐答案

使用 .aggregate() 以及一些后期处理以仅获取数组响应中的值".

Query conditions with .distinct() apply to "document selection" and not the array entries contained "within" the document. If you need to "filter" array content then you apply .aggregate() instead, as well as a little post processing to get only the "values" in the array response.

db.collection.aggregate([
  { "$match": { "_id": "TEST" } },
  { "$unwind": "$payload" },
  { "$match": { "payload.status": { "$in": ["TRUE","FALSE"] } } },
  { "$group": { "_id": "$payload._id" } },
]).map( d => d._id );

其中的主要部分是 $ unwind > 流水线阶段,主要是因为您希望以后将数组中的值用作

The main parts there are the $unwind pipeline stage which you do primarily because you want the values from within the array to use later as the key to $group on. This essentially produces a new document for each array member, but each document only contains that array member. It's "denormalizing" for MongoDB structures that contain arrays.

下一件事是以下 $ match > 管道,它与任何查询一样工作,并且仅选择符合条件的文档.由于所有数组成员现在都是文档",因此不匹配的条目(作为文档)将被排除在外.您可以选择使用 $ filter 提取数组,但由于我们需要 $ unwind 在下一阶段,我们也可以简单地 $ match .

The next thing is the following $match pipeline, which works like any query and only selects documents that match the conditions. Since all array members are now "documents" then non matching entries ( as documents ) get excluded. You could alternately use $filter to extract whilst still an array, but since we need $unwind for the next stage we may as well simply $match.

这时,只剩下符合条件的数组条目. $ group 是为了获得不同"的值,因此通常您会选择更广泛的范围,而不仅仅是单个文档或此处的值尚未完全不同的任何内容.因此,这实际上只是保持 的所有相同行为.

At this point you are only left with the array entries that match the conditions. The $group is to obtain "distinct" values, so typically you would do this over a wider selection than just a single document or anything where the values here are not already distinct. So this is really just keeping all the same behavior of .distinct() intact.

最后,由于 .aggregate() .distinct() ,因为它在结果中返回文档",我们只需使用

Finally, since the output of .aggregate() differs from the design of .distinct() in that it returns "documents" in results, we simply use the .map() method to process the cursor results and return only the "values" from the specific document property as an "array".

这篇关于根据数组中的条件从数组中获取不同的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆