|
|
|
@ -9,6 +9,7 @@ var dateToJSON = function () { return { $$date: this.getTime() }; } |
|
|
|
|
, util = require('util') |
|
|
|
|
, _ = require('underscore') |
|
|
|
|
, modifierFunctions = {} |
|
|
|
|
, matcherFunctions = {} |
|
|
|
|
; |
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -125,6 +126,10 @@ function deepCopy (obj) { |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// ==============================================================
|
|
|
|
|
// Updating documents
|
|
|
|
|
// ==============================================================
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Set field to value in a model |
|
|
|
|
* Create it if it doesn't exist |
|
|
|
@ -221,6 +226,30 @@ function modify (obj, updateQuery) { |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// ==============================================================
|
|
|
|
|
// Finding documents
|
|
|
|
|
// ==============================================================
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Test for field equality |
|
|
|
|
* @param {Object} obj The model to check |
|
|
|
|
* @param {String} field Can contain dots, in that case that means we will set a subfield recursively |
|
|
|
|
* @param {Model} value |
|
|
|
|
*/ |
|
|
|
|
matcherFunctions.$eq = function (obj, field, value) { |
|
|
|
|
var fieldParts = typeof field === 'string' ? field.split('.') : field; |
|
|
|
|
|
|
|
|
|
if (!obj) { return false; } // field cannot be empty here so that means there is no match
|
|
|
|
|
|
|
|
|
|
if (fieldParts.length === 1) { |
|
|
|
|
return obj[fieldParts[0]] === value; |
|
|
|
|
} else { |
|
|
|
|
return matcherFunctions.$eq(obj[fieldParts[0]], fieldParts.slice(1), value); |
|
|
|
|
} |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Tell if a given document matches a query |
|
|
|
|
*/ |
|
|
|
@ -229,7 +258,7 @@ function match (obj, query) { |
|
|
|
|
, i, k; |
|
|
|
|
|
|
|
|
|
Object.keys(query).forEach(function (k) { |
|
|
|
|
if (obj[k] !== query[k]) { match = false; } |
|
|
|
|
if (!matcherFunctions.$eq(obj, k, query[k])) { match = false; } |
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
return match; |
|
|
|
|