我正在进行小型社交网络项目Node / Express / Mongodb / Mongoose。我有一些带有嵌套文档和引用的架构。例如,一个用户可以发帖,另一个用户可以在该帖子下发表评论。我想查询所有集合并将结果按从新到旧的顺序排序。我需要一个用户供稿。用于显示系统中的最新活动。
相关架构
const UserSchema = new mongoose.Schema({
name: {
type: String,
required: true,
},
email: {
type: String,
required: true,
unique: true,
},
password: {
type: String,
required: true,
},
approved: {
type: Boolean,
required: true,
default: false,
},
payment: {
type: Boolean,
default: false,
},
emailApproved: {
type: Boolean,
default: false,
},
paymentDate: {
type: Date,
},
resetToken: String,
resetTokenExpiration: Date,
emailToken: {
type: String,
},
following: [{ type: mongoose.Schema.Types.ObjectId, ref: "user" }],
followers: [{ type: mongoose.Schema.Types.ObjectId, ref: "user" }],
role: {
type: String,
required: true,
default: "user",
enum: ["user", "admin", "root", "commission"],
},
date: {
type: Date,
default: Date.now,
},
});
const ProfileSchema = new mongoose.Schema(
{
user: {
type: mongoose.Schema.Types.ObjectId,
ref: "user",
},
image: {
type: String,
},
company: {
type: String,
},
website: {
type: String,
},
start: {
type: Number,
required: true,
},
graduation: {
type: Number,
required: true,
},
department: {
type: String,
required: true,
},
option: {
type: String,
required: true,
},
product: {
type: String,
required: true,
},
mentorship: {
type: Boolean,
},
scholarship: {
type: Boolean,
},
mentorshipSeeker: {
type: Boolean,
},
work: {
type: Boolean,
},
workSeeker: {
type: Boolean,
},
sponsorship: {
type: Boolean,
},
sponsorshipSeeker: {
type: Boolean,
},
internship: {
type: Boolean,
},
scholarshipSeeker: {
type: Boolean,
},
internshipSeeker: {
type: Boolean,
},
representative: {
type: Boolean,
},
location: {
type: String,
},
status: {
type: String,
required: true,
},
skills: {
type: [String],
required: true,
},
boardMember: {
type: Boolean,
},
bio: {
type: String,
},
description: {
type: String,
},
phone: {
type: String,
required: true,
},
emailWork: {
type: String,
},
showPhone: {
type: Boolean,
required: true,
default: false,
},
member: {
type: Boolean,
},
payment: {
type: Boolean,
},
signed: {
type: Boolean,
},
schoolRelation: {
type: String,
},
experience: [
{
title: {
type: String,
required: true,
},
company: {
type: String,
required: true,
},
location: {
type: String,
},
from: {
type: Date,
required: true,
},
to: {
type: Date,
},
current: {
type: Boolean,
default: false,
},
description: {
type: String,
},
},
],
education: [
{
school: {
type: String,
required: true,
},
degree: {
type: String,
required: true,
},
fieldofstudy: {
type: String,
required: true,
},
from: {
type: Date,
required: true,
},
to: {
type: Date,
},
current: {
type: Boolean,
default: false,
},
description: {
type: String,
},
},
],
social: {
youtube: {
type: String,
},
twitter: {
type: String,
},
facebook: {
type: String,
},
linkedin: {
type: String,
},
instagram: {
type: String,
},
},
date: {
type: Date,
default: Date.now,
},
},
{ timestamps: true }
);
const PostSchema = new Schema(
{
activity: {
type: Schema.Types.ObjectId,
ref: "activity",
unique: true,
index: true,
sparse: true,
},
group: {
type: Schema.Types.ObjectId,
ref: "group",
unique: true,
index: true,
sparse: true,
},
user: {
type: Schema.Types.ObjectId,
ref: "user",
},
text: {
type: String,
required: true,
},
name: {
type: String,
},
image: {
type: String,
},
likes: [
{
user: {
type: Schema.Types.ObjectId,
ref: "user",
},
date: {
type: Date,
default: Date.now,
},
},
],
comments: [
{
user: {
type: Schema.Types.ObjectId,
ref: "user",
},
text: {
type: String,
required: true,
},
name: {
type: String,
},
avatar: {
type: String,
},
date: {
type: Date,
default: Date.now,
},
},
],
date: {
type: Date,
default: Date.now,
},
},
{ timestamps: true }
);
我想查询所有模式,包括嵌套模式。然后将它们排序在新文档中。
例如
{latest activity:[
{
comment:{
_id:345345098203948209423409,
user:345345345345345,
text:loremipsum,
date:28/6/2020
postId:4350394583049545
},
{
like:{
_id:23402394802934820394
user:3459803948509348503945
postId:2323409283409238409234
date:27/06/2020
}
},
{
profile:{
_id:2234234234234,
user:2340293840293842093,
date:25/06/2020
}
}]
}
我可以通过服务器端javascript执行。 但是我正在寻找一种通过mongoose / mongo查询实现它的方法。任何帮助将不胜感激
谢谢