Reputation: 1
I've created a Spring Boot REST API to create and store Books. This API has a POST createBook()
and a GET getAll()
request. Both requests are working fine, using the method GET http:localhost:8080/books I can see the list of books that I created. The problem is that I needed to execute a query to replace all books with price null to 0, so I was trying to perform that query in 'mongosh'. In the 'mongosh' shell I used the database that I created for this API but there are no collections inside it.
Note: For now I am not using any authentication.
BookService.java:
@RestController
@RequestMapping("/books")
public class BookController {
// Wire the controller to the "BookRepository" for data access
@Autowired
private BookRepository bookRepository;
@Autowired
private BookService bookService;
@GetMapping
public ResponseEntity<List<Book>> getAllBooks() {
List<Book> books = bookRepository.findAll();
return ResponseEntity.ok(books);
}
// Other methods bellow...
BookRepository.java:
public interface BookRepository extends MongoRepository<Book, String> { }
application.properties:
spring.data.mongodb.uri=mongodb://localhost:27017/book_app_db
POSTMAN request:
GET localhost:8080/books
Response:
[
{
"id": "65b95dc2ddb7f820a72bff76",
"title": "title1",
"author": "author1",
"isbn": "9765453734",
"category": null,
"price": "1"
},
{
"id": "65b95e9bddb7f820a72bff77",
"title": "title2",
"author": "author2",
"isbn": "9765355634",
"category": null,
"price": null
},
{
"id": "65b95fc2ddb7f820a72bff78",
"title": "title3",
"author": "author3",
"isbn": "97653567834",
"category": null,
"price": null
},
...
]
mongosh (mongo shell):
C:\Users\ASUS>mongosh
Current Mongosh Log ID: 65c3a4aea4c573e16eb447ec
Connecting to: mongodb://127.0.0.1:27017/?directConnection=true&serverSelectionTimeoutMS=2000&appName=mongosh+2.1.3
Using MongoDB: 7.0.5
Using Mongosh: 2.1.3
For mongosh info see: https://docs.mongodb.com/mongodb-shell/
The server generated these startup warnings when booting
2024-02-03T16:26:40.694+00:00: Access control is not enabled for the database. Read and write access to data and configuration is unrestricted
test> show databases
admin 40.00 KiB
book_app_db 8.00 KiB
config 108.00 KiB
local 72.00 KiB
test_database 72.00 KiB
test> use book_app_db
switched to db book_app_db
book_app_db> show collections
book_app_db>
I tried to use 'show collections' to see the book collection. I expected to see the collection books containing the same documents returned in the GET request.
I also tried to create a new database from 'mongosh' to verify if I could insert documents manually and if the find()
was working and it did - I was able to create a table, a collection and documents and then display them using the find()
.
Upvotes: 0
Views: 60