diff --git a/dbjson.php b/dbjson.php index 68a1380..c03884b 100644 --- a/dbjson.php +++ b/dbjson.php @@ -67,11 +67,12 @@ class dbjson */ public function insertOne ($collection, $document) { + $uniqueKey = $this->uniqueKey (); $this->lockEX (); $this->db = $this->readDB (); - $this->db[$collection]["content"][] = array_merge (array ( - "_id"=>$this->uniqueKey ()), - $document); + $this->db[$collection]["content"][$uniqueKey] = array_merge ( + array ("_id"=>$uniqueKey), + $document); $this->writeDB ($this->db); $this->lockUN (); return 1; @@ -91,9 +92,10 @@ class dbjson $this->db = $this->readDB (); foreach ($documents as $document) { - $this->db[$collection]["content"][] = array_merge (array ( - "_id"=>$this->uniqueKey ()), - $document); + $uniqueKey = $this->uniqueKey (); + $this->db[$collection]["content"][$uniqueKey] = array_merge ( + array ("_id"=>$uniqueKey), + $document); } $this->writeDB ($this->db); $this->db = null; @@ -172,7 +174,22 @@ class dbjson { // Merge the new document with the old $tmp = $this->db[$collection]["content"][$key]; - $tmp = array_merge ($tmp, $document); + // We need to merge the old and the new document. + // If there is an array in the document, it is overwrited + foreach ($document as $k=>$v) + { + if (is_array ($v)) + { + if (isset ($tmp[$k])) + $tmp[$k] = array_merge ($tmp[$k], $v); + else + $tmp[$k] = $v; + } + else + { + $tmp[$k] = $v; + } + } $this->db[$collection]["content"][$key] = $tmp; // Remove the needed unset fields foreach ($unset as $field) @@ -298,7 +315,8 @@ class dbjson $fvals[1] !== "<" && $fvals[1] !== ">" && $fvals[1] !== "exists" && - $fvals[1] !== "not exists") + $fvals[1] !== "not exists" && + $fvals[1] !== "in_array") throw new \Exception ("Invalid filter operator provided", 500); if ($fvals[1] === "==" && $document[$fkey] === $fvals[0]) $matchFilter = true; @@ -316,6 +334,9 @@ class dbjson elseif (strtolower ($fvals[1]) === "not exists" && ! array_key_exists ($fkey, $document)) $matchFilter = true; + elseif (strtolower ($fvals[1]) === "in_array" && + in_array ($fvals[0], $document[$fkey])) + $matchFilter = true; else { $matchFilter = false;