Skip to content

Instantly share code, notes, and snippets.

@hpcslag
Created April 9, 2015 14:10
Show Gist options
  • Save hpcslag/b5cbb6ea6d830cdd6965 to your computer and use it in GitHub Desktop.
Save hpcslag/b5cbb6ea6d830cdd6965 to your computer and use it in GitHub Desktop.
Mongodb用法
/*
* This is Official API Note, use mongojs!
*/
var mongojs = require('mongojs');
var db = mongojs('test',['table']); //test 是db的名稱,不需要create. table是集合的名稱
/**
* Search All(Find())
* @class
*/
db.table.find(function(err,doc){
console.log(doc);
});
/**
* Search Data (FindOne)
* @class
*/
db.table.findOne({
query: { name: 'Mac' }
},function(err,doc){
console.log(doc);
});
/**
* New save Data
* @class
*/
db.table.save({ name : "Mac"})
/**
* Remove data
* @class
*/
db.table.remove({name:'Mac'})
/**
* Modify Update Data
* @class
*/
db.table.update({
name:'Eric'
}, {$inc:{name:'Mac'}}, {multi:true}, function() {
// the update is complete
});
/**
* Add a new data insert in data
*/
db.table.findAndModify({
query: { name: 'Eric' },
update: { $set: { name:'Mac' } },
new: true
}, function(err, doc, lastErrorObject) {
// doc.tag === 'maintainer'
});
/**
* GOD MODE
*/
db.table.drop();
//分頁功能,跳過10筆搜尋的資料後找到三筆資料拿出來
db.table.find().skip(10).limit(3)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment