Discussion:
[mongodb-user] find only matching subdocument from array
Monika Shah
2017-10-02 12:14:15 UTC
Permalink
What is equivalent query in Mongo for
Select only those grades subdocument from restaurant sample collection
which match some criteria i.e. score>10
Note: One restaurant may have many grade subdocument with satisfied criteria
--
You received this message because you are subscribed to the Google Groups "mongodb-user"
group.

For other MongoDB technical support options, see: https://docs.mongodb.com/manual/support/
---
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mongodb-user+***@googlegroups.com.
To post to this group, send email to mongodb-***@googlegroups.com.
Visit this group at https://groups.google.com/group/mongodb-user.
To view this discussion on the web visit https://groups.google.com/d/msgid/mongodb-user/cb58a741-556a-496b-9fbc-fac2c2d286a3%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Monika Shah
2017-10-02 12:37:34 UTC
Permalink
db.restaurants.find({"grades.score":{$gt:10}},{_id:0,grades:{$elemMatch:{score:{$gt:10}}}}).pretty()
Post by Monika Shah
What is equivalent query in Mongo for
Select only those grades subdocument from restaurant sample collection
which match some criteria i.e. score>10
Note: One restaurant may have many grade subdocument with satisfied criteria
--
You received this message because you are subscribed to the Google Groups "mongodb-user"
group.

