Skip to content

Commit

Permalink
Basic updates.
Browse files Browse the repository at this point in the history
  • Loading branch information
mbostock committed Jul 17, 2011
1 parent bee4fe4 commit e92b1e8
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 4 deletions.
27 changes: 26 additions & 1 deletion lib/mongo-mock/collection.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,33 @@ function collection(id, options) {
if (callback) callback(null);
}

// TOOD $inc
// TODO $set
// TODO $unset
// TODO $push
// TODO $pushAll
// TODO $addToSet
// TODO $pop
// TODO $pull
// TODO $pullAll
// TODO $rename
// TODO $bit
// TODO options.upsert
// TODO $ positional operator
function update(query, object, options, callback) {
if (!callback) { callback = options; options = null; }
callback({message: "Not yet implemented."});
var single = !(options && options.multi),
f = filter(query),
i = -1,
n = collection.length,
o;
while (++i < n) {
if (f(o = collection[i])) {
(collection[i] = JSON.parse(JSON.stringify(object)))._id = o._id;
if (single) break;
}
}
callback(null);
}

function save(object, options, callback) {
Expand All @@ -61,6 +85,7 @@ function collection(id, options) {
};
}

// TODO search arrays (e.g., "comments.by": "joe" in comments: [{by: "joe"}])
// TODO report errors when operator is unknown
function filter(fields) {
var predicates = [],
Expand Down
13 changes: 11 additions & 2 deletions test/test-collection.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,17 @@ db.open(function(error) {

db.open(function(error) {
db.collection("save", function(error, collection) {
collection.save({hello: "world"}, function(error) {
collection.find({}, log("save(object) + find"));
var object = {hello: "world"};
collection.save(object, function(error) {
collection.find({}, log("save(new)"));
collection.save(object, function(error) {
collection.find({}, log("save(existing)"));
object.hello = 2;
object.foo = "bar";
collection.save(object, function(error) {
collection.find({}, log("save(updated)"));
});
});
});
});
});
Expand Down
8 changes: 7 additions & 1 deletion test/test-collection.out
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,12 @@ find(_id = ObjectId(4e21d7d80123ab0123000004)):
find(array = [1,2,"three"]):
{"array":[1,2,"three"],"_id":"4e21d7dc0123ab0123000008"}

save(object) + find:
save(new):
{"hello":"world","_id":"4e21d7dd0123ab0123000009"}

save(existing):
{"hello":"world","_id":"4e21d7dd0123ab0123000009"}

save(updated):
{"hello":2,"_id":"4e21d7dd0123ab0123000009","foo":"bar"}

0 comments on commit e92b1e8

Please sign in to comment.