忘掉Mongoose吧,这是更优雅的Mongorito

MongoDB安装(mac)

# 进入 /usr/local
cd /usr/local

# 下载
sudo curl -O https://fastdl.mongodb.org/osx/mongodb-osx-x86_64-3.4.2.tgz

# 解压
sudo tar -zxvf mongodb-osx-x86_64-3.4.2.tgz

# 重命名为 mongodb 目录
sudo mv mongodb-osx-x86_64-3.4.2 mongodb

# 把安装目录/bin添加到 PATH 路径中
export PATH=/usr/local/mongodb/bin:$PATH

# 建立数据库存储目录 /data/db
cd /
sudo mkdir data
cd /data
sudo mkdir db

# 启动 mongodb,默认数据库目录即为 /data/db
sudo mongod
# 若是没有建立全局路径 PATH,须要进入如下目录
cd /usr/local/mongodb/bin
sudo ./mongod复制代码

Mongorito

MongoDB ORM for Node based on Reduxjavascript

安装

npm install mongorito --save
# or
yarn add mongorito复制代码

建立数据库链接

const { Database } = require('mongorito');
const db = new Database('localhost/test');
async function do(){
    await db.connect();   
}复制代码

建立Model

//class “Model”没有任何属性和方法
const { Model } = require('mongorito'); 

class Post extends Model {}

// 在数据库里注册 Model
db.register(Post);复制代码

建立Document

// 建立一个Model的实例
const post = new Post();
//or 
const post = new Post({
    title: 'First Post',
    author: {
        name: 'Neil Lin',
        age: 23
    },
    content: 'This is content',
    created: Date.now()
})复制代码

获取document中的字段

const title = post.get('title');
const author = post.get('author.name')复制代码

更新

async function do(){
    post.set('title','Second Post');
    post.set({
        author: {
            name: 'monica'
        },
        content: 'This content has already updated.'
    })

    await post.save()   
}复制代码

删除

async function do(){
    // 删除单个字段
    post.unset('created');

    // 删除多个字段
    post.unset(['created','author.age'])

    await post.save();

    // 删除document
    await post.remove();   
}复制代码

自增字段

async function do(){
    const post = new Post({
        views: 0
    })

    await post.increment('views');

    post.get('views'); // => 1

    await post.increment('views', 2);

    post.get('views'); // => 3
}复制代码

一次增长多个字段java

async function do(){
    const post = new Post({
        views: 10,
        comments: 10
    })

    await post.increment({
        views: 5,
        comments: 2
    })

    post.get('views') // => 15
    post.get('comments') // => 12
}复制代码

嵌套其余Model

class Post extends Model {}
class Author extends Model {}
class Comments extends Model {}

Post.embeds('author', Author)
Post.embeds('comments', Comments)

const post = new Post({
    title: 'Great post',
    author: new Author({name: 'Steve'}),
    comments: [new Comment({body: 'Interesting!'})]
})
//or
const post = new Post({
    title: 'Great post',
    author: {
        name: 'Steve'
    },
    comments: [{
        body: 'Interesting!'
    }]
})

async function do(){
    await post.save();
}复制代码

查询

async function do(){
    // 查找所有posts
    await Post.find();

    // 查找tag为"js"的posts
    await Post.find({tag: "js"});
    // or
    await Post.where({tag: "js"}).find();

    // 查找最近5条posts
    await Post
            .limit(5)
            .sort('created','desc')
            .find();

    // 查找其中一条
    await Post.findOne({title: 'First Post'});

    // 获取posts条数
    await Post.count({author: 'Neil Lin'});
}复制代码

插件

使用第三方插件git

const timestamp = require('mongorito-timestamps');

db.use(timestamp({
    createdAt: 'created', // default 'created_at'
    updatedAt: 'updated' // default 'updated_at'
}))
// 或者只用于某个Model
Post.use(timestamp({
    createdAt: 'created', // default 'created_at'
    updatedAt: 'updated' // default 'updated_at'
}))

async function do(){
    const post = new Post({title: 'Hello'});
    await post.save();
    post.get('created_at');
    //=> 2017-05-17T18:02:06.612Z
}复制代码

编写插件github

const extendPost = Post => {
    Post.Recently = function() {
        return this
            .limit(5)
            .sort('created', 'desc')
            .find();
    }

    Post.prototype.setTag = function(tag) {
        this.set('tag',tag);
    }
}

Post.use(extendPost);

async function do(){
    const post = new Post();

    post.setTag('mongodb');

    await post.Recently(); //=> [Post, Post, Post]
}复制代码
相关文章
相关标签/搜索