I'm currently failing to build a mongoDB query that uses first a sort and then outputs one sample. Documents in the database look like this:
{
"_id" : "cynthia",
"crawled" : false,
"priority" : 2.0
}
I'm trying to achieve the following: Get me one random element with the highest priority.
I tested it with the following query:
db.getCollection('profiles').aggregate([
{$match: {crawled: false }},
{$sort: {priority: -1}},
{$sample: {size: 1}}
])
Unfortunately, this is not working. Mongo seems to totally ignore the $sort. I see no difference between using with $sort or not.
Does anybody with more mongoDB experience has an idea on that? If you have an idea of a better implementation of the "priority" feature just tell me.
Every idea is highly appreciated.
$limitbut had the wrong keyword. So that's clearly not what you were asking. I'm asking you if you actually intend "one" to be random then to actually explain how priority should apply. It's really unclear what you expect and the language isn't coming through clearly either. I think if you could show us from a selection of 5 to 10 sample documents what you would expect to happen. If you could basically "draw that" by showing the documents and what you expect to happen, then it would be very clear for everyone.$limitand afterwards a$sampleis doing exactly what we expected. It's just important to have indexing enabled on the fields used. Thank you all :)