Geek Culture

A new tech publication by Start it up (https://medium.com/swlh).

Learn Mongo DB in less than 5 minutes

Miriam Bellon
Geek Culture
Published in
3 min readJun 7, 2021

--

When you are learning Node.js is a must to learn about database. MongoDB and Mongoose are going to be your best friends in that case. We can say that Mongo DB is a NoSQL database which means that is a document-oriented database and all the data can be stored in a document file — in this case, will be a JSON document. The structure is very simple: in MongoDB you can create a collection on each database. Each collection has a key and a value. NoSQL database makes changes easily thanks to these inputs.

If you are familiar with JSON, you know that you can add more keys and values to an object:

{
field1: value1,
field2: value2,
field3: value3,
field4:{
fiel4-1: value4-1,
field4-2: value4-2
}
}

You might think, what if I don't give any id to the object? Don’t worry, MongoDB will add the id for you in case you didn’t.

Got it! But why do we need Mongoose?

Mongoose is a JavaScript Library which defines an specific model based on a Schema — which is a JSON structure that has information about the properties in the file such as validation.

But first, we need to connect our database with connect() method. connect() has a mandatory parameter which is the link to MongoDB database or uri. After that, we need to use listen() method to run the server.

When the Schema is defined, we can create a model which is assigned to a document file on MongoDB. Thanks to Mongoose you can save, delete, or validate your data — we can also validate our data with express-validate — using async fuctions from MongoDB. This is an example of how to define a Schema:

const testSchema = mongoose.Schema({
name: String,
})

There is a variety of data that can be defined on a Schema:

  • String
  • Number
  • Boolean
  • Buffer (binary type)
  • Date
  • Array
  • Schema.Types.ObjectIdString (hex)
  • Schema.Types.Mixed (any data)

We can use done() in order to proceed after an asynchronous operation is done. It can be called as a success — done(null, data) — or as an error done(err).

Managing our data base

Let’s see some queries we can use when managing our data:

  • Consult in our database: we can use find() in order to consult inside a collection.
db.collection.find()

When we want to consult a specific document with a specific value in a collection we use

db.collection.find({field:"value"})
  • Create some instances of your models: create() takes an array of objects as a first argument and saves all in the database.
  • Find in our database:

We can use find() and it takes an object as a first argument and a callback. This returns an array of matches.

We can also use findOne() and it will return a single matching document.

Searching by _id it’s also possible, using findById() in order to find an alphanumeric key.

  • Insert in our database: we can use some methods. First, we can use insert()
db.collection.insert( { _id: 10, type: "misc", item: "book", qty: 5 
} )
  • Update in our database: we can use update() method and inside we need to add the flag upsert. We need to indicate the id of the object we want to update if not this is going to be a new element in the data base.
db.collection.update( 
{ _id: 10, type: "misc", item: "book", qty: 5 },
{upsert:true}
)

You can also use the flag { multi: true } in order to update more than one object.

  • You can insert a document using save() method.
db.collection.save(function(err, data) { _id: 10, type: "misc", item: "book", qty: 5 
} )
  • You can also insert one document by using insertOne() method
db.collection.insertOne( { _id: 10, type: "misc", item: "book", qty: 5 
} )
  • To remove a document you can use remove() or drop() methods:

remove() removes the documents that has an specific criteria or pass an empty document which remove the present document.

drop() removes all the documents, the id and remove the collection. This method is more efficient.

db.collection.remove( { type: "misc"} )db.collection.drop( { type: "misc" } )

--

--

Miriam Bellon
Miriam Bellon

Written by Miriam Bellon

Half UX/UI Designer, haft Art Director with a touch of web dev. Sometimes I write articles. You can find me on: www.miriam-bellon.com

No responses yet