英文:
how to populate 2 arrays and sort them by their time created using mongoose?
问题
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
// HousingPost Model
const HousingPostSchema = new Schema({
// ... (same fields as in your code)
date: {
type: Date,
default: Date.now
}
});
module.exports = mongoose.model('housingModel', HousingPostSchema);
// ForSalePosts Model
const ForSalePostSchema = new Schema({
// ... (same fields as in your code)
date: {
type: Date,
default: Date.now
}
});
module.exports = mongoose.model('forSaleModel', ForSalePostSchema);
// AllPosts Model
const AllPostsSchema = new Schema({
user: {
type: Schema.Types.ObjectId,
ref: 'users'
},
forsaleposts: [{
type: Schema.Types.ObjectId,
ref: 'forSaleModel'
}],
housingposts: [{
type: Schema.Types.ObjectId,
ref: 'housingModel'
}]
});
module.exports = mongoose.model('allposts', AllPostsSchema);
// User Model
const UserSchema = new Schema({
// ... (same fields as in your code)
date: {
type: Date,
default: Date.now
}
});
module.exports = mongoose.model('users', UserSchema);
I've included the "date" field with a default value of "Date.now" in both the HousingPost and ForSalePost schemas, as you mentioned you want to sort the posts by date. Now, you can use the code you provided to populate and sort the posts:
AllPosts.findOne({ user: req.params.userid })
.populate('housingposts')
.populate('forsaleposts')
.then(posts => res.json(posts))
.catch(err => res.status(404).json({ nopostfound: 'There is no housing posts' }));
This code will return all posts for a logged-in user, combining both housing and for sale posts and sorting them by date.
英文:
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const PostSchema = new Schema({
user: {
type: Schema.Types.ObjectId,
ref: 'users'
},
title: {
type: String,
required: true
},
location: {
type: String,
required: true
},
description: {
type: String,
required: true
},
name: {
type: String,
required: true
},
userid: {
type: String,
required: true
},
email: {
type: String,
required: true
},
phonenumber: {
type: String,
required: true
},
language: {
type: String,
required: true
},
postcode: {
type: String,
required: true
}
,
price: {
type: String,
required: true
},
date: {
type: Date,
default: Date.now
}
})
module.exports = HousingPost = mongoose.model('housingModel', PostSchema);
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const PostSchema = new Schema({
user: {
type: Schema.Types.ObjectId,
ref: 'users'
},
title: {
type: String,
required: true
},
location: {
type: String,
required: true
},
description: {
type: String,
required: true
},
name: {
type: String,
required: true
},
price: {
type: String,
required: true
},
userid: {
type: String,
required: true
},
email: {
type: String,
required: true
},
phonenumber: {
type: String,
required: true
},
language: {
type: String,
required: true
},
make: {
type: String,
required: true
},
model: {
type: String,
required: true
},
condition: {
type: String,
required: true
},
postcode: {
type: String,
required: true
},
links: [{ type: String }],
date: {
type: Date,
default: Date.now
}
})
module.exports = ForSalePosts = mongoose.model('forSaleModel', PostSchema);
const mongoose = require('mongoose')
const Schema = mongoose.Schema;
const AllPostsSchema = new Schema({
user: {
type: Schema.Types.ObjectId,
ref: 'users'
},
forsaleposts: [{
type: Schema.Types.ObjectId,
ref: 'forSaleModel'
}],
housingposts: [{
type: Schema.Types.ObjectId,
ref: 'housingModel'
}],
})
module.exports = AllPosts = mongoose.model('allposts', AllPostsSchema)
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const UserSchema = new Schema({
name: {
type: String,
required: true,
trim: true
},
email: {
type: String,
required: true,
trim: true
},
password: {
type: String,
required: true
},
city: {
type: String,
required: true,
trim: true
},
date: {
type: Date,
default: Date.now
},
})
module.exports = User = mongoose.model('users', UserSchema)
these are the models i have. everytime a post created in any category, their user and post ids are being pushed into category model. however i'd like to join these tables and sort them by data like :
AllPosts.findOne({ user: req.params.userid })
.populate('housingposts')
.populate('forsaleposts')
.sort()
.then(posts => res.json(posts))
.catch(err => res.status(404).json({ nopostfound: 'There is no housing posts' }))
i need this to return allposts by logged in user and list them by date. AllPosts schema doesnt have a date field but HousingPosts and ForSalePosts do.
答案1
得分: 1
你所寻找的是:
AllPosts.findOne({ user: req.params.userid })
.populate({path: 'housingposts', options: { sort: { 'date': -1 } } })
还可以参考Mongoose JS获取更多详情。
英文:
So what you're looking for is:
AllPosts.findOne({ user: req.params.userid })
.populate({path: 'housingposts', options: { sort: { 'date': -1 } } })
Also see Mongoose JS for more details
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论