如何填充2个数组并按使用猫鼬创建的时间对其进行排序?

时间:2020-01-06 09:35:02

标签: node.js mongodb express mongoose

    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)

这些是我的模特。每当在任何类别中创建帖子时,其用户名和帖子ID都会被推送到类别模型中。但是我想加入这些表并按如下数据对它们进行排序:

 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 :(得分:0)

所以您要寻找的是:

[ {"patient":{"_id":"0dc329fe4ac40d8640528025cb53bbdc"},"graphing":[]}  ,

{"patient":{"_id":"0f107203d419496e855d1e92259883bc"},"graphing":[]} ,

{"patient":{"_id":"0f10be0663c34f62a82a5ac8331bea6a"},"graphing":[]} ]

另请参见Mongoose JS