Answer the question
In order to leave comments, you need to log in
How to get one document owned by each user?
There are two collections db.users and db.groups, when selecting, you need to get one user document (db.users)
For example, db.groups contains the following data
{
_id: id1,
idUser: 'ObjectID db.users._id' // String (сюда помещаю ID пользователя)
name: 'Название группы',
createdAt: 'Дата создания группы'
}
Groups
.aggregate([
{
$project: {
_id: 1,
name: 1,
createdAt: 1 // Дата создания группы
}
}, {
$match: {
idUser: {
$in: ['id1', 'id2', 'id3', 'id4'] // Получу все группу принадлежащие пользователям
}
}
}, {
$sort: {
createdAt: 1
}
},{
$group: {
_id: null,
documents: {
$push: {
name: '$name'
}
}
}
}
])........
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question