0

I'm using aggregate in order to get some documents ( I can't use find that way). I used to use the .count() function to know how many documents I got. This function isn't working with aggregate. When I tried $count I just got the count without all the documents. What else can I do?

shr7
  • 163
  • 1
  • 2
  • 6
  • Does [this](https://stackoverflow.com/questions/72838497/getting-count-of-total-documents-when-using-aggregation-along-with-skip-and-limi/72838622#72838622) answer your question? – nimrod serok Jul 18 '22 at 10:21
  • Does this answer your question? [Using "$count" Within an "addField" Operation in MongoDB Aggregation](https://stackoverflow.com/questions/51029987/using-count-within-an-addfield-operation-in-mongodb-aggregation) – nimrod serok Jul 18 '22 at 10:23
  • Using ```$count``` return an object with the count, but not the documents it counted. – shr7 Jul 25 '22 at 13:43
  • It is if used inside `$facet` or `$setWindowFields` – nimrod serok Jul 25 '22 at 14:48
  • It's giving me the same result. I guess there's another way to use it – shr7 Jul 26 '22 at 15:24

1 Answers1

0

try .count_documents or .. Does this answer your question? Just like this

pipeline = [
    {'$group': {'_id':None, 'count': {'$sum':1}}}
]
nums = m_collection.aggregate(pipeline)
print(list(nums))
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jul 21 '22 at 05:56
  • Adding ```$group``` return an object with the count, but not the documents it counted. Any other function for counting is not working – shr7 Jul 25 '22 at 13:41