Showing posts with label mongodb. Show all posts
Showing posts with label mongodb. Show all posts

Saturday, May 25, 2019

13 – Spring boot : connecting to MongoDB


Spring boot connection to mongoDB


application.properties

    
#mongodb
spring.data.mongodb.host=localhost
spring.data.mongodb.port=27017
spring.data.mongodb.database=mongotuto
 
#logging
logging.level.org.springframework.data=debug
logging.level.=error
    
  

POM.xml

    

<!--xml version="1.0" encoding="UTF-8"?-->
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 <modelVersion>4.0.0</modelVersion>
 <parent>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-parent</artifactId>
  <version>2.1.4.RELEASE</version>
  <relativePath/> <!-- lookup parent from repository -->
 </parent>
 <groupId>com.example</groupId>
 <artifactId>SpringBootMongoDB03</artifactId>
 <version>0.0.1-SNAPSHOT</version>
 <name>SpringBootMongoDB03</name>
 <description>Demo project for Spring Boot</description>

 <properties>
  <java.version>1.8</java.version>
 </properties>

 <dependencies>
  <dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-data-mongodb</artifactId>
  </dependency>
  <dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-web</artifactId>
  </dependency>

  <dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-devtools</artifactId>
   <scope>runtime</scope>
  </dependency>
  <dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-test</artifactId>
   <scope>test</scope>
  </dependency>
 </dependencies>

 <build>
  <plugins>
   <plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
   </plugin>
  </plugins>
 </build>

</project>
  
  


Application Main class


package com.example.discovery;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class SpringBootMongoDb03Application {

 public static void main(String[] args) {
  SpringApplication.run(SpringBootMongoDb03Application.class, args);
 }

}


  


Entity Car

package com.example.discovery;

import org.bson.types.ObjectId;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;

@Document(collection = "Car")
public class Car {

 @Id 
 public ObjectId _id;
 public String brand;
 public String name;
 public String color;
 
 public Car(ObjectId _id, String name, String brand, String color) {
      this._id = _id;
      this.name = name;
      this.brand = brand;
      this.color = color;
    }

 public ObjectId get_id() {
  return _id;
 }

 public void set_id(ObjectId _id) {
  this._id = _id;
 }

 public String getbrand() {
  return brand;
 }

 public void setbrand(String brand) {
  this.brand = brand;
 }

 public String getName() {
  return name;
 }

 public void setName(String name) {
  this.name = name;
 }

 public String getcolor() {
  return color;
 }

 public void setcolor(String color) {
  this.color = color;
 }
 
 
}

  



CarRepository

package com.example.discovery;

import org.bson.types.ObjectId;
import org.springframework.data.mongodb.repository.MongoRepository;

public interface CarRepository extends MongoRepository {
   Car findBy_id(ObjectId _id);
 }
  


CarController

package com.example.discovery;

import java.util.List;

import javax.validation.Valid;

import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/cars")
public class CarController {
  @Autowired
  private CarRepository repository;
  
  @RequestMapping(value = "/", method = RequestMethod.GET)
  public List getAllCars() {
    return repository.findAll();
  }
  
  @RequestMapping(value = "/{id}", method = RequestMethod.GET)
  public Car getCarById(@PathVariable("id") ObjectId id) {
    return repository.findBy_id(id);
  }
  
  @RequestMapping(value = "/", method = RequestMethod.POST)
  public Car createCar(@Valid @RequestBody Car Cars) {
    Cars.set_id(ObjectId.get());
    repository.save(Cars);
    return Cars;
  }
  
  @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
  public void deleteCar(@PathVariable ObjectId id) {
    repository.delete(repository.findBy_id(id));
  }
  
  @RequestMapping(value = "/{id}", method = RequestMethod.PUT)
  public void modifyCarById(@PathVariable("id") ObjectId id, @Valid @RequestBody Car Cars) {
    Cars.set_id(id);
    repository.save(Cars);
  }
  
 
}

  


This is just a trick, hope it paved your way out. 

Wednesday, April 6, 2016

trying to open unclosed connection

When having the error, the first thing to do is to restarting your mongo database.
If the error still persist, chances are that your connection statement is as below:

var dbUrl= 'mongodb://localhost:27017/test';
mongoose.connect(dbUrl);

To solve it , change connect to createConnection

var dbUrl= 'mongodb://localhost:27017/test';
mongoose.createConnection(dbUrl);


It should be working now, if not drop me a comment and will come to your rescue...


Monday, March 14, 2016

TypeError: require(...).connect is not a function

I recently had  a hard time trying to fix this issue.
I initially wrote these lines below to establish my connection:

var databaseUrl = "localhost/school";
var collections = ["student", "professor"]
var db = require("mongojs").connect(databaseUrl, collections);
var http = require("http");


Now it seems that since 1.x , mongojs no longer support the connect function,
instead we should replace the third line with:


var db = require("mongojs")(databaseUrl, collections);


Source: https://github.com/mafintosh/mongojs/issues/286


 

the default storage engine 'wiredtiger' is not available issue solved

I have created my data/db in the bin folder, for more about the installation watch this tutorial

Now to start mongod, instead of :

mongod --dbpath ./data/db

Do this instead:

mongod --storageEngine=mmapv1 --dbpath ./data/db

Hope it helps:



mongodb unclean shutdown detected

Are you having this error message : mongodb unclean shutdown detected ?
In my system I have my data/db under the bin folder
if you are using linux :

sudo rm /data/db/mongod.lock
sudo mongod --dbpath /data/db --repair
sudo mongod --storageEngine=mmapv1 --dbpath ./data/db
 
 
if you are using windows:
 
Go to your data/db directory:
1. delelte mongod.lock file
2. mongod --dbpath ./data/db --repair
3. mongod --storageEngine=mmapv1 --dbpath ./data/db 
 
More about the issue here