Mongoose query where value is not null

前端 未结 6 1333
轻奢々
轻奢々 2020-12-23 15:55

Looking to do the following query:

Entrant
    .find
      enterDate : oneMonthAgo
      confirmed : true
    .where(\'pincode.length > 0\')
    .exec (er         


        
6条回答
  •  南方客
    南方客 (楼主)
    2020-12-23 16:30

    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": "myemail@gmail.com",
                    "created_at": "2020-06-05T11:05:36.450Z"
                }
            }
        ],
        "page": 1
    }
    

    Thanks in advance.

提交回复
热议问题