Skip to content
Advertisement

Tag: mongodb

Spring Boot + MongoDB Id query

I have a Spring Boot application combined with MongoDB as the persistance layer. I have the following structure: I also have a ResourceRepository: I found online that a way to have the id property returned in the JSON when you perform a GET request like http://localhost:8080/resources/ is to change the id property to Id (uppercase i). Indeed, if the property

How to read date (Timestamp) from MongoDB using Java

Am trying to read date field from MongoDB in below format In the result 2017-01-23 22:16:07.000000, instead of 000 it should be the 812 (Original value: 2017-01-23-10.46.07.812000) Note: Using MongoDB Java driver 3.4. Thank you in advance! Bharathi Answer You can use Java’s SimpleDateFormat to format the date accordingly. For example, assuming you inserted the date in MongoDB using the

Change or override default Behavior of Mongorepository.save(document)

Is there any way to intercept or change the document before MongoRepository.save(e) updates it? I am trying to push a subproperty inside and array in a document. I have tried to manipulate DBObjects by implementing converter(using custom converter) but the $push operation did not work over there. I think to make it work I have to implement something like mongoOperation.update(dbObjectMatch,dbObjectUdate).

MongoDB trying to connect to localhost, Why?

I am currently developing a Java application connected to a remote MongoDB databse. I have implemented the authentication methods fallowing the mongo guides: The app connect properly to the database but there is a thing i can’t understand.It connects well to the remote server,but I don’t know why it try to connect to localhost:27017. So, how can I tell it

Spring Data MongoDB Annotation @CreatedDate isn’t working, when ID is assigned manually

I’m trying to use auditing to save dateCreated and dateUpdated in my objects, but since I set ID manually, there’s some additional work. Following Oliver Gierke’s suggestion in DATAMONGO-946 I’m trying to figure out how to correctly implement it. As original poster in Jira task above, I’ve downloaded example from here https://github.com/spring-guides/gs-accessing-data-mongodb.git and modified it a bit: and and a

Resolve DBRef into Json

I’m getting the following error in a Normalized Data Model structure in MongoDB: It’s caused by this line: Specifically the toJson() part. I have a DBRef Object in my Document, so I can reference a Document from another Collection. An Embedded Document Structure is not option. So how can I fix this? Answer You have to import the DBRef Codec

find in MongoCollection

I have a MongoCollection<Document> in which I assign a collection. I’m trying to find a user by his id. with that I’m getting an error java.lang.ClassCastException: com.mongodb.FindIterableImpl cannot be cast to org.bson.Document When I try I’m getting an error The method find(Bson, Class) in the type MongoCollection is not applicable for the arguments (BasicDBObject, BasicDBObject) Answer Try to create a

Find all objects in collection Java Mongodb

Below code finds the first document in a collection : There does not appear to be a findAll method. How to find all the documents in the collection testCollection ? Answer You have to use the DBCollection.find() method, which Select all documents in collection and get a cursor to the selected documents. So, what you have to do, is:

Encrypt password fields in mongodb

I have following code, it insert the userName and password into database but the password is stored in plain text format. I mean when I’ll look into the db I can see the inserted password. I want to store password in encrypted format How can I achieve this? Answer According to the conversation in the comments, what you mean is

Spring data mongodb. Generating id’s error

I have made an experiment… one common entity for two Spring data’s repositories: – JPA – MongoDB first of all I’ using following libraries versions: spring-data-jpa : 1.7.0.RELEASE spring-data-mongodb : 1.6.0.RELEASE I have an Entity: JPA Repository has following look: MongoDB repository has following look: package ua.home.springdata.investigation.repository.mongo; So… JPA works 🙂 Nothing special 🙂 But MongoDB test is not passed

Advertisement