Check that Field Exists with MongoDB



  • 3 Answer(s)

    You can use $ne (for “not equal”)

    db.collection.find({ "fieldToCheck": { $exists: true, $ne: null } })
    
    
    Answered on January 8, 2019.
    Add Comment

    Try to use this:

    {
    "_id":"1234"
    "open":"Yes"
    "things":{
                "paper":1234
                "bottle":"Available"
                "bottle_count":40
              }
    }
    

    You want to know if the bottle field is present of not ?

    Answer:

    db.products.find({"things.bottle":{"$exists":true}})
    
    Answered on January 8, 2019.
    Add Comment

    Just try to do it:

    db.getCollection('collectionName').findOne({"fieldName" : {$ne: null}})
    
    
    Answered on January 8, 2019.
    Add Comment


  • Your Answer

    By posting your answer, you agree to the privacy policy and terms of service.