Skip to content

Commit

Permalink
Serialize lists of embedded docs. Fixes #2
Browse files Browse the repository at this point in the history
  • Loading branch information
zbjornson committed Jan 11, 2016
1 parent 30cb800 commit 6fdce0a
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 2 deletions.
8 changes: 7 additions & 1 deletion MongoDBLink.m
Original file line number Diff line number Diff line change
Expand Up @@ -275,11 +275,17 @@
serialize[{}] := JavaNew["com.mongodb.BasicDBObject"]

serialize[_, x : $rulepattern] := Block[
{newdbobj = JavaNew["com.mongodb.BasicDBObject"]},
{newdbobj = JavaNew["com.mongodb.BasicDBObject"]},
Map[serialize[newdbobj, #] &, x];
newdbobj
]

serialize[_, x : { $rulepattern }] := Block[
{newdblist = JavaNew["com.mongodb.BasicDBList"]},
MapIndexed[newdblist@put[First[#2] - 1, serialize[#]]&, x];
newdblist
]

(* $in, $nin, ... *)
serialize[dbobj_, Rule["_id", {a_ -> b_}]] :=
dbobj@put["_id", serialize[dbobj, {a -> (ObjectId /@ b)}]]
Expand Down
2 changes: 1 addition & 1 deletion PacletInfo.m
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

Paclet[
Name -> "MongoDBLink",
Version -> "0.1.1",
Version -> "0.1.2",
MathematicaVersion -> "6+",
Extensions ->
{
Expand Down
8 changes: 8 additions & 0 deletions Tests.wlt
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,14 @@ VerificationTest[
TestID->"ebd27ef8-953f-433f-9f13-976c2f07ec6c"
]

VerificationTest[
MongoDBLink`Private`serialize[{"a" -> 1, "b" -> {{"b1" -> 1}}}]@toString[]
,
"{ \"a\" : 1 , \"b\" : [ { \"b1\" : 1}]}"
,
TestID->"aa43675c-c925-4450-b221-8cb964194447"
]

EndTestSection[]

VerificationTest[
Expand Down

0 comments on commit 6fdce0a

Please sign in to comment.