Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

mari tang
mari tang

Posted on • Edited on

     

Some mongoDB + mongoose + NodeJS basics

warnings

Note that this is strictly a hello-world kind of exercise. In a real environment, you'd probably have some more security configuration on your database. This creates an entirely unprotected database that can be read or modified by anyone who connects to it. With that said, here's a guide for how to go from a basic understanding of NodeJS to reading from / writing to a mongoDB database.

getting MongoDB's process running

Here's a big-picture breakdown of MongoDB. MongoDB is made up ofdatabases, which havecollections inside of them, which contain multipledocuments. Inmongoose, thedocuments are essentially javascript objects that follow a pattern that follows aschema that we define.

In order to get anything done in our database, we need to have a process running that will allow users to access it. In order to do this, you want to install MongoDB, and then runmongod. You may have errors regarding permissions in /data/db. This is because /data/db is a protected folder, and you may need to run mongod as a superuser (which may not be the best- I'm not clear on the security implications here). it looks like this:sudo mongod. (Leave this window open so that the process can continue to run, and don't touch it unless you want to shut down your database.)

When you typesudo mongod, part of what shows up on your console should look like this:MongoDB starting : pid=2265 port=27017 dbpath=/data/db. The port for a mongoDB instance is 27017 by default, but it will always be printed when you start up mongod. just look forport. This will be important later.

Now, your database will be stored at /data/db, though not in any format that seems to be readable to us. We can access our databases through the mongo shell, which we'll get into later.

Creating and configuring our NodeJS server

Now, we can finally get into creating and configuring our server.

First, we have tonpm init in our desired directory to create our server, then we'llnpm install mongodb andnpm install mongoose as a bare minimum for a proof of concept / hello world-type server.

We'll create a file calledserver.js in our project folder that we'll run usingnode server.js. Thisserver.js will contain all of the logic that our server follows.

Our goal is to create a database, write to it, and read from it, all from our server. Here's a quick roadmap of how we'll do it:

  1. import mongoose
  2. connect to our database
  3. create aschema object using mongoose.Schema() constructor and an object that defines the criteria (keys, datatype, etc) for our data
  4. create amodel by using mongoose.connect(), with the name of ourcollection, and theschema object we just created
  5. use themodel as a constructor function to generate a mongoDBdocument object
  6. the mongoDBdocument object that we have returned will have a method called save(), which will allow us to save it to the database.
  7. If we want to find our data afterwards, ourmodel (not the document), has a method called 'find()'. Note that our database methods are asynchronous.

step 1: import mongoose inside of our server file.

//in server.jsconstmongoose=require('mongoose')
Enter fullscreen modeExit fullscreen mode

Next, let's connect to our database. Once again, if you've runmongod in your CLI, it should print out some information, including a port number (27017 by default). If you're connecting to a remote host, they'll provide the information required to log in.

step 2: connect to db

You'll connect atmongodb://localhost:<portnumber>/<databasename>, and it'll look like this:

//in server.jsconstmongoose=require('mongoose')mongoose.connect('mongodb://localhost:27017/test',{useNewUrlParser:true});
Enter fullscreen modeExit fullscreen mode

You may have noticed that we've added 'test' to the end ofmongodb://localhost:27017/test.test is the name that we're choosing to call our database. We haven't created this database yet, but it doesn't matter, because in MongoDB, it will be created when we start storing documents on it.

Now that this connection has been established, we can start creating schemas, models, and documents. This starts with ourmongoose.schema method, which is a constructor function that lives onmongoose

Let's store ourmongoose.schema on a const, so we can access it more easily.

step 2.5: save our schema method

//in server.jsconstmongoose=require('mongoose')mongoose.connect('mongodb://localhost:27017/test',{useNewUrlParser:true});constSchema=mongoose.Schema
Enter fullscreen modeExit fullscreen mode

mongoose.Schema is a constructor that takes an object.

The object that we give to the constructor is a 'schema' that represents the structure of our data, with key/value pairs.

Step 3: create a schema

In our example code, our schema will be called exampleSchema.

//in server.jsconstmongoose=require('mongoose')mongoose.connect('mongodb://localhost:27017/test',{useNewUrlParser:true});constSchema=mongoose.SchemaconstexampleSchema=newSchema({someKeyName:{type:String,required:true,unique:true},someOtherKeyName:{type:Number,required:false,unique:false}})
Enter fullscreen modeExit fullscreen mode

