как заполнить 2 массива и отсортировать их по времени, созданному с помощью mon goose? - PullRequest
0 голосов
/ 06 января 2020
    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)

это модели, которые у меня есть. каждый раз, когда сообщение создается в любой категории, его идентификаторы пользователя и сообщения помещаются в модель категории. однако я хотел бы присоединиться к этим таблицам и отсортировать их по данным, таким как:

 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' }))

Мне нужно это, чтобы вернуть всех постов зарегистрированному пользователю и перечислить их по дате. Схема AllPosts не имеет поля даты, но HousingPosts и ForSalePosts имеют.

1 Ответ

0 голосов
/ 06 января 2020

Итак, что вы ищете:

AllPosts.findOne({ user: req.params.userid })
.populate({path: 'housingposts', options: { sort: { 'date': -1 } } })

Также см. Пн goose JS для более подробной информации

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...