mongoose - How to check if sub-field exists and is not null in mongodb aggregation?

Mongoose - How to check if sub-field exists and is not null in mongodb aggregation?

In MongoDB aggregation, you can check if a sub-field exists and is not null using the $ifNull or $cond operator along with $type. Here is an example using the $cond operator to check if a sub-field exists and is not null:

Example Data

Assume we have a collection named users with documents like this:

{ "_id": ObjectId("60c72b2f9af1f4c4dcd03f12"), "name": "John Doe", "profile": { "email": "john@example.com", "phone": null } } 

Aggregation Pipeline

  1. Match stage: Filter documents (optional).
  2. Add Fields stage: Add a new field that checks if the sub-field exists and is not null.

Here's how you can do it:

[ { "$match": { // Your match criteria here (optional) } }, { "$addFields": { "profileExistsAndNotNull": { "$cond": { "if": { "$and": [ { "$ne": ["$profile", null] }, { "$ne": ["$profile.phone", null] }, { "$ne": ["$profile.phone", undefined] } ] }, "then": true, "else": false } } } } ] 

Explanation

  • $match: This stage is optional and is used to filter documents based on your criteria.
  • $addFields: This stage is used to add a new field profileExistsAndNotNull which is true if the sub-field profile.phone exists and is not null or undefined.
    • $cond: This operator is used to check conditions and output a value based on the condition.
      • $and: Ensures all conditions are met.
      • $ne: Checks that the value is not equal to null or undefined.

Additional Example Using $ifNull

You can also achieve this using $ifNull to check if the sub-field exists and is not null:

[ { "$match": { // Your match criteria here (optional) } }, { "$addFields": { "profileExistsAndNotNull": { "$ifNull": ["$profile.phone", false] } } } ] 

Explanation

  • $ifNull: Checks if profile.phone is null or does not exist, and if so, it returns false. Otherwise, it returns the value of profile.phone.

Both approaches effectively check if the sub-field exists and is not null. The first approach using $cond gives you more control and clarity, while the second approach using $ifNull is more concise but less flexible. Choose the approach that best fits your use case.

Examples

  1. mongoose check if subfield exists and is not null

    • Description: Shows how to filter documents in Mongoose where a subfield exists and is not null using aggregation.
    YourModel.aggregate([ { $match: { 'subDocumentField': { $exists: true, $ne: null } } } ]).exec((err, results) => { if (err) { console.error(err); return; } console.log(results); }); 
  2. mongoose aggregation match subdocument exists and not null

    • Description: Demonstrates using Mongoose aggregation framework to match documents where a subdocument exists and is not null.
    YourModel.aggregate([ { $match: { 'subDocumentField': { $exists: true, $ne: null } } } ]).exec((err, results) => { if (err) { console.error(err); return; } console.log(results); }); 
  3. mongoose aggregation filter subfield not null

    • Description: Filters documents in Mongoose aggregation pipeline where a specific subfield is not null.
    YourModel.aggregate([ { $match: { 'subDocumentField.someSubField': { $exists: true, $ne: null } } } ]).exec((err, results) => { if (err) { console.error(err); return; } console.log(results); }); 
  4. mongoose check subfield not null in aggregation

    • Description: Checks if a nested subfield is not null using Mongoose aggregation framework.
    YourModel.aggregate([ { $match: { 'subDocumentField.someSubField': { $exists: true, $ne: null } } } ]).exec((err, results) => { if (err) { console.error(err); return; } console.log(results); }); 
  5. mongoose subdocument field exists and not empty

    • Description: Queries Mongoose for documents where a subdocument field exists and is not empty in an aggregation pipeline.
    YourModel.aggregate([ { $match: { 'subDocumentField': { $exists: true, $ne: {} } } } ]).exec((err, results) => { if (err) { console.error(err); return; } console.log(results); }); 
  6. mongoose aggregation check nested field not null

    • Description: Checks if a nested field inside a subdocument is not null using Mongoose aggregation.
    YourModel.aggregate([ { $match: { 'subDocumentField.nestedField': { $exists: true, $ne: null } } } ]).exec((err, results) => { if (err) { console.error(err); return; } console.log(results); }); 
  7. mongoose aggregate match subfield exists

    • Description: Matches documents in Mongoose aggregation where a specific subfield exists.
    YourModel.aggregate([ { $match: { 'subDocumentField': { $exists: true } } } ]).exec((err, results) => { if (err) { console.error(err); return; } console.log(results); }); 
  8. mongoose check if nested field exists

    • Description: Checks if a nested field exists within a document using Mongoose aggregation framework.
    YourModel.aggregate([ { $match: { 'subDocumentField.nestedField': { $exists: true } } } ]).exec((err, results) => { if (err) { console.error(err); return; } console.log(results); }); 
  9. mongoose find documents with non-null subfield

    • Description: Finds documents in Mongoose aggregation where a specific subfield is not null.
    YourModel.aggregate([ { $match: { 'subDocumentField.someSubField': { $ne: null } } } ]).exec((err, results) => { if (err) { console.error(err); return; } console.log(results); }); 
  10. mongoose aggregation match subdocument not null

    • Description: Matches documents in Mongoose aggregation where a subdocument is not null.
    YourModel.aggregate([ { $match: { 'subDocumentField': { $ne: null } } } ]).exec((err, results) => { if (err) { console.error(err); return; } console.log(results); }); 

More Tags

angularjs-authentication pydroid parent-pom facebook-fql node-gyp workflow-definition-language nsfilemanager crt catalina catalan

More Programming Questions

More Trees & Forestry Calculators

More Electrochemistry Calculators

More Electronics Circuits Calculators

More Gardening and crops Calculators