mongoose 多集合关联搜索问题,求帮助~~
发布于 5天前 作者 adam1985 106 次浏览 来自 问答

按标题、用户名(user模型)、平台名称(platform模型) 进行搜索, 怎么做关联搜索呢?

下面代码有问题,搜索不到数据:

var kw = /关键词/;
var conditions = {$or: [{"title": kw}, {"username.name": kw}, {"platname.name": kw}]};
var query = model.find( conditions ).populate({
                        path: 'platname',
                        select: 'name'
                    }).populate({
                        path: 'username',
                        select: 'name'
                    });
query = query.sort(sort).skip(start).limit(pagesize); 
query.exec(function (err, results) {});

var mongoose = require('mongoose'),
    Schema = mongoose.Schema,
    ObjectId = Schema.Types.ObjectId;
var timerSchema = new Schema({
        mode: Number,
        username: {type: ObjectId, ref: 'user'},   
        platname: {type: ObjectId, ref: 'platform'},
        time: String,
        title: String,
        state: {
            type : Number,
            default : 1
        }
});     
        
// user模型
var userSchema = new Schema({
    name: {
        type: String,
        unique: true
    },
    pwd: String,
    email: {
        type: String,
        unique: true
    }
});

// platform模型
var platSchema = new Schema({
    name: {
        type: String,
        unique: true
    },
    account:{
        type: String,
        unique: true
    },
    pwd: String
});

\n```
1 回复

没人作答呢

回到顶部