fiddle.js icon indicating copy to clipboard operation
fiddle.js copied to clipboard

False positives for modified boolean

Open rauchg opened this issue 13 years ago • 3 comments

If I execute $addToSet and it results in a noop, fiddle will still return true indicating the object was modified.

rauchg avatar May 09 '12 14:05 rauchg

Looking at my code, the only reason why false should be returned is if you modify a collection with a filter. For example:

var fiddler = fiddle({$inc:{age:1}}, {name:'Craig'});


fiddler({name:"Craig"}); //returns true
fiddler({name:"John"}); //returns false

However, there seems to be a bug where if a set doesn't exist in your target object, fiddle fails to add it. I've opened a new issue for this.

I'll also take a look at the mongodb docs to see where a noop might occur when updating an object.

crcn avatar May 09 '12 15:05 crcn

Oh I see… The true/false response is for filters. In my case, it'd be very useful to also know whether something is noop. Not sure if it aligns with the goals of the lib or I should implement that myself on my end

rauchg avatar May 09 '12 19:05 rauchg

I think it makes sense to return false if no objects have been modified, so I added it.

By the looks of it, there are only a few operations where this would be useful: $pop, $pull, $rename, and $pullAll. Any other operation should modify an object. Here are a few examples:

var fiddler = fiddle({$rename:{doesNotExist:'cooking'}}),
fiddler2    = fiddle({$pull:{hobbies:'cooking'}});

//returns false
console.log(fiddler({name:"Craig"})); 

//returns false
console.log(fiddler2({name:"Craig", hobbies:['traveling']})); 

 //returns true, removed "cooking"
console.log(fiddler2({name:"Craig", hobbies:['cooking']}));

crcn avatar May 09 '12 19:05 crcn