如何在嵌套数组中查询。(使用pymongo)

发布于 2024-12-03 21:46:10 字数 1370 浏览 0 评论 0原文

我是 mongodb 的新手。 我制作了一个像这样的嵌套数组文档。

data = {
    "title": "mongo community",
    "description": "I am a new bee",
    "topics": [{
        "title": "how to find object in array",
        "comments": [{
            "description": "desc1"
        }]
    },
    {
        "title": "the case to use ensureIndex",
        "comments": [{
            "description": "before query"
        },
        {
            "description": "If you want"
        }
        ]
    }
    ]
}

之后,将其放入“社区” db.community.insert(data)

所以,我想积累“评论”,其主题标题是“如何在数组中查找对象” 然后我尝试,

<块引用>

data = db.community.find_one({"title":"mongo Community","topics.title":"如何在数组中查找对象" } )

结果是

>>> print data
{
    u 'topics': [{
        u 'comments': [{
            u 'description': u 'desc1'
        }],
        u 'title': u 'how to find object in array'
    },
    {
        u 'comments': [{
            u 'description': u 'before query'
        },
        {
            u 'description': u 'If you want'
        }],
        u 'title': u 'the case to use ensureIndex'
    }],
    u '_id': ObjectId('4e6ce188d4baa71250000002'),
    u 'description': u 'I am a new bee',
    u 'title': u 'mongo community'
}

我不需要主题“使用ensureIndex的情况“

你能给我什么建议吗?

谢谢。

I'm new bee in mongodb.
I made a nested array document like this.

data = {
    "title": "mongo community",
    "description": "I am a new bee",
    "topics": [{
        "title": "how to find object in array",
        "comments": [{
            "description": "desc1"
        }]
    },
    {
        "title": "the case to use ensureIndex",
        "comments": [{
            "description": "before query"
        },
        {
            "description": "If you want"
        }
        ]
    }
    ]
}

after that, put it in the "community"
db.community.insert(data)

so,I would like to accumulate "comments" which topics title is "how to find object in array"
then I tried,

data = db.community.find_one({"title":"mongo community","topics.title":"how to find object in array" } )

the result is

>>> print data
{
    u 'topics': [{
        u 'comments': [{
            u 'description': u 'desc1'
        }],
        u 'title': u 'how to find object in array'
    },
    {
        u 'comments': [{
            u 'description': u 'before query'
        },
        {
            u 'description': u 'If you want'
        }],
        u 'title': u 'the case to use ensureIndex'
    }],
    u '_id': ObjectId('4e6ce188d4baa71250000002'),
    u 'description': u 'I am a new bee',
    u 'title': u 'mongo community'
}

I don't need the topics "the case to use ensureIndex"

Whould you give me any advice.

thx.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

绝不服输 2024-12-10 21:46:10

看起来您正在将主题作为数组嵌入到单个文档中。您应该尽量避免频繁地从 MongoDB 返回部分文档。您可以使用 find 方法的“fields”参数来完成此操作,但如果您经常这样做,则不太容易使用。

因此,为了解决这个问题,您可以尝试将每个主题制作为单独的文档。我想这对你来说也会更容易。如果您想保存论坛“社区”的信息,请将其放入单独的集合中。例如,您可以在 monbodb shell 中使用以下内容:

// ad a forum:
var forum = { 
                title:"mongo community",
                description:"I am a new bee"
            };
db.forums.save(forum);

// add first topic:
var topic = {
                title: "how to find object in array",
                comments: [ {description:"desc1"} ],
                forum:"mongo community"
            };
db.topics.save(topic);

// add second topic:
var topic = {
                title: "the case to use ensureIndex",
                comments: [ 
                    {description:"before query"},
                    {description:"If you want"}
                ],
                forum:"mongo community"
            };
db.topics.save(topic);

print("All topics:");
printjson(db.topics.find().toArray());


print("just the 'how to find object in array' topic:")
printjson(db.topics.find({title:"how to find object in array"}).toArray());

另请参阅文档 Trees In MongoDB 关于 MongoDB 中的架构设计。它恰好使用与您正在使用的模式类似的模式,并针对更高级的用例对其进行了扩展。

It looks like you're embedding topics as an array all in a single document. You should try to avoid returning partial documents frequently from MongoDB. You can do it with the "fields" argument of the find method, but it isn't very easy to work with if you're doing it frequently.

So to solve this you could try to make each topic a separate document. I think that would be easier for you too. If you want to save information about the "community" for forum, put it in a separate collection. For example, you could use the following in the monbodb shell:

// ad a forum:
var forum = { 
                title:"mongo community",
                description:"I am a new bee"
            };
db.forums.save(forum);

// add first topic:
var topic = {
                title: "how to find object in array",
                comments: [ {description:"desc1"} ],
                forum:"mongo community"
            };
db.topics.save(topic);

// add second topic:
var topic = {
                title: "the case to use ensureIndex",
                comments: [ 
                    {description:"before query"},
                    {description:"If you want"}
                ],
                forum:"mongo community"
            };
db.topics.save(topic);

print("All topics:");
printjson(db.topics.find().toArray());


print("just the 'how to find object in array' topic:")
printjson(db.topics.find({title:"how to find object in array"}).toArray());

Also, see the document Trees In MongoDB about schema design in MongoDB. It happens to be using a similar schema to what you are working with and expands on it for more advanced use cases.

哑剧 2024-12-10 21:46:10

MongoDB 对文档进行操作,即顶级文档(您保存更新插入查找 和 find_one 上)。 Mongo 的查询语言允许您在嵌入对象中进行搜索,但始终会返回、更新或操作这些顶级文档中的一个(或多个)。

MongoDB 通常被称为“无模式”,但更准确的描述是“(具有)灵活模式”或“(具有)每个文档模式”。在这种情况下,您的模式设计(将主题直接嵌入社区中)不适用于该特定查询。但是,此模式可能更有效地支持其他查询,例如在单个查询中列出社区内的主题。您可能需要考虑要进行的查询并相应地重新设计您的架构。

关于 MongoDB 限制的一些注意事项:

  1. 始终返回顶级文档(可选地仅返回字段的子集,如 @scott 所指出的 - 请参阅 有关此主题的 mongodb 文档
  2. 每个文档的数据大小限制为 16 MB(从版本 1.8+ 开始),因此此模式将不起作用如果社区有一长串主题

,请参阅有关架构设计的 mongodb 文档架构设计Kyle Banker 的视频“架构设计基础知识”,以及 Eliot Horowitz 的视频视频“大规模架构设计”,了解介绍、提示和注意事项。

MongoDB operates on documents, that is, the top level documents (the things you save, update, insert, find, and find_one on). Mongo's query language lets you search within embedded objects, but will always return, update, or manipulate one (or more) of these top-level documents.

MongoDB is often called "schema-less," but something more like "(has) flexible schemas" or "(has) per-document schemas" would be a more accurate description. This is a case where your schema design -- having topics embedded directly within a community -- is not working for this particular query. However there are probably other queries that this schema supports more efficiently, like listing the topics within a community in a single query. You might want to consider the queries you want to make and re-design your schema accordingly.

A few notes on MongoDB limitations:

  1. top-level documents are always returned (optionally with only a subset of fields, as @scott noted -- see the mongodb docs on this topic)
  2. each document is limited to 16 megabytes of data (as of version 1.8+), so this schema will not work well if the communities have a long list of topics

For help with schema design, see the mongodb docs on schema design, Kyle Banker's video "Schema Design Basics", and Eliot Horowitz's video "Schema Design at Scale" for an introduction, tips, and considerations.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文