猫鼬查询对象数组

时间:2020-05-24 13:47:23

标签: javascript node.js mongodb mongoose

如何使用Mongoose查询数组?

搜索架构:

const searchschema = new schema({
    yeardate: { type: Number, required: true, min: 1820 },
    word: { type: String, required: true, index: true },
    user: [{ type: schema.Types.ObjectId, ref: 'User' }]    })

用户架构:

const userschema = new schema({
    username: { type: String, required: true, unique: true, index: true },//
    name: { type: String, default: 'NoName' },
    gender: { type: String, default: 'male' },   })

这是我尝试过但无法使用的查询:

searchmodel.paginate({ 'user': { $elemMatch: { gender: 'female' } } }, { page: page, limit: 5, populate: ['user'] }).then(searches => {
        if (!searches) {
            return res.json({ message: 'there is no search' })
        }else {
            return res.json(searches)
        }
    })

0 个答案:

没有答案