What this means is that each object in our collection is going to have two keys: someKeyName and someOtherKeyName. The value stored on someKeyName will have a type ofString, will be required, and will be unique. The value stored on someOtherKeyName will need to be aNumber, and is neither required nor unique (I'm not sure what unique is).

Step 4: Create a Model

Next, we'll create something called a 'model', which will link our schema to our collection.

we invokemongoose.model(), which takes two arguments, one of which is a string that is the name of our collection, the other of which is the new object we created. Once again, it doesn't matter that we haven't already created a collection. MongoDB will create it for us once we try to store data inside of it.

//in server.jsconstmongoose=require('mongoose')mongoose.connect('mongodb://localhost:27017/test',{useNewUrlParser:true});constSchema=mongoose.SchemaconstexampleSchema=newSchema({someKeyName:{type:String,required:true,unique:true},someOtherKeyName:{type:Number,required:false,unique:false}})constnewModel=mongoose.model('collectionName',exampleSchema)
Enter fullscreen modeExit fullscreen mode

that step links the schema with a name for your collection (which produces a 'model'). This model is actually a constructor function, which creates 'document' objects.

step 5: Create a document

In order to do this, we take our model (newModel), and give it an object that holds all of the data that we want to go in a single document.

//in server.jsconstmongoose=require('mongoose')mongoose.connect('mongodb://localhost:27017/test',{useNewUrlParser:true});constSchema=mongoose.SchemaconstexampleSchema=newSchema({someKeyName:{type:String,required:true,unique:true},someOtherKeyName:{type:Number,required:false,unique:false}})constnewModel=mongoose.model('collectionName',exampleSchema)constnewDocument=newModel({someKeyName:'hi',someOtherKeyName:12345})
Enter fullscreen modeExit fullscreen mode

step 6: save the document

Now thenewDocument object exists, we have access to methods on it. One of these is asave method. It does exactly what you think it does.

//in server.jsconstmongoose=require('mongoose')mongoose.connect('mongodb://localhost:27017/test',{useNewUrlParser:true});constSchema=mongoose.SchemaconstexampleSchema=newSchema({someKeyName:{type:String,required:true,unique:true},someOtherKeyName:{type:Number,required:false,unique:false}})constnewModel=mongoose.model('collectionName',exampleSchema)constnewDocument=newModel({someKeyName:'hi',someOtherKeyName:12345})newDocument.save()
Enter fullscreen modeExit fullscreen mode

Congrats! you just used mongoose to create and write to a database.

Let's go ahead and verify that the document exists in our collection. You can do this outside of node through the next sequence of commands:

mongo will spin up the mongo command line.

inside of the mongo command line interface, you can type the following:

show dbs will show you a list of dbs. You should seetest on the list.

use test will log you into yourtest database.

show collections will show you the collections in yourtest database. You should see acollectionName collection printed out.

db.collectionName.find() will do an empty query on thecollectionName database, which should return every document within that collection.

you should see something like this:{ "_id" : ObjectId("5c78204590796a1c74a20b11"), "someKeyName" : "hi", "someOtherKeyName" : 1234, "__v" : 0 }

There's your database entry!

Of course, this is not so useful for us. Given that we're trying to work with our database from inside of nodeJS, we need to be able to read from our database within our server file.

Back inside of the server, let's invoke a 'find' on our model in order to read from the database.

//in server.jsconstmongoose=require('mongoose')mongoose.connect('mongodb://localhost:27017/test',{useNewUrlParser:true});constSchema=mongoose.SchemaconstexampleSchema=newSchema({someKeyName:{type:String,required:true,unique:true},someOtherKeyName:{type:Boolean,required:false,unique:false}})constnewModel=mongoose.model('collectionName',exampleSchema)newModel.find({somekeyname:'hi'},(err,results)=>{console.log(results)})
Enter fullscreen modeExit fullscreen mode

This may require a bit of explanation. The first argument is fairly clear. it's an object with key/value pairs that we're searching for. The second argument (called a callback function) may be a little less obvious.

Basically, we expectnewModel.find to query our database, then do something with the data. What does it do? That's defined in our callback function. If the search fails for some reason, it'll pass in an argument aserr, and if it succeeds, we'll get something inresults.

{someKeyName: "hi"} should match our entry{ "_id" : ObjectId("5c78204590796a1c74a20b11"), "someKeyName" : "hi", "someOtherKeyName" : 1234, "__v" : 0 }, and we don't have anything else in our database, so ourresults variable will evaluate to this:[{ "_id" : ObjectId("5c78204590796a1c74a20b11"), "someKeyName" : "hi", "someOtherKeyName" : 1234, "__v" : 0 }].console.log will simply print that value to our terminal.

Note that, even though it's just one item, the callback forfind returns an array. If it doesn't find anything, it'll just give an empty array[].

Now that you've got the array of matches, you can start to do stuff with it, whether that's looking up and displaying information, checking authentication, or other such operations!

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

"Poets do not go mad; but chess-players do. Mathematicians go mad, and cashiers; but creative artists very seldom." -GK Chesterton
  • Work
    Software Engineer at PayPal
  • Joined

More frommari tang

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp