값이 null이 아닌 Mongoose 쿼리
다음 쿼리를 수행하려고 합니다.
Entrant
.find
enterDate : oneMonthAgo
confirmed : true
.where('pincode.length > 0')
.exec (err,entrants)->
제가 where 조항을 제대로 하고 있나요?다음 위치에서 문서를 선택합니다.pincode
는 늘이 아닙니다.
(query api를 사용할 때) 다음과 같이 할 수 있습니다.
Entrant.where("pincode").ne(null)
...그 결과 다음과 같은 mongo 쿼리가 발생합니다.
entrants.find({ pincode: { $ne: null } })
도움이 될 수 있는 몇 가지 링크:
필드 값이 지정된 값과 같지 않은 문서를 선택합니다.여기에는 필드가 포함되지 않은 문서가 포함됩니다.
User.find({ "username": { "$ne": 'admin' } })
$nin은 지정된 배열에 필드 값이 없거나 필드가 존재하지 않는 문서를 선택합니다.
User.find({ "groups": { "$nin": ['admin', 'user'] } })
결국 여기에 오게 되었고, 제 문제는 그 질문에 대한 질문이었습니다.
{$not: {email: /@domain.com/}}
대신
{email: {$not: /@domain.com/}}
좋아, 얘들아 이 문제에 대한 가능한 해결책을 찾았어.Mongo에는 Join이 존재하지 않는다는 것을 깨달았습니다.그 때문에 우선 사용자의 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"}
})
});
total 필드 값이 지정된 값과 같지 않은 문서를 카운트합니다.
async function getRegisterUser() {
return Login.count({"role": { $ne: 'Super Admin' }}, (err, totResUser) => {
if (err) {
return err;
}
return totResUser;
})
}
안녕 얘들아 난 이거에 갇혔어쩔 수 없다.사용자를 참조하는 문서 프로파일이 있으며, 사용자 참조가 null이 아닌 프로파일을 나열하려고 했습니다(인구 중 롤별로 이미 필터링했기 때문에). 그러나 몇 시간 동안 구글을 검색한 후 이 프로파일을 얻는 방법을 찾을 수 없습니다.질문이 있습니다.
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 }
잘 부탁드립니다.
언급URL : https://stackoverflow.com/questions/16531895/mongoose-query-where-value-is-not-null
'programing' 카테고리의 다른 글
OpenCart에서 간단한 모듈을 만드는 방법Wordpress에서 최신 게시물을 가져와 OpenCart에 표시하는 예? (0) | 2023.04.04 |
---|---|
Spring 앱의 옵션 환경 변수 (0) | 2023.04.04 |
Postgres에서 JSON 개체에 요소 추가 (0) | 2023.03.25 |
언로드 이벤트를 사용하여 Ajax 요청을 확실하게 실행할 수 있습니까? (0) | 2023.03.25 |
Wordpress에 대한 테마 주석 헤더를 남기면서 Sas 압축 출력을 사용합니다. (0) | 2023.03.25 |