Answers:
您应该能够做到这一点(因为您正在使用查询api):
Entrant.where("pincode").ne(null)
...这将导致类似于以下内容的mongo查询:
entrants.find({ pincode: { $ne: null } })
一些链接可能会有所帮助:
...("myArraySubDoc[0].someValue").ne(true)
?
where("myArraySubDoc.0.someValue").ne(true)
对字段值不等于指定值的文档总数进行计数。
async function getRegisterUser() {
return Login.count({"role": { $ne: 'Super Admin' }}, (err, totResUser) => {
if (err) {
return err;
}
return totResUser;
})
}
好的,我发现了这个问题的可能解决方案。我意识到联接在Mongo中不存在,这就是为什么首先需要查询具有所需角色的用户ID,然后再对配置文件进行另一次查询的原因,如下所示:
const exclude: string = '-_id -created_at -gallery -wallet -MaxRequestersPerBooking -active -__v';
// Get the _ids of users with the role equal to role.
await User.find({role: role}, {_id: 1, role: 1, name: 1}, function(err, docs) {
// Map the docs into an array of just the _ids
var ids = docs.map(function(doc) { return doc._id; });
// Get the profiles whose users are in that set.
Profile.find({user: {$in: ids}}, function(err, profiles) {
// docs contains your answer
res.json({
code: 200,
profiles: profiles,
page: page
})
})
.select(exclude)
.populate({
path: 'user',
select: '-password -verified -_id -__v'
// group: { role: "$role"}
})
});
大家好,我对此深信不疑。我有一个引用了User的文档个人资料,并且我尝试列出用户ref不为null的个人资料(因为在填充过程中我已经按rol进行了过滤),但是经过Google几个小时的搜索后,我无法弄清楚如何得到这个。我有这个查询:
const profiles = await Profile.find({ user: {$exists: true, $ne: null }}) .select("-gallery") .sort( {_id: -1} ) .skip( skip ) .limit(10) .select(exclude) .populate({ path: 'user', match: { role: {$eq: customer}}, select: '-password -verified -_id -__v' }) .exec(); And I get this result, how can I remove from the results the user:null colletions? . I meant, I dont want to get the profile when user is null (the role does not match). { "code": 200, "profiles": [ { "description": null, "province": "West Midlands", "country": "UK", "postal_code": "83000", "user": null }, { "description": null, "province": "Madrid", "country": "Spain", "postal_code": "43000", "user": { "role": "customer", "name": "pedrita", "email": "myemail@gmail.com", "created_at": "2020-06-05T11:05:36.450Z" } } ], "page": 1 }
提前致谢。