[英]MongoDB/Node Lookup/match
Currently I have 3 Schemas: 目前,我有3个架构:
UserFavorite (This schema is supposed to be used to store the favorites the user has decided) UserFavorite (此架构应用于存储用户确定的收藏夹)
var UserFavoriteSchema = new Schema({
_id: {
type: Schema.ObjectId,
auto: true
},
favoriteId: {
type: Schema.ObjectId,
required: true
},
userId: {
type: Schema.ObjectId,
required: true
}
});
module.exports = mongoose.model('UserFavorite', UserFavoriteSchema);
Favorites (This schema holds the available favorites in the system) 收藏夹 (此架构保存系统中可用的收藏夹)
var FavoriteSchema = new Schema({
_id: {
type: Schema.ObjectId,
auto: true
},
storeId: {
type: String,
auto: true
},
name: {
type: String,
required: true
},
...
});
module.exports = mongoose.model('Favorite', FavoriteSchema);
User Schema 用户架构
var UserSchema = new Schema({
_id: {
type: Schema.ObjectId,
auto: true
},
name: {
type: String,
required: true
},
password: {
type: String,
required: true
},
...
});
module.exports = mongoose.model('User', UserSchema);
Everytime an user favorites an item (from Favorite collection) I will store the ID on UserFavorite. 每当用户收藏一个项目(来自收藏集合)时,我都会将ID存储在UserFavorite上。
My problem: I'm trying to retrieve all favorite data from a given user (all infos from Favorite that are presented on UserFavorite). 我的问题:我正在尝试从给定的用户检索所有喜欢的数据(UserFavorite上显示的来自“收藏夹”的所有信息)。
Here's my code: 这是我的代码:
UserFavorite.aggregate([
{
$match: {
"userId": mongoose.Types.ObjectId(userId)
}
},
{
$lookup: {
"from": "favorites",
"localField": "favoriteId",
"foreignField": "_id",
"as": "favorites"
}
}
]).exec(function (err, data) {
if(err){
//ERROR
}else{
//Everything's okay
}
});
My expected result: 我的预期结果:
I expect to have each User favorite inside of Favorites array. 我希望收藏夹数组中包含每个用户收藏夹。 (the 'as' from Lookup), like the following:
(来自Lookup的“ as”),如下所示:
[
{
"userId": "5bc74f4ac42a2719b8827404",
"favorites": [
{
"_id": "5bc7d92f4235972ea805664e",
"thumbnail": "string",
"storeId": "string",
"name": "string",
},
{
"_id": "5bc7d9414235972ea805664f",
"thumbnail": "no_image",
"storeId": "1234",
"name": "Fulério Store",
}
]
},
]
But what I get is: 但是我得到的是:
[
{
"userId": "5bc74f4ac42a2719b8827404",
"favoriteId": "5bc7d92f4235972ea805664e",
"favorites": [
{
"_id": "5bc7d92f4235972ea805664e",
"thumbnail": "string",
"storeId": "string",
"name": "string",
"__v": 0
}
]
},
{
"userId": "5bc74f4ac42a2719b8827404",
"favoriteId": "5bc7d9414235972ea805664f",
"favorites": [
{
"_id": "5bc7d9414235972ea805664f",
"thumbnail": "no_image",
"storeId": "1234",
"name": "Fulério Store",
"__v": 0
}
]
}
]
It brings the informations of each favorite of "UserFavorite", instead of putting all favorites from a given user into an array. 它带来“ UserFavorite”的每个收藏夹的信息,而不是将给定用户的所有收藏夹放入数组中。
How could I get it working? 我该如何运作?
Thank you in advance! 先感谢您!
You'll need to use the $group operator, what you've got now is akin to 您将需要使用$ group运算符,您现在所拥有的类似于
Try adding this as the last stage of the aggregation 尝试将其添加为聚合的最后阶段
{
$group: {
_id: "$userId",
favoriteArray: {$addToSet: "$favorites"}
}
}
The $group stage will roll them all up based on the userId $ group阶段将根据userId将它们全部汇总
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.