0

Is there any way in monogdb to group all value with different key. Below is my data structure:

data structure

So my question is how can i count all 0/0 0/1 occurrence times? I have googled for a while and all answers i got with the same key. but you can see my keys inside samples are not the same. So any help will be appreciated.

1 Answer 1

1

You can try below aggregation in 3.4.

Use $objectToArray to transform object to array of key value pairs followed by $unwind and $group on value to count occurrences.

db.col.aggregate([
  {"$addFields":{"samples":{"$objectToArray":"$samples"}}},
  {"$unwind":"$samples"},
  {"$group":{"_id":"$samples.v","count":{"$sum":1}}}
])
Sign up to request clarification or add additional context in comments.

2 Comments

thanks, but here i can not use objectToArray function, whether is there any other way to do that?
Np. I'm afraid there is not a way on server side. You have to do it client side. Can you change structure to store the values as array ? like samples:[{k:b56, v:0/1}, ....]

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.