For other MongoDB technical support options, see: https://docs.mongodb.com/manual/support/
---
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mongodb-user+***@googlegroups.com.
To post to this group, send email to mongodb-***@googlegroups.com.
Visit this group at https://groups.google.com/group/mongodb-user.
To view this discussion on the web visit https://groups.google.com/d/msgid/mongodb-user/4e345a69-0415-4c1e-a2ff-cfd9a4e0756f%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Monika Shah
2017-10-02 13:28:35 UTC
Permalink
This query displays only one matching element from subdocument array. How
to display only all matching elements from array ?
Post by Monika Shah
db.restaurants.find({"grades.
score":{$gt:10}},{_id:0,grades:{$elemMatch:{score:{$gt:10}}}}).pretty()
Post by Monika Shah
db.restaurants.find({"grades.score":{$gt:10}},{_id:0,grades:{$elemMatch:{score:{$gt:10}}}}).pretty()
Post by Monika Shah
What is equivalent query in Mongo for
Select only those grades subdocument from restaurant sample collection
which match some criteria i.e. score>10
Note: One restaurant may have many grade subdocument with satisfied criteria
--
You received this message because you are subscribed to the Google Groups "mongodb-user"
group.

For other MongoDB technical support options, see: https://docs.mongodb.com/manual/support/
---
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mongodb-user+***@googlegroups.com.
To post to this group, send email to mongodb-***@googlegroups.com.
Visit this group at https://groups.google.com/group/mongodb-user.
To view this discussion on the web visit https://groups.google.com/d/msgid/mongodb-user/b86b22e7-8765-48e7-b763-3f35a864e5d7%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Tran Quan Dinh
2018-11-18 15:43:06 UTC
Permalink
Can you help me to convert this query to java.
Thanks.
Post by Monika Shah
db.restaurants.find({"grades.score":{$gt:10}},{_id:0,grades:{$elemMatch:{score:{$gt:10}}}}).pretty()
Post by Monika Shah
What is equivalent query in Mongo for
Select only those grades subdocument from restaurant sample collection
which match some criteria i.e. score>10
Note: One restaurant may have many grade subdocument with satisfied criteria
--
You received this message because you are subscribed to the Google Groups "mongodb-user"
group.

For other MongoDB technical support options, see: https://docs.mongodb.com/manual/support/
---
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mongodb-user+***@googlegroups.com.
To post to this group, send email to mongodb-***@googlegroups.com.
Visit this group at https://groups.google.com/group/mongodb-user.
To view this discussion on the web visit https://groups.google.com/d/msgid/mongodb-user/501b5277-d3d6-4dd6-b4b7-4fe3301d13ac%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
'Kevin Adistambha' via mongodb-user
2017-10-04 01:29:08 UTC
Permalink
Hi,

The $elemMatch projection has the limitation that it will only project the
first matching element. This is mentioned in the $elemMatch projection
documentation page
<https://docs.mongodb.com/manual/reference/operator/projection/elemMatch/>.

To display all matching elements, you need to use the Aggregation framework
<https://docs.mongodb.com/manual/aggregation/>.

You didn’t give an example document you’re trying to match, but I assume
it’s similar to this:

{
"name": "Restaurant A",
"grades": [
{
"reviewer": "a",
"score": 10
},
{
"reviewer": "b",
"score": 10
},
{
"reviewer": "c",
"score": 9
}
]
}

The aggregation stages you can use depends on your MongoDB version. If
you’re using MongoDB 3.4, you can use the new $addFields stage:

db.restaurants.aggregate([
// match the relevant document
{$match:{'grades.score':{$gte: 10}}},
// filter out irrelevant array entries
{$addFields: {
top_reviews: {
$filter: {
input: '$grades',
cond: {
$gte: ['$$this.score', 10]
}
}
}
}},
// project only the relevant fields
{$project: {_id: 0, grades: 0}}
])

or if you’re using older versions of MongoDB, you would need to use the
relatively less-performant $unwind stage:

db.restaurants.aggregate([
// match relevant documents
{$match: {'grades.score': {$gte: 10}}},
// unwind the grades array
{$unwind: '$grades'},
// match the relevant unwound array element
{$match: {'grades.score': {$gte: 10}}},
// re-group the array elements
{$group: {
_id: '$name',
top_reviews: {$push: '$grades'}
}}
])

Either way, both methods should result in:

{ "name": "Restaurant A", "top_reviews": [ { "reviewer": "a", "score": 10 }, { "reviewer": "b", "score": 10 } ] }

Here are the relevant documentations regarding the aggregation stages:

- $match
<https://docs.mongodb.com/manual/reference/operator/aggregation/match/>
- $addFields
<https://docs.mongodb.com/manual/reference/operator/aggregation/addFields/>
- $filter
<https://docs.mongodb.com/manual/reference/operator/aggregation/filter/>
- $unwind
<https://docs.mongodb.com/manual/reference/operator/aggregation/unwind/>
- $group
<https://docs.mongodb.com/manual/reference/operator/aggregation/group/>

Best regards,
Kevin
​
--
You received this message because you are subscribed to the Google Groups "mongodb-user"
group.

For other MongoDB technical support options, see: https://docs.mongodb.com/manual/support/
---
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mongodb-user+***@googlegroups.com.
To post to this group, send email to mongodb-***@googlegroups.com.
Visit this group at https://groups.google.com/group/mongodb-user.
To view this discussion on the web visit https://groups.google.com/d/msgid/mongodb-user/830ca90c-77b7-4e14-a80f-266195a52e8a%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Monika Shah
2017-10-12 16:24:29 UTC
Permalink
Thank you for proper answer.
But, why to specify filter condition i.e. score >10 mutiple times?
Post by Monika Shah
What is equivalent query in Mongo for
Select only those grades subdocument from restaurant sample collection
which match some criteria i.e. score>10
Note: One restaurant may have many grade subdocument with satisfied criteria
--
You received this message because you are subscribed to the Google Groups "mongodb-user"
group.

For other MongoDB technical support options, see: https://docs.mongodb.com/manual/support/
---
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mongodb-user+***@googlegroups.com.
To post to this group, send email to mongodb-***@googlegroups.com.
Visit this group at https://groups.google.com/group/mongodb-user.
To view this discussion on the web visit https://groups.google.com/d/msgid/mongodb-user/c60bfc37-eca1-4c4a-9229-869812a00fe9%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
'Kevin Adistambha' via mongodb-user
2017-10-17 02:20:36 UTC
Permalink
Hi

But, why to specify filter condition i.e. score >10 mutiple times?

I believe you are referring to the $unwind example. This is due to how
$unwind works, i.e. it’s unwinding the array into separate documents. Hence
the first $match is to filter irrelevant documents, and the second $match
is to filter out irrelevant unwound array entries.

Please see the $unwind documentation page
<https://docs.mongodb.com/manual/reference/operator/aggregation/unwind/>.

Best regards,
Kevin
​
--
You received this message because you are subscribed to the Google Groups "mongodb-user"
group.

For other MongoDB technical support options, see: https://docs.mongodb.com/manual/support/
---
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mongodb-user+***@googlegroups.com.
To post to this group, send email to mongodb-***@googlegroups.com.
Visit this group at https://groups.google.com/group/mongodb-user.
To view this discussion on the web visit https://groups.google.com/d/msgid/mongodb-user/fdd36c63-01b0-43b6-a0fe-759f8bd04e30%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
'Kevin Adistambha' via mongodb-user
2018-11-27 03:48:04 UTC
Permalink
Hi Tran

Can you help me to convert this query to java.

I’m not certain what query you want to convert to Java. In general, the
Java driver syntax follows the mongo shell syntax quite closely. See MongoDB
Driver Quick Start
<http://mongodb.github.io/mongo-java-driver/3.8/driver/getting-started/quick-start/>
for some examples.

If you need more help, could you open a new thread with more details, e.g.
your Java driver version and the query you’re having issues with.

Best regards,
Kevin
​
--
You received this message because you are subscribed to the Google Groups "mongodb-user"
group.

For other MongoDB technical support options, see: https://docs.mongodb.com/manual/support/
---
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mongodb-user+***@googlegroups.com.
To post to this group, send email to mongodb-***@googlegroups.com.
Visit this group at https://groups.google.com/group/mongodb-user.
To view this discussion on the web visit https://groups.google.com/d/msgid/mongodb-user/f1bc64c9-b6a6-4fe0-9d1e-ebcea6725d51%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Loading...