2

I want to extract value from array. right now i have

db.getCollection('typeE').distinct("list.name",{"list.name":"C"})

I have such structure

object(1)"list":[{"name":"A","value":10},{"name":"B","value":20},{"name":"C","value":50}]
object(2)"list":[{"name":"D","value":100},{"name":"E","value":70}]

I want to receive 50 , but right now receive only object(1)

I also tried

db.getCollection('typeE').distinct("list.value",{"list": {$elemMatch: {"name":"C"}}})

but it's return array

1 Answer 1

1

your doing this the wrong way. What you want (if I get you right) is to get distinct value of subdocuments having "name": "C"

you should try this :

db.getCollection('typeE').aggregate([
  {$unwind: "$list"},
  {$match: { "list.name": "C"}},
  {$group: {_id: null, distinctValue: {$addToSet: "$list.value"}}}
])

this will output:

{"_id": null, "distinctValue": [ 50 ] }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.