Can perform a direct array match

pull/2/head
Louis Chatriot 9 years ago
parent 1f6259adaa
commit 93dda4ba59
  1. 9
      lib/model.js
  2. 8
      test/model.test.js

@ -489,7 +489,7 @@ function areThingsEqual (a, b) {
// Arrays (no match since arrays are used as a $in) // Arrays (no match since arrays are used as a $in)
// undefined (no match since they mean field doesn't exist and can't be serialized) // undefined (no match since they mean field doesn't exist and can't be serialized)
if (util.isArray(a) || util.isArray(b) || a === undefined || b === undefined) { return false; } if ((!(util.isArray(a) && util.isArray(b)) && (util.isArray(a) || util.isArray(b))) || a === undefined || b === undefined) { return false; }
// General objects (check for deep equality) // General objects (check for deep equality)
// a and b should be objects at this point // a and b should be objects at this point
@ -708,6 +708,11 @@ function matchQueryPart (obj, queryKey, queryValue, treatObjAsValue) {
// Check if the value is an array if we don't force a treatment as value // Check if the value is an array if we don't force a treatment as value
if (util.isArray(objValue) && !treatObjAsValue) { if (util.isArray(objValue) && !treatObjAsValue) {
// If the queryValue is an array, try to perform an exact match
if (util.isArray(queryValue)) {
return matchQueryPart(obj, queryKey, queryValue, true);
}
// Check if we are using an array-specific comparison function // Check if we are using an array-specific comparison function
if (queryValue !== null && typeof queryValue === 'object' && !util.isRegExp(queryValue)) { if (queryValue !== null && typeof queryValue === 'object' && !util.isRegExp(queryValue)) {
keys = Object.keys(queryValue); keys = Object.keys(queryValue);
@ -725,7 +730,7 @@ function matchQueryPart (obj, queryKey, queryValue, treatObjAsValue) {
// queryValue is an actual object. Determine whether it contains comparison operators // queryValue is an actual object. Determine whether it contains comparison operators
// or only normal fields. Mixed objects are not allowed // or only normal fields. Mixed objects are not allowed
if (queryValue !== null && typeof queryValue === 'object' && !util.isRegExp(queryValue)) { if (queryValue !== null && typeof queryValue === 'object' && !util.isRegExp(queryValue) && !util.isArray(queryValue)) {
keys = Object.keys(queryValue); keys = Object.keys(queryValue);
firstChars = _.map(keys, function (item) { return item[0]; }); firstChars = _.map(keys, function (item) { return item[0]; });
dollarFirstChars = _.filter(firstChars, function (c) { return c === '$'; }); dollarFirstChars = _.filter(firstChars, function (c) { return c === '$'; });

@ -1126,7 +1126,13 @@ describe('Model', function () {
}); });
describe('Query operator array $size', function () { describe('Comparing on arrays', function () {
it("Can perform a direct array match", function () {
model.match({ planets: ['Earth', 'Mars', 'Pluto'], something: 'else' }, { planets: ['Earth', 'Mars'] }).should.equal(false);
model.match({ planets: ['Earth', 'Mars', 'Pluto'], something: 'else' }, { planets: ['Earth', 'Mars', 'Pluto'] }).should.equal(true);
model.match({ planets: ['Earth', 'Mars', 'Pluto'], something: 'else' }, { planets: ['Earth', 'Pluto', 'Mars'] }).should.equal(false);
});
it('Can query on the size of an array field', function () { it('Can query on the size of an array field', function () {
// Non nested documents // Non nested documents

Loading…
Cancel
Save