mongoDB 中的嵌套对象文本搜索 [英] Nested object text search in mongoDB

查看:54
本文介绍了mongoDB 中的嵌套对象文本搜索的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不确定我将如何解决这个问题:
我想在 mongoDB 集合中搜索并仅返回适合搜索查询的嵌套对象(对所有字段使用文本搜索).

集合中的所有文档都具有以下格式:

<代码>{阿尔:[{_id: 1,name: '随机',描述:'你好世界'},{_id: 2,name: '世界',description: '这是一个随机描述'},{_id: 3,name: '随机',描述:'嗨'}]}

在这种情况下,如果我的搜索查询是world",那么结果应该是:

<预><代码>[{_id: 1,name: '随机',描述:'你好世界'},{_id: 2,name: '世界',description: '这是一个随机描述'},//... 集合中适合查询的其他文档的对象]

如果这在 mongoDB 中是不可能的,是否有任何 JavaScript 库可以实现这一点?非常感谢您的帮助!

解决方案

有了聚合框架,它可能看起来像这样

db.getCollection('yourCollection').aggregate([{$unwind: '$arr'},{$匹配:{$或:[{ 'arr.name':/world/i },{ 'arr.description':/world/i }]}},{$项目:{_id: '$arr._id',name: '$arr.name',描述:'$arr.description'}}])

这将为您的示例数据产生以下输出:

<代码>{_id":1,"name" : "随机",描述":你好世界"}{_id":2,"name": "世界","description" : "这是一个随机的描述"}

如果您需要一个带有结果文档的数组,如您的问题所示,您可以简单地在管道末尾链接一个 toArray() 调用 - 但请记住正如 SSDMS 在评论中指出的那样,这可能会导致在大结果集的情况下增加内存消耗.

I am not sure how I am going to solve this problem:
I want to search in a mongoDB collection and return only the nested objects that fits the search query (using text search on all of the fields).

All documents in the collection have this format:

{
  arr: [
    {
      _id: 1,
      name: 'Random',
      description: 'Hello world'
    },
    {
      _id: 2,
      name: 'World',
      description: 'This is a random description'
    },
    {
      _id: 3,
      name: 'Random',
      description: 'Hi'
    }
  ]
}

In this case, if my search query is 'world', then this should be the result:

[
  {
    _id: 1,
    name: 'Random',
    description: 'Hello world'
  },
  {
    _id: 2,
    name: 'World',
    description: 'This is a random description'
  },
  //... objects from other documents in the collection that fits the query
]

If this is not possible in mongoDB, are there any JavaScript libraries that can achieve this? Would greatly appreciate the help!

解决方案

With the aggregation framework it could look like so

db.getCollection('yourCollection').aggregate([
    {
        $unwind: '$arr'
    },
    {
        $match: {
            $or: [
                { 'arr.name': /world/i },
                { 'arr.description': /world/i }
            ]
        }
    },
    {
        $project: {
            _id: '$arr._id',
            name: '$arr.name',
            description: '$arr.description'
        }
    }
])

which will result in the following output for your example data:

{
    "_id" : 1,
    "name" : "Random",
    "description" : "Hello world"
}
{
    "_id" : 2,
    "name" : "World",
    "description" : "This is a random description"
}  

If you have the need for a single array with the resulting documents as shown in your question, you can simply chain a toArray() call at the end of the pipeline - keep in mind though that this may cause increased memory consumption in case of large result sets as pointed out by SSDMS in the comments.

这篇关于mongoDB 中的嵌套对象文本搜索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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