javascriptnode.jsmongodbnedb

How do I find documents by name only with NeDB?


I'm trying to find documents by only the document name in NeDB. I have read through the documentation, but all examples also search on a value. I am only inserting one document, and need to query by its name only.

    const Datastore = require('nedb')
        , db = new Datastore({ filename: './data/database.json', autoload: true })
                    
    db.find("myDocName", (err, docs)=>{ // this returns no results even though the document exists
                        
        if(docs[0]){           
             console.log(docs[0])    
        } else {
             database.db.insert({myDocName: {some: "data"})           
        }
                    
    })

I have also tried matching any value with regular expressions, to no avail:

    let regEx = /.*/
    database.db.find({"myDocName":regEx}, (err, docs)=>{
        ....

Solution

  • it seems that the $exists operator might be exactly that what you are looking for:

    db.find({myDocName: {$exists: true}}, (err, docs)=>{
      ...
    });