# Mongoose to Ottoman

Migrate MongoDB to Couchbase

# Moving Data from MongoDB to Couchbase

In this post (opens new window) you will find an extensive explanation that allow you to export the MongoDB data structure, import to Couchbase and make some basic transformations on those documents.

In the following sections we will be looking at some examples of homologous codes between Mongoose and Ottoman that can be useful in the migration process.

# Defining Schema

Everything in Ottoman and Mongoose starts with a Schema.

# Mongoose

import mongoose from 'mongoose';
const { Schema } = mongoose;

const blogSchema = new Schema({
  title: { type: String },
  author: String, // String is shorthand for { type: String }
  body:   String,
  comments: [{ body: String, date: Date }],
  date: { type: Date, default: Date.now },
  hidden: Boolean,
  meta: {
    votes: { type: Number, min: 0, max: 5 },
    favs:  Number
  }
});
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

# Ottoman

import { Schema } from 'ottoman';

const blogSchema = new Schema({
  title: { type: String },
  author: String, // String is shorthand for { type: String }
  body: String,
  comments: [{ body: String, date: Date }],
  date: { type: Date, default: Date.now },
  hidden: Boolean,
  status: { type: String, enum: ['Close', 'Open', 'Review'] },
  meta: {
    votes: { type: Number, min: 0, max: 5 },
    favs: Number,
  }
});
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

# Creating Model

# Mongoose

const Blog = mongoose.model('Blog', blogSchema);
1

# Ottoman

import { model } from 'ottoman';

const Blog = model('Blog', blogSchema);
1
2
3

# Connection and Save Model Instance

# Mongoose

import { connect } from 'mongoose';

async function createBlog() {
  await connect('mongodb://localhost:27017/test');

  const blog = new Blog({ name: 'Jane Doe' });

  try {
    const newBlog = await blog.save();
    console.log(`Blog '${ newBlog.title }' successfully created`);
  }
  catch (e) {
    console.log(`ERROR: ${e.message}`);
  }
}

createBlog();
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

# Ottoman

import { connect, start, close } from 'ottoman';

const connection = connect('couchbase://localhost/travel-sample@Administrator:password');

async function createBlog() {
    const blog = new Blog({ name: 'Jane Doe' });

    try {
        await start();
        const newBlog = await blog.save();
        await close();
        console.log(`Blog '${ newBlog.title }' successfully created`);
    }
    catch (e) {
        console.log(`ERROR: ${e.message}`);
    }
}

createBlog();
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

# Instance Methods

# Mongoose

blogSchema.methods.findSimilarTypes = function(cb) {
  return mongoose.model('Blog').find({ type: this.type }, cb);
};

// Now all of our blogs instances have a findSimilarTypes method available to them.
const BlogModel = mongoose.model('Blog', blogSchema);
const newBlog = new BlogModel({ title: 'Blog title' });

const blogs = await newBlog.findSimilarTypes();
console.log(blogs);
1
2
3
4
5
6
7
8
9
10

# Ottoman

blogSchema.methods.findSimilarTypes = function() {
  return connection.getModel('Blog').find({ type: this.type });
};

// Now all of our blogs instances have a findSimilarTypes method available to them.
const BlogModel = model('Blog', blogSchema);
const newBlog = new BlogModel({ title: 'Blog title' });

const blogs = await newBlog.findSimilarTypes();
console.log(blogs);
1
2
3
4
5
6
7
8
9
10

# Statics

# Mongoose

blogSchema.statics.findByTitle = function(title) {
  return this.find({ title: title });
};

const BlogModel = mongoose.model('Blog', blogSchema);
const blogs = await BlogModel.findByTitle(`Some blog's title`);
1
2
3
4
5
6

# Ottoman

blogSchema.statics.findByTitle = function(title) {
  return this.find({ title: title });
};

const BlogModel = model('Blog', blogSchema);
const blogs = await BlogModel.findByTitle(`Some blog's title`);
1
2
3
4
5
6

# Querying

# Mongoose

BlogModel.find({ title: `Some blog's title` }).exec(callback);
1

# Ottoman

const response = await BlogModel.find({ title: `Some blog's title` });
1

# Deleting

# Mongoose

BlogModel.findByIdAndDelete('id');
1

# Ottoman

const response = await BlogModel.removeById('id');
1

# Updating

# Mongoose

BlogModel.updateOne({ title: `Some blog's title` }, { title: 'New Blog title' }, cb);
1

# Ottoman

const response = await BlogModel.findOneAndUpdate({ title: `Some blog's title` }, { title: 'New Blog title' });
1