Skip to content

Instantly share code, notes, and snippets.

@findyourmagic
Last active August 29, 2015 14:08
Show Gist options
  • Select an option

  • Save findyourmagic/a8c506766c04d629ac35 to your computer and use it in GitHub Desktop.

Select an option

Save findyourmagic/a8c506766c04d629ac35 to your computer and use it in GitHub Desktop.

mongo

基础

  • 文档是mongo中的基本单元
  • 集合可以看作是没有模式的表
  • 单实例可以创建多个具有单独权限的数据库
  • 自带索引键"_id"

常用操作CURD

var post = {
	"title" : "title",
	"content" : "here is content",
	"date" : new Date().getTime()
};
  • 插入
db.blog.insert(post);
  • 读取
db.blog.find();
db.blog.findOne();
  • 更新
post.content = "here is new content";
db.blog.update({ title : "title" }, post);
  • 删除
db.blog.remove({ title : "title" });

更新操作详解

  • 使用post替换掉查到的文档
var post = { title: "title", "content":"content" }
db.blog.update({_id : id}, post);
  • 使用修改器 $inc
db.blog.update({_id : id}, {"$inc" : { "pv" : 1 }});
// 查到的文档 pv 增加 1
// 1 是步长 表示每次增加的数量
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment