Mongodb Mongoose查找返回错误结果
我有两种模式:Mongodb Mongoose查找返回错误结果,mongodb,mongoose,Mongodb,Mongoose,我有两种模式: const userSchema = new Schema( { username: { type: String, required: true, }, password: { type: String, required: true, }, }, { timestamps: true, } ); const userLikeSchema = new Schema( {
const userSchema = new Schema(
{
username: {
type: String,
required: true,
},
password: {
type: String,
required: true,
},
},
{
timestamps: true,
}
);
const userLikeSchema = new Schema(
{
userId: {
type: String,
required: true,
ref: 'User',
},
likedUserId: {
type: String,
required: true,
ref: 'User',
},
isActive: {
type: Boolean,
required: true,
default: true,
},
},
{
timestamps: true,
}
);
我试图得到一个用户列表,按喜欢的数量降序排列。
我不确定我喜欢的模式是否正确。
我编写了以下查询:
const likedUsers = await UserLike.aggregate(
[
{
$group: {
_id: '$likedUserId',
likes: { $sum: 1 },
},
},
{ $sort: { likes: -1 } },
{
$lookup: {
from: 'users',
localField: 'likedUserId',
foreignField: '_id.str',
as: 'user',
},
},
]
);
我试图得到如下结果:
[
{
"_id": "604bb648be8680063009fddc",
"likes": 2,
"user": {
"_id": "604bb648be8680063009fddc",
"username": "muhamed",
"password": "$2b$10$EVYWZb4vl2TFGmIrCWe1sO/QogdU6/Ui8TgujY4PMKLJKIVOzmOi6",
"createdAt": "2021-03-12T18:43:20.806Z",
"updatedAt": "2021-03-12T18:46:17.635Z",
"__v": 0
},
}
]
但我得到的是以下信息,显示了每个项目的每个用户:
[
{
"_id": "604bb648be8680063009fddc",
"likes": 2,
"user": [
{
"_id": "604bb648be8680063009fddc",
"username": "muhamed",
"password": "$2b$10$EVYWZb4vl2TFGmIrCWe1sO/QogdU6/Ui8TgujY4PMKLJKIVOzmOi6",
"createdAt": "2021-03-12T18:43:20.806Z",
"updatedAt": "2021-03-12T18:46:17.635Z",
"__v": 0
},
{
"_id": "604bc703ea4bf93fb427056a",
"username": "krasniqi",
"password": "$2b$10$dnKumHhKNIfA6BM3uekymOpIdMFuQy9aYYKmGBxnW401CjTAuMLIy",
"createdAt": "2021-03-12T19:54:43.368Z",
"updatedAt": "2021-03-12T19:54:43.368Z",
"__v": 0
},
{
"_id": "604c90ab9f7b970cd46ff668",
"username": "matin",
"password": "$2b$10$CEUCaGk.JF5PBwsTBE3fRufVXzUBt.eLyo28eTt8zhBezVSFflMhS",
"createdAt": "2021-03-13T10:15:07.877Z",
"updatedAt": "2021-03-13T10:15:07.877Z",
"__v": 0
}
]
},
]
我遗漏了什么?我认为关于
\u id.str
有一个问题
您已将userLikeSchema设置为错误。您应该已经设置了ID字段的类型mongoose.Schema.ObjectId
:
const userLikeSchema = new Schema(
{
userId: {
type: mongoose.Schema.ObjectId,
required: true,
ref: 'User',
},
likedUserId: {
type: mongoose.Schema.ObjectId,
required: true,
ref: 'User',
},
isActive: {
type: Boolean,
required: true,
default: true,
},
},
{
timestamps: true,
}
);
之后,您可以通过\u id
轻松查找:
const likedUsers = await UserLike.aggregate(
[
{
$group: {
_id: '$likedUserId',
likes: { $sum: 1 },
},
},
{ $sort: { likes: -1 } },
{
$lookup: {
from: 'users',
localField: '_id',
foreignField: '_id',
as: 'user',
},
}, {
$unwind: "$user"
}
]
);
作为$lookup
的结果,您将获得一个用户
数组,因此您需要使用$unwind
获取单个用户
对象
结果如下:
{
"_id": "604bb648be8680063009fddc",
"likes": 2,
"user": {
"_id": "604bb648be8680063009fddc",
"username": "muhamed",
"password": "$2b$10$EVYWZb4vl2TFGmIrCWe1sO/QogdU6/Ui8TgujY4PMKLJKIVOzmOi6",
"createdAt": "2021-03-12T18:43:20.806Z",
"updatedAt": "2021-03-12T18:46:17.635Z",
"__v": 0
},
}
非常感谢,我没有注意到ID是字符串,因为我复制粘贴了它们