nedb icon indicating copy to clipboard operation
nedb copied to clipboard

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

Open impfromliga opened this issue 7 years ago • 2 comments

//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 avatar Dec 09 '18 16:12 impfromliga

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...

impfromliga avatar Dec 09 '18 16:12 impfromliga

@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

JamesMGreene avatar Mar 10 '19 05:03 JamesMGreene