elemMatch搜索子文档数组

时间:2022-05-26 21:26:58

How to do search using elemMatch on array of SubDocument? I have document called ReportCollection with elements such as:-

如何在SubDocument数组上使用elemMatch进行搜索?我有一个名为ReportCollection的文档,其元素如下:

/* 0 */
{
    "_id" : ObjectId("5507bfc435e9470c9aaaa2ac"),
    "owner" : ObjectId("5507bfc31e14d78e177ceebd"),
    "reports" : {
        "xReport" : [ 
            {
                "name" : "xReport",
                "parameters" : {
                    "x" : {
                        "dateTime" : "2015-03-11T18:30:00.000Z",
                        "unit" : 1,
                        "value" : 102
                    }
                },
                "createdBy" : ObjectId("5507bfc31e14d78e177ceebd"),
                "modifiedBy" : ObjectId("5507bfc31e14d78e177ceebd"),
                "_id" : ObjectId("5507bfc41e14d78e177ceebf")
            }
        ]
    }
}

I got reports.xReport[]._id as search parameter.

我将reports.xReport [] ._ id作为搜索参数。

My following attempt is failing :-

我的以下尝试失败了: -

  db.reports.find ({ {owner: ObjectId("5507afd3d54bae3513c185cb")}, 
    { 'reports.xReport': {$elemMatch: {_id: ObjectId("5507afd3d54bae3513c185cd") }}} } )

1 个解决方案

#1


3  

It seems like you are trying to put work into the "projection" that you should be doing in the query. Rather your statement should look more like this:

您似乎正在尝试将工作放入您应该在查询中执行的“投影”中。相反,你的陈述看起来应该更像这样:

db.reports.find(
    {
        "owner": ObjectId("5507bfc31e14d78e177ceebd"),
        "reports.xReport._id": ObjectId("5507bfc41e14d78e177ceebf")
    },
    { "reports.xReport.$": 1 }
)

So the "query" does the work of matching the array position and the "projection" just uses that position in the match.

因此“查询”执行匹配数组位置的工作,而“投影”只是在匹配中使用该位置。

Also note that you never really need $elemMatch with a single field to match against. Only when multiple fields are required in the condition do you need to use this to match a specific "element".

另请注意,您永远不需要$ elemMatch与单个字段匹配。仅当条件中需要多个字段时,您才需要使用它来匹配特定的“元素”。

By the same token, that should also be in the "query" statement instead.

出于同样的原因,这也应该在“查询”语句中。

$elemMatch in it's projected form cannot be used with sub-document fields through dotted notation.

其中$ elemMatch的投影形式不能与子文档字段一起使用,也可以通过虚线表示法使用。

#1


3  

It seems like you are trying to put work into the "projection" that you should be doing in the query. Rather your statement should look more like this:

您似乎正在尝试将工作放入您应该在查询中执行的“投影”中。相反,你的陈述看起来应该更像这样:

db.reports.find(
    {
        "owner": ObjectId("5507bfc31e14d78e177ceebd"),
        "reports.xReport._id": ObjectId("5507bfc41e14d78e177ceebf")
    },
    { "reports.xReport.$": 1 }
)

So the "query" does the work of matching the array position and the "projection" just uses that position in the match.

因此“查询”执行匹配数组位置的工作,而“投影”只是在匹配中使用该位置。

Also note that you never really need $elemMatch with a single field to match against. Only when multiple fields are required in the condition do you need to use this to match a specific "element".

另请注意,您永远不需要$ elemMatch与单个字段匹配。仅当条件中需要多个字段时,您才需要使用它来匹配特定的“元素”。

By the same token, that should also be in the "query" statement instead.

出于同样的原因,这也应该在“查询”语句中。

$elemMatch in it's projected form cannot be used with sub-document fields through dotted notation.

其中$ elemMatch的投影形式不能与子文档字段一起使用,也可以通过虚线表示法使用。