查询查找MongoDB中的子文档

时间:2022-05-20 16:51:35

Alright, i've got a simple question. I have a simple Document in MongoDB which holds a sub-document called "penalties". Now i want to find the Document (here with the _id "Cammeritz") by a String in the sub-document ("penalties"), e.g. "penaltyid = 0f77d885-6597-3f47-afb1-0cee2ea3ece1". Can you maybe help me? Best would be an explanation for Java but it is okay if you maybe just help with a normal MongoDB query.

好吧,我有个简单的问题。我在MongoDB中有一个简单的文档,它包含一个名为“点球”的子文档。现在,我希望通过子文档中的字符串(“惩罚”)查找文档(这里有_id“Cammeritz”)。“penaltyid = 0 f77d885 - 6597 - 3 - f47 afb1 - 0 - cee2ea3ece1”。你能帮我一下吗?最好是对Java进行解释,但是如果您只是帮助进行常规的MongoDB查询,那么这是可以的。

{
    "_id" : "Cammeritz",
    "penalties" : [ 
        {
            "_id" : null,
            "date" : ISODate("2017-09-25T20:01:23.582Z"),
            "penaltyid" : "0f77d885-6597-3f47-afb1-0cee2ea3ece1",
            "reason" : "Hacking",
            "teammember" : "Luis",
            "type" : "ban"
        }, 
        {
            "_id" : null,
            "date" : ISODate("2017-09-25T20:01:23.594Z"),
            "penaltyid" : "7f5411b0-e66a-33b3-ac4f-4f3159aa88a9",
            "reason" : "Spam",
            "teammember" : "BluingFX",
            "type" : "kick"
        }
    ],
    "isBanned" : true,
    "isMuted" : false
}

1 个解决方案

#1


2  

Oops, I misread your question. You'll need to use dot notation. db.collection.find( { penalties.penaltyid: '0f77d885-6597-3f47-afb1-0cee2ea3ece1' } ) For more info see Query on a Nested Field.

我看错你的问题了。你需要使用点符号。db.collection。找到({处罚。penaltyid: '0f77d885-6597-3f47-afb1-0cee2ea3ece1'})更多信息请参见嵌套字段的查询。


Original answer:

最初的回答:

db.collection.find( { penalties: "0f77d885-6597-3f47-afb1-0cee2ea3ece1" } ) should work. For more see Query an Array for an Element from the mongodb docs. I'm not very familiar with Java so I can't help much there.

db.collection。查找(惩罚{“0f77d885-6597-3f47-afb1- 0cee2ea3e1”})应该有效。要了解更多,请从mongodb文档中查询一个元素的数组。我对Java不是很熟悉,所以在那里我帮不了什么忙。

#1


2  

Oops, I misread your question. You'll need to use dot notation. db.collection.find( { penalties.penaltyid: '0f77d885-6597-3f47-afb1-0cee2ea3ece1' } ) For more info see Query on a Nested Field.

我看错你的问题了。你需要使用点符号。db.collection。找到({处罚。penaltyid: '0f77d885-6597-3f47-afb1-0cee2ea3ece1'})更多信息请参见嵌套字段的查询。


Original answer:

最初的回答:

db.collection.find( { penalties: "0f77d885-6597-3f47-afb1-0cee2ea3ece1" } ) should work. For more see Query an Array for an Element from the mongodb docs. I'm not very familiar with Java so I can't help much there.

db.collection。查找(惩罚{“0f77d885-6597-3f47-afb1- 0cee2ea3e1”})应该有效。要了解更多,请从mongodb文档中查询一个元素的数组。我对Java不是很熟悉,所以在那里我帮不了什么忙。