louischatriot / nedb

The JavaScript Database, for Node.js, nw.js, electron and the browser
MIT License
13.46k stars 1.02k forks source link

$exists array [] (quantum mechanics of data base) #581

Open impfromliga opened 5 years ago

impfromliga commented 5 years ago

//strange result with $exists on Arrays:

db.find({},(err,all)=>{
    db.find({arr:{$exists:true}},(err,exists)=>{
        db.find({arr:{$exists:false}},(err,empty)=>{
            console.log('done count exists', all.length, '=', exists.length + empty.length);
            //done count exists 416 = 305
            //wtf?
        })
    })
})
impfromliga commented 5 years ago

Perhaps some where in the library the fact of property $exists check like if(property) or etc. Or i'm bad understand the rule that $exists "return true". In case of array follows that [] not $exists, and not not $exist! such a quantum mechanics of data base...

JamesMGreene commented 5 years ago

@impfromliga Can you confirm that your data set also contained 111 records with an empty array value ([]) for the arr property?

I added some quick checks to various array values in the tests to try to reproduce this. It appears that an empty array fails to match on both $exists: true and $exists: false.

model.match({ a: [5, 6] }, { a: { $exists: true } }).should.equal(true);

model.match({ a: [5] }, { a: { $exists: true } }).should.equal(true);
model.match({ a: [5] }, { b: { $exists: false } }).should.equal(true);

model.match({ a: [0] }, { a: { $exists: true } }).should.equal(true);
model.match({ a: [null] }, { a: { $exists: true } }).should.equal(true);

model.match({ a: [] }, { a: { $exists: false } }).should.equal(false);

model.match({ a: [] }, { a: { $exists: true } }).should.equal(true); // UNEXPECTED FAIL