通过其字符串之一获取整个数组.数组在另一个数组内.MongoDB/Javascript [英] Get whole array by one of its strings. Array is inside another array. Mongodb/Javascript

查看:38
本文介绍了通过其字符串之一获取整个数组.数组在另一个数组内.MongoDB/Javascript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Mongodb 中,如果我有如下结构,并且我知道 string 让我们说 vid >0 >0 我怎样才能得到整个 vid >0 数组返回?这样我也可以访问 vid >0 >1vid >0 >2 ?

db 文档结构:

<预><代码>...更新于:2021-01-17T16:44:28.824+00:00视频:(数组)0:(数组)0:adfsdfasfd"1:这是一些示例文本";2:https://example.com"1:(数组)0:gfjghjhjgh"1:这是另一个示例文本";2:https://example2.com"...

所以这次关于上面的文本示例的问题是,如果我知道adfsdfasfd",如何获得整个数组 0?字符串?

解决方案

使用聚合查询:

  • $match 条件,嵌套 $ememMatch 用于 2 级条件
  • $reduce 迭代 vid 数组的循环,设置初始值 [],如果字符串在当前数组中,则检查条件然后返回当前数组,否则返回初始值

let searchString = "adfsdfasfd";db.collection.aggregate([{$匹配:{vid: { $elemMatch: { $elemMatch: { $in: [searchString] } } }}},{$addFields:{视频:{$减少:{输入:$vid",初始值: [],在: {$cond: [{ $in: [searchString, "$$this"] }, "$$this", "$$value"]}}}}}])

游乐场

结果:

<预><代码>[{视频":[adfsdfasfd",这是一些示例文本",https://example.com"]}]


使用查找查询:

  • 匹配条件同上
  • 用于投影获取匹配结果但在相同的嵌套数组中

let searchString = "adfsdfasfd";db.collection.find({ vid: { $elemMatch: { $elemMatch: { $in: [searchString] } } } }).project({ "vid.$": 1, _id: 0 }).toArray()

游乐场

结果:

<预><代码>[{视频":[[adfsdfasfd",这是一些示例文本",https://example.com"]]}]

In Mongodb if I have a structure like below, and I know the string let's say at vid > 0 > 0 how can I get the whole vid > 0 array returned? So that I can access also vid > 0 > 1 and vid > 0 > 2 ?

EDIT: db document structure:

...
updatedAt: 2021-01-17T16:44:28.824+00:00
vid: (Array)
  0: (Array)
    0: "adfsdfasfd"
    1: "this is some sample text"
    2: "https://example.com"
  1: (Array)
    0: "gfjghjhjgh"
    1: "this is another sample text"
    2: "https://example2.com"
...

So the question in regard to the text example above this time, is how do I get the whole Array 0 if I know the "adfsdfasfd" string?

解决方案

Using aggregation query:

  • $match condition, put nested $ememMatch for 2 level condition
  • $reduce to iterate loop of vid array, set initial [], check condition if string in current array then return current array otherwise return initial value

let searchString = "adfsdfasfd";
db.collection.aggregate([
  {
    $match: {
      vid: { $elemMatch: { $elemMatch: { $in: [searchString] } } }
    }
  },
  {
    $addFields: {
      vid: {
        $reduce: {
          input: "$vid",
          initialValue: [],
          in: {
            $cond: [{ $in: [searchString, "$$this"] }, "$$this", "$$value"]
          }
        }
      }
    }
  }
])

Playground

Result:

[
  {
    "vid": [
      "adfsdfasfd",
      "this is some sample text",
      "https://example.com"
    ]
  }
]


Using find query:

  • Put match condition same as above
  • for projection get match result but in same nested array

let searchString = "adfsdfasfd";
db.collection.find(
  { vid: { $elemMatch: { $elemMatch: { $in: [searchString] } } } }
).project({ "vid.$": 1, _id: 0 }).toArray()

Playground

Result:

[
  {
    "vid": [
      [
        "adfsdfasfd",
        "this is some sample text",
        "https://example.com"
      ]
    ]
  }
]

这篇关于通过其字符串之一获取整个数组.数组在另一个数组内.MongoDB/Javascript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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