Answer the question
In order to leave comments, you need to log in
How to merge collections and get only the first entry in the array?
Is it possible to implement everything like this?
There are two collections
First: Collections
var schema = new Schema({
name: { type: String },
owner: { type: mongoose.Schema.Types.ObjectId, ref: 'Users' }, // владелец
photos: [{ type: mongoose.Schema.Types.ObjectId, ref: 'Photos' }],
public: { type: Number, default: 1 },
createdAt: { type: Date, default: Date.now }
})
var schema = new Schema({
createdAt: { type: Date, default: Date.now },
owner: { type: mongoose.Schema.Types.ObjectId, ref: 'Users', required: !0 },
image: { type: String, required: !0 },
domain: { type: String, default: config.domain.photos },
});
Collections.aggregate([
{ $match: { owner: request.owner } },
{ $sort: { createdAt: -1 } },
{ $lookup: { from: 'photos', localField: 'photos', foreignField: '_id', as: 'photos' } },
{
$project: {
_id: { $toString: "$_id" },
name: 1,
photos: {
"$map": {
input: "$photos",
as: "p",
in: { $concat: ['$$p.domain', 'folder/', '$$p.image'] } }
}
}
}
])
{
name: 'Папка',
_id: '5d557777ab6666e77777e',
photos: [
'https://site/folder/img.jpg',
'https://site/folder/img2.jpg''
]
}
{
name: 'Папка',
_id: '5d557777ab6666e77777e',
photos: 'https://site/folder/img.jpg',
}
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