Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.6k views
in Technique[技术] by (71.8m points)

mongodb - How to use $elemMatch on aggregate's projection?

This is my object:

{ "_id" : ObjectId("53fdcb6796cb9b9aa86f05b9"), "list" : [ "a", "b" ], "complist" : [ { "a" : "a", "b" : "b" }, { "a" : "c", "b" : "d" } ] }

And this is what I want to accomplish: check if "list" contains a certain element and get only the field "a" from the objects on "complist" while reading the document regardless of any of these values. I'm building a forum system, this is the query that will return the details of a forum. I need to read the forum information while knowing if the user is in the forum's white list.

With a find I can use the query

db.itens.find({},{list:{$elemMatch:{$in:["a"]}}})

to get only the first element that matches a certain value. This way I can just check if the returned array is not empty and I know if "list" contains the value I'm looking for. I can't do it on the query because I want the document regardless of it containing the value I'm looking for in the "list" value. I need the document AND know if "list" has a certain value.

With an aggregate I can use the query

db.itens.aggregate({$project:{"complist.a":1}})

to read only the field "a" of the objects contained in complist. This is going to get the forum's threads basic information, I don't want all the information of the threads, just a couple of things.

But when I try to use the query

db.itens.aggregate({$project:{"complist.b":1,list:{$elemMatch:{$in:["a"]}}}})

to try and do both, it throws me an error saying the operator $elemMatch is not valid.

Am I doing something wrong here with the $elemMatch in aggregate? Is there a better way to accomplish this?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Quite on old question but literally none of the proposed answers are good.


TLDR:

You can't use $elemMatch in a $project stage. but you can achieve the same result using other aggregation operators like $filter.

db.itens.aggregate([
    {
        $project: {
            compList: {
               $filter: {
                input: "$complist",
                as: "item",
                cond: {$eq: ["$$item.a", 1]}
               }
            }
        }
    }
])

And if you want just the first item from the array that matches the condition similarly to what $elemMatch does you can incorporate $arrayElemAt


In Depth Explanation:

First let's understand $elemMatch:

$elemMatch is a query expressions while also this projection version of it exists this refers to a query projection and not $project aggregation stage.

So what? what does this have to do with anything? well a $project stage has certain input structure it can have while the one we want to use is:

<field>: <expression>

What is a valid expression?

Expressions can include field paths, literals, system variables, expression objects, and expression operators. Expressions can be nested.

So we want to use an expression operator, but as you can see from the doc's $elemMatch is not part of it. hence it's not a valid expression to be used in an aggregation $project stage.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...