我有两个型号,为了简单起见我缩短它们。我想获取用户不为空的所有视频。
var Video = new mongoose.Schema({
user : {type: mongoose.Schema.Types.ObjectId, ref: 'User'},
status : {type: String, default: 'Active'},
recycled : {type: Number, default: 0}
});
var User = new mongoose.Schema({
email : {type: String, unique: true, lowercase: true},
password: {type: String, select: false},
status : {type: String, default: 'Active'},
recycled : {type: Number, default: 0}
)};
我正在对排序和东西进行分页,所以我使用了聚合框架。 我有条件对象:
var condition = {
recycled: {$ne: 1},
status : 'Active'
}
所以我的查询看起来像这样:
var _pageNumber = req.body.pageNumber,
_pageSize = req.body.limit,
condition = {
recycled: {$ne: 1},
status : 'Active'
},
var populateQuery = [
{
"path": "user",
model : 'User',
match : {
recycled: {$ne: 1}
}
}
// some other models populated...
];
Video.aggregate(
[
{$match: condition},
{$sort: sort},
{$skip: (_pageNumber > 0 ? ((_pageNumber - 1) * _pageSize) : 0)},
{$limit: _pageSize}
],
function (err, results) {
if (err) return next(err);
Video.populate(results, populateQuery,
function (err, results) {
if (err) throw err;
return res.status(200).send({
message : 'listOk',
videos : results,
pageNumber: _pageNumber
});
)
});
我试过以下:
var condition = {
recycled: {$ne: 1},
status : 'Active',
'user' : {$ne: null} or something like 'videos.user' : {$ne: null}
}
但我认为在执行条件且上述情况不起作用的那段时间内,用户不会填充视频。
我也尝试过
var populateQuery = [
{
"path": "user",
model : 'User',
match : {
recycled : {$ne: 1},
"videos.user": {$ne: null}
}
}
]
由于