PageSourceSearch

https://ottomanjs.com/assets/js/db501fe4.91bf3246.js

js ottomanjs.com collected 2026-09-25 21:45:32 UTC 10,364 bytes, 1 lines download raw bytes

1"use strict";(self.webpackChunkottoman_documentation=self.webpackChunkottoman_documentation||[]).push([[1152],{4998:(e,n,o)=>{o.r(n),o.d(n,{assets:()=>c,contentTitle:()=>i,default:()=>g,frontMatter:()=>s,metadata:()=>t,toc:()=>d});const t=JSON.parse('{"id":"advanced/mongoose-to-couchbase","title":"Mongoose to Ottoman","description":"Migrate MongoDB to Couchbase","source":"@site/docs/advanced/mongoose-to-couchbase.md","sourceDirName":"advanced","slug":"/advanced/mongoose-to-couchbase","permalink":"/docs/advanced/mongoose-to-couchbase","draft":false,"unlisted":false,"tags":[],"version":"current","frontMatter":{},"sidebar":"tutorialSidebar","previous":{"title":"Advanced Concepts","permalink":"/docs/advanced/ottoman"},"next":{"title":"Ottoman for Couchbase Node.js SDK developers","permalink":"/docs/advanced/ottoman-couchbase"}}');var a=o(4848),l=o(8453);const s={},i="Mongoose to Ottoman",c={},d=[{value:"Moving Data from MongoDB to Couchbase",id:"moving-data-from-mongodb-to-couchbase",level:2},{value:"Defining Schema",id:"defining-schema",level:2},{value:"Mongoose",id:"mongoose",level:4},{value:"Ottoman",id:"ottoman",level:4},{value:"Creating Model",id:"creating-model",level:2},{value:"Mongoose",id:"mongoose-1",level:4},{value:"Ottoman",id:"ottoman-1",level:4},{value:"Connection and Save Model Instance",id:"connection-and-save-model-instance",level:2},{value:"Mongoose",id:"mongoose-2",level:4},{value:"Ottoman",id:"ottoman-2",level:4},{value:"Instance Methods",id:"instance-methods",level:2},{value:"Mongoose",id:"mongoose-3",level:4},{value:"Ottoman",id:"ottoman-3",level:4},{value:"Statics",id:"statics",level:2},{value:"Mongoose",id:"mongoose-4",level:4},{value:"Ottoman",id:"ottoman-4",level:4},{value:"Querying",id:"querying",level:2},{value:"Mongoose",id:"mongoose-5",level:4},{value:"Ottoman",id:"ottoman-5",level:4},{value:"Deleting",id:"deleting",level:2},{value:"Mongoose",id:"mongoose-6",level:4},{value:"Ottoman",id:"ottoman-6",level:4},{value:"Updating",id:"updating",level:2},{value:"Mongoose",id:"mongoose-7",level:4},{value:"Ottoman",id:"ottoman-7",level:4}];function r(e){const n={a:"a",code:"code",h1:"h1",h2:"h2",h4:"h4",header:"header",img:"img",p:"p",pre:"pre",...(0,l.R)(),...e.components};return(0,a.jsxs)(a.Fragment,{children:[(0,a.jsx)(n.header,{children:(0,a.jsx)(n.h1,{id:"mongoose-to-ottoman",children:"Mongoose to Ottoman"})}),"\n",(0,a.jsx)(n.p,{children:(0,a.jsx)(n.img,{alt:"Migrate MongoDB to Couchbase",src:o(7844).A+"",width:"1024",height:"408"})}),"\n",(0,a.jsx)(n.h2,{id:"moving-data-from-mongodb-to-couchbase",children:"Moving Data from MongoDB to Couchbase"}),"\n",(0,a.jsxs)(n.p,{children:["In ",(0,a.jsx)(n.a,{href:"https://blog.couchbase.com/migrating-data-from-mongodb-to-couchbase/",children:"this post"})," 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."]}),"\n",(0,a.jsx)(n.p,{children:"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."}),"\n",(0,a.jsx)(n.h2,{id:"defining-schema",children:"Defining Schema"}),"\n",(0,a.jsx)(n.p,{children:"Everything in Ottoman and Mongoose starts with a Schema."}),"\n",(0,a.jsx)(n.h4,{id:"mongoose",children:"Mongoose"}),"\n",(0,a.jsx)(n.pre,{children:(0,a.jsx)(n.code,{className:"language-javascript",children:"import mongoose from 'mongoose';\nconst { Schema } = mongoose;\n\nconst blogSchema = new Schema({\n  title: { type: String },\n  author: String, // String is shorthand for { type: String }\n  body:   String,\n  comments: [{ body: String, date: Date }],\n  date: { type: Date, default: Date.now },\n  hidden: Boolean,\n  meta: {\n    votes: { type: Number, min: 0, max: 5 },\n    favs:  Number\n  }\n});\n"})}),"\n",(0,a.jsx)(n.h4,{id:"ottoman",children:"Ottoman"}),"\n",(0,a.jsx)(n.pre,{children:(0,a.jsx)(n.code,{className:"language-javascript",children:"import { Schema } from 'ottoman';\n\nconst blogSchema = new Schema({\n  title: { type: String },\n  author: String, // String is shorthand for { type: String }\n  body: String,\n  comments: [{ body: String, date: Date }],\n  date: { type: Date, default: Date.now },\n  hidden: Boolean,\n  status: { type: String, enum: ['Close', 'Open', 'Review'] },\n  meta: {\n    votes: { type: Number, min: 0, max: 5 },\n    favs: Number,\n  }\n});\n"})}),"\n",(0,a.jsx)(n.h2,{id:"creating-model",children:"Creating Model"}),"\n",(0,a.jsx)(n.h4,{id:"mongoose-1",children:"Mongoose"}),"\n",(0,a.jsx)(n.pre,{children:(0,a.jsx)(n.code,{className:"language-javascript",children:"const Blog = mongoose.model('Blog', blogSchema);\n"})}),"\n",(0,a.jsx)(n.h4,{id:"ottoman-1",children:"Ottoman"}),"\n",(0,a.jsx)(n.pre,{children:(0,a.jsx)(n.code,{className:"language-javascript",children:"import { model } from 'ottoman';\n\nconst Blog = model('Blog', blogSchema);\n"})}),"\n",(0,a.jsx)(n.h2,{id:"connection-and-save-model-instance",children:"Connection and Save Model Instance"}),"\n",(0,a.jsx)(n.h4,{id:"mongoose-2",children:"Mongoose"}),"\n",(0,a.jsx)(n.pre,{children:(0,a.jsx)(n.code,{className:"language-javascript",children:"import { connect } from 'mongoose';\n\nasync function createBlog() {\n  await connect('mongodb://localhost:27017/test');\n\n  const blog = new Blog({ name: 'Jane Doe' });\n\n  try {\n    const newBlog = await blog.save();\n    console.log(`Blog '${ newBlog.title }' successfully created`);\n  }\n  catch (e) {\n    console.log(`ERROR: ${e.message}`);\n  }\n}\n\ncreateBlog();\n"})}),"\n",(0,a.jsx)(n.h4,{id:"ottoman-2",children:"Ottoman"}),"\n",(0,a.jsx)(n.pre,{children:(0,a.jsx)(n.code,{className:"language-javascript",children:"import { connect, start, close } from 'ottoman';\n\nasync function createBlog() {\n    const connection = await connect('couchbase://localhost/travel-sample@Administrator:password');\n    const blog = new Blog({ name: 'Jane Doe' });\n\n    try {\n        await start();\n        const newBlog = await blog.save();\n        await close();\n        console.log(`Blog '${ newBlog.title }' successfully created`);\n    }\n    catch (e) {\n        console.log(`ERROR: ${e.message}`);\n    }\n}\n\ncreateBlog();\n"})}),"\n",(0,a.jsx)(n.h2,{id:"instance-methods",children:"Instance Methods"}),"\n",(0,a.jsx)(n.h4,{id:"mongoose-3",children:"Mongoose"}),"\n",(0,a.jsx)(n.pre,{children:(0,a.jsx)(n.code,{className:"language-javascript",children:"blogSchema.methods.findSimilarTypes = function(cb) {\n  return mongoose.model('Blog').find({ type: this.type }, cb);\n};\n\n// Now all of our blogs instances have a findSimilarTypes method available to them.\nconst BlogModel = mongoose.model('Blog', blogSchema);\nconst newBlog = new BlogModel({ title: 'Blog title' });\n\nconst blogs = await newBlog.findSimilarTypes();\nconsole.log(blogs);\n"})}),"\n",(0,a.jsx)(n.h4,{id:"ottoman-3",children:"Ottoman"}),"\n",(0,a.jsx)(n.pre,{children:(0,a.jsx)(n.code,{className:"language-javascript",children:"blogSchema.methods.findSimilarTypes = function() {\
1n  return connection.getModel('Blog').find({ type: this.type });\n};\n\n// Now all of our blogs instances have a findSimilarTypes method available to them.\nconst BlogModel = model('Blog', blogSchema);\nconst newBlog = new BlogModel({ title: 'Blog title' });\n\nconst blogs = await newBlog.findSimilarTypes();\nconsole.log(blogs);\n"})}),"\n",(0,a.jsx)(n.h2,{id:"statics",children:"Statics"}),"\n",(0,a.jsx)(n.h4,{id:"mongoose-4",children:"Mongoose"}),"\n",(0,a.jsx)(n.pre,{children:(0,a.jsx)(n.code,{className:"language-javascript",children:"blogSchema.statics.findByTitle = function(title) {\n  return this.find({ title: title });\n};\n\nconst BlogModel = mongoose.model('Blog', blogSchema);\nconst blogs = await BlogModel.findByTitle(`Some blog's title`);\n"})}),"\n",(0,a.jsx)(n.h4,{id:"ottoman-4",children:"Ottoman"}),"\n",(0,a.jsx)(n.pre,{children:(0,a.jsx)(n.code,{className:"language-javascript",children:"blogSchema.statics.findByTitle = function(title) {\n  return this.find({ title: title });\n};\n\nconst BlogModel = model('Blog', blogSchema);\nconst blogs = await BlogModel.findByTitle(`Some blog's title`);\n"})}),"\n",(0,a.jsx)(n.h2,{id:"querying",children:"Querying"}),"\n",(0,a.jsx)(n.h4,{id:"mongoose-5",children:"Mongoose"}),"\n",(0,a.jsx)(n.pre,{children:(0,a.jsx)(n.code,{className:"language-javascript",children:"BlogModel.find({ title: `Some blog's title` }).exec(callback);\n"})}),"\n",(0,a.jsx)(n.h4,{id:"ottoman-5",children:"Ottoman"}),"\n",(0,a.jsx)(n.pre,{children:(0,a.jsx)(n.code,{className:"language-javascript",children:"const response = await BlogModel.find({ title: `Some blog's title` });\n"})}),"\n",(0,a.jsx)(n.h2,{id:"deleting",children:"Deleting"}),"\n",(0,a.jsx)(n.h4,{id:"mongoose-6",children:"Mongoose"}),"\n",(0,a.jsx)(n.pre,{children:(0,a.jsx)(n.code,{className:"language-javascript",children:"BlogModel.findByIdAndDelete('id');\n"})}),"\n",(0,a.jsx)(n.h4,{id:"ottoman-6",children:"Ottoman"}),"\n",(0,a.jsx)(n.pre,{children:(0,a.jsx)(n.code,{className:"language-javascript",children:"const response = await BlogModel.removeById('id');\n"})}),"\n",(0,a.jsx)(n.h2,{id:"updating",children:"Updating"}),"\n",(0,a.jsx)(n.h4,{id:"mongoose-7",children:"Mongoose"}),"\n",(0,a.jsx)(n.pre,{children:(0,a.jsx)(n.code,{className:"language-javascript",children:"BlogModel.updateOne({ title: `Some blog's title` }, { title: 'New Blog title' }, cb);\n"})}),"\n",(0,a.jsx)(n.h4,{id:"ottoman-7",children:"Ottoman"}),"\n",(0,a.jsx)(n.pre,{children:(0,a.jsx)(n.code,{className:"language-javascript",children:"const response = await BlogModel.findOneAndUpdate({ title: `Some blog's title` }, { title: 'New Blog title' });\n"})})]})}function g(e={}){const{wrapper:n}={...(0,l.R)(),...e.components};return n?(0,a.jsx)(n,{...e,children:(0,a.jsx)(r,{...e})}):r(e)}},7844:(e,n,o)=>{o.d(n,{A:()=>t});const t=o.p+"assets/images/mongodb-to-couchbase-37a3b7b42478d45f635069022e4a362f.jpg"},8453:(e,n,o)=>{o.d(n,{R:()=>s,x:()=>i});var t=o(6540);const a={},l=t.createContext(a);function s(e){const n=t.useContext(l);return t.useMemo(function(){return"function"==typeof e?e(n):{...n,...e}},[n,e])}function i(e){let n;return n=e.disableParentContext?"function"==typeof e.components?e.components(a):e.components||a:s(e.components),t.createElement(l.Provider,{value:n},e.children)}}}]);

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.