How to select only distinct values from MongoDB?

Good day!
Suppose that there is such a collection:
[{
 "task": 1,
 "creator": 1,
 "progress" : 0,
}, {
 "task": 1,
 "creator": 1,
 "progress" : 0.3,
}, {
 "task": 1,
 "creator": 1,
 "progress" : 0.7,
}, {
 "task": 2,
 "creator": 1,
 "progress" : 0.1,
}, {
 "task": 2,
 "creator": 1,
 "progress" : 0.5,
}, {
 "task": 2,
 "creator": 1,
 "progress" : 0.9,
}]


ie we have the user ID, task ID and its progress.

The task is the following: select only the last (unique) advances for each task to a particular user.
I.e. the output should be:
[{
 "task": 1,
 "creator": 1,
 "progress" : 0.7,
}, {
 "task": 2,
 "creator": 1,
 "progress" : 0.9,
}]


Now I tried to do
const uniqTasks = Task.find({
 creator: 1
})
.distinct('task')
 .exec();


Get this dictionary, and then the findOne + sort({_id: -1}) to Promise.all.

Promise.all(uniqTasks.map(task) => {
 return Task.findOne({
 creator: 1,
 task: task
})
 .sort({_id: -1})
.exec();
});


As you know, this is N parallel queries to the database, ie, to put it mildly the bad code. How can I aggregate these requests, what would the output have the same result?
July 9th 19 at 11:03
1 answer
July 9th 19 at 11:05
Do not store duplicates and all.
this is a capped collection - kenton.Klein commented on July 9th 19 at 11:08
: what difference does it make?

The challenge is the following: select only the last (unique) advances for each task to a particular user.

If I correctly understood the task, the "obsolete" documents are not necessary at all, so it is better to rewrite, and not to produce.

I would do a unique index on (creator + task), in the end, only one team update-upsert that would update (add if not present) value, and only one find command to get required data, the extra "duplicates" do not take up memory. - Bianka0 commented on July 9th 19 at 11:11
> If I correctly understood the task, the "obsolete" documents are not necessary at all, so it is better to rewrite, and not to produce.

Capped, to rewrite. It's an oxymoron. You may not modify, capped collection.

> the extra "duplicates" do not take up memory.
Capped. They do not take. It's first in, last out limited collection. - kenton.Klein commented on July 9th 19 at 11:14
: divine answer "then why use Capped, use conventional and perezapisi". So do not come out as in this collection I need to be able to open a Stream for Pub / Sub - kenton.Klein commented on July 9th 19 at 11:17
:
> You can't modify a capped collection.
I can:

> db.createCollection("log", { capped : true, size : 5242880, max : 5000 } )
{ "ok" : 1 }
> db.log.insert({name: 'linux'})
WriteResult({ "nInserted" : 1 })
> db.log.find()
{ "_id" : ObjectId("574a8bf4bf0004e83b3c7d55"), "name" : "linux" }
> db.log.update({"_id" : ObjectId("574a8bf4bf0004e83b3c7d55")}, {$set:{name:'ubuntu'}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.log.find()
{ "_id" : ObjectId("574a8bf4bf0004e83b3c7d55"), "name" : "ubuntu" }

> foretell response
No, first you can rewrite it (with nuances), and secondly if you need Pub / Sub, then use a suitable tool for this (zmq, rabbitmq,... at worst, redis) - Bianka0 commented on July 9th 19 at 11:20
strange, Mongo 3+ says that the capped - invariant. "Adult" pub / sub is not suitable, I need the ability to filter the queue and to look for more complex criteria than queue in Rabbit. - kenton.Klein commented on July 9th 19 at 11:23
besides (what was the question) I should be given the opportunity to request the latest results for each task, not only to subscribe to new events. Everything is ancient leading me queue disappear, as they can notify only about the new opening event, but do not provide the ability to request old. (there is even a Zero can be set up so, I'm sure it will play in sample rate, Mongo, and will compete in costimizable with the current solution) - kenton.Klein commented on July 9th 19 at 11:26

Find more questions by tags MongooseNode.jsMongoDBNoSQL