0

Is it possible to check if a value of $count is equal to something in one query ?

something like:

{
  count: {...} : $eq : 5
}

eg: I manage a poll site and I want to query for all the questions that were answered positivly by at least 2 people. I dont have a field to hold how many people answered a question I need to $count for it on an array of objects, each answer holds a prop that marks if the it's positive (1) or negetive (0).

[
{questionId:1,
text:...,
answers: [{text, pos:1},{text, pos:0},{text, pos:0}....]},
{questionId:2,
text:...,
answers: [{text, pos:1},{text, pos:0},{text, pos:1}....]},
{questionId:3,
text:...,
answers: [{text, pos:0},{text, pos:1},{text, pos:1}....]},
{questionId:4,
text:...,
answers: [{text, pos:0},{text, pos:1},{text, pos:0}....]},
]

the result I expect is:

[{questionId:2, ...}, {questionId:3, ...}]
  • Not a clue what you are asking here. The only `$count` in MongoDB is actually an aggregation pipeline stage, and there is nothing to actually compare to. Perhaps you should show some data and what you expect to achieve instead. – Neil Lunn Aug 09 '17 at 08:21
  • Lets say I manage a poll site and I want to query for all the questions that were answered by more than 5 people. I dont have a field to hold how many people answered a question I need to `$count` for it. – Segev Lahav Aug 09 '17 at 08:27
  • Which means what? Please try and show a clear example in your question. Show some data, and show an expected result. That is usually universal and overcomes either language problems or lack of understanding terminology to explain. – Neil Lunn Aug 09 '17 at 08:30
  • I edited the question. – Segev Lahav Aug 09 '17 at 08:45
  • `.find({ "answers.5": { "$exists": true } }` would return documents with more than entries in an array. The question has been asked many times before. Adding a link. Basically query on the existence of the array index, which is `n-1`. – Neil Lunn Aug 09 '17 at 08:51
  • [Query for documents where array size is greater than 1](https://stackoverflow.com/questions/7811163/query-for-documents-where-array-size-is-greater-than-1) – Neil Lunn Aug 09 '17 at 08:53
  • I updated my question to a more specific one because i want to filter my array with an arbitrary filter before I count it. * keep in mind my array is within an object. – Segev Lahav Aug 09 '17 at 10:37

0 Answers0