希望执行以下查询:
Entrant
.find
enterDate : oneMonthAgo
confirmed : true
.where('pincode.length > 0')
.exec (err,entrants)->
我正在做where子句吗?我想选择pincode
不为空的文档。
答案 0 :(得分:139)
您应该可以这样做(因为您正在使用查询API):
Entrant.where("pincode").ne(null)
...这将导致类似的mongo查询:
entrants.find({ pincode: { $ne: null } })
一些可能有用的链接:
答案 1 :(得分:7)
我最终到了这里,我的问题是我在查询
{$not: {email: /@domain.com/}}
而不是
{email: {$not: /@domain.com/}}
答案 2 :(得分:3)
选择字段值不等于的文档 指定的值。这包括不包含 字段。
User.find({ "username": { "$ne": 'admin' } })
$ nin在以下位置选择文档: 字段值不在指定数组中,或者该字段不存在。
User.find({ "groups": { "$nin": ['admin', 'user'] } })
答案 3 :(得分:0)
总计计算该字段的值不等于指定值的文档。
async function getRegisterUser() {
return Login.count({"role": { $ne: 'Super Admin' }}, (err, totResUser) => {
if (err) {
return err;
}
return totResUser;
})
}
答案 4 :(得分:0)
好的,我找到了解决这个问题的可能方法。我意识到联接在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"}
})
});
答案 5 :(得分:-1)
大家好,我对此深感困惑。我有一个引用了User的文档配置文件,并且我试图列出用户ref不为null的配置文件(因为在填充过程中我已经按rol进行了过滤),但是 谷歌搜索几个小时后,我不知道如何得到这个。一世 进行以下查询:
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 }
谢谢。