Mongoose query where value is not null

MongodbMongoose

Mongodb Problem Overview


Looking to do the following query:

Entrant
    .find
      enterDate : oneMonthAgo
      confirmed : true
    .where('pincode.length > 0')
    .exec (err,entrants)->

Am I doing the where clause properly? I want to select documents where pincode is not null.

Mongodb Solutions


Solution 1 - Mongodb

You should be able to do this like (as you're using the query api):

Entrant.where("pincode").ne(null)

... which will result in a mongo query resembling:

entrants.find({ pincode: { $ne: null } })

A few links that might help:

Solution 2 - Mongodb

$ne > selects the documents where the value of the field is not equal to > the specified value. This includes documents that do not contain the > field.

User.find({ "username": { "$ne": 'admin' } })

$nin

> $nin selects the documents where: > the field value is not in the specified array or the field does not exist.

User.find({ "groups": { "$nin": ['admin', 'user'] } })

Solution 3 - Mongodb

I ended up here and my issue was that I was querying for

{$not: {email: /@domain.com/}}

instead of

{email: {$not: /@domain.com/}}

Solution 4 - Mongodb

Ok guys I found a possible solution to this problem. I realized that joins do not exists in Mongo, that's why first you need to query the user's ids with the role you like, and after that do another query to the profiles document, something like this:

    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"} 
          })
    });

Solution 5 - Mongodb

total count the documents where the value of the field is not equal to the specified value.

async function getRegisterUser() {
    return Login.count({"role": { $ne: 'Super Admin' }}, (err, totResUser) => {
        if (err) {
            return err;
        }
        return totResUser;
    })
}

Solution 6 - Mongodb

Hello guys I am stucked with this. I've a Document Profile who has a reference to User,and I've tried to list the profiles where user ref is not null (because I already filtered by rol during the population), but after googleing a few hours I cannot figure out how to get this. I have this query:
>
> 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": "[email protected]", > "created_at": "2020-06-05T11:05:36.450Z" > } > } > ], > "page": 1 > }

Thanks in advance.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionwesbosView Question on Stackoverflow
Solution 1 - Mongodbnumbers1311407View Answer on Stackoverflow
Solution 2 - MongodbTính Ngô QuangView Answer on Stackoverflow
Solution 3 - MongodbMalcolmOceanView Answer on Stackoverflow
Solution 4 - MongodbR0bertinskiView Answer on Stackoverflow
Solution 5 - MongodbVadivel SubramanianView Answer on Stackoverflow
Solution 6 - MongodbR0bertinskiView Answer on Stackoverflow