Created
September 10, 2015 11:58
-
-
Save katopz/40ff481b898d83476832 to your computer and use it in GitHub Desktop.
Hello world MongoDB via Meteor + Aggregate
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ref : https://github.com/meteorhacks/meteor-aggregate | |
Meteor.startup(function () { | |
// C : insert | |
console.log("## insert"); | |
// insert list | |
var list_id = Lists.insert({ | |
name: "foo", | |
incompleteCount: 1, | |
type: "dummy" | |
}); | |
// insert 1 item | |
Todos.insert({ | |
listId: list_id, | |
text: "first", | |
createdAt: new Date(), | |
checked: true | |
}); | |
// insert list | |
list_id = Lists.insert({ | |
name: "bar", | |
incompleteCount: 2, | |
type: "dummy" | |
}); | |
// insert 2 items | |
Todos.insert({ | |
listId: list_id, | |
text: "first", | |
createdAt: new Date() | |
}); | |
Todos.insert({ | |
listId: list_id, | |
text: "second", | |
createdAt: new Date() | |
}); | |
// R : find * | |
console.log("## find * "); | |
Todos.find().forEach(function (todo) { | |
console.log(todo.text); | |
}); | |
// R : find where | |
console.log("## find where "); | |
Todos.find({"checked": true}).forEach(function (todo) { | |
console.log(todo.text); | |
}); | |
// R : find in | |
console.log("## find in "); | |
Todos.find({text: {$in: ["first"]}}).forEach(function (todo) { | |
console.log(todo.text); | |
}); | |
// R : find or | |
console.log("## find or "); | |
Todos.find({$or: [{text: 'first'}, {text: 'second'}]}).forEach(function (todo) { | |
console.log(todo.text); | |
}); | |
// R : find and | |
console.log("## find and "); | |
Todos.find({text: 'first', "checked": true}).forEach(function (todo) { | |
console.log(todo.text); | |
}); | |
// U : update | |
Lists.update(list_id, { | |
name: "bar!", | |
incompleteCount: 1 | |
}); | |
// D : remove | |
console.log("## remove "); | |
// remove item | |
Todos.remove( | |
{text: {$in: ["first", "second"]}}, | |
function (err, results) { | |
console.log("removed Todos : " + results); | |
} | |
); | |
// remove list | |
Lists.remove( | |
{"type": "dummy"}, | |
function (err, results) { | |
console.log("removed Lists : " + results); | |
} | |
); | |
// aggregate | |
console.log("## aggregate "); | |
Todos.aggregate([ | |
{$match: {"checked": true}}, | |
{$group: {_id: "$listId", count: {$sum: 1}}} | |
]).forEach(function (todo) { | |
console.log(todo); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment