Say I have a User class which has a manual reference to a customer document:
public class User(){
#Id
public String id;
public String name;
public String customerId;
}
I want both the id & customerId to be stored as an ObjectId in mongo.
When saving a User document, the "id" gets converted to an ObjectId, however, the customerId gets saved as a string. I could have customerId of type ObjectId, but I would rather have the POJO as a string and have the customerId automatically convert to ObjectId when saving/querying. There does not seem to be any built in annotation which behaves like #Id, but can be used for manual references. How would I go about creating one, or is there a better solution? I have read a bit above converters, but I do not want to re-map the whole POJO to a DBObject.
Any advice would be appreciated.
when you get your customer data you have to create the objectId yourself.
Db.Customer.find({"_id" : new ObjectId("$valueFromUserTable")});
so in Spring Java you would:
ObjectId objId = new ObjectId("$valueFromUserTable");
Query query = new Query(Criteria.where("_id").is(objId));
Customer customer = super.mongoOps.find(query, Customer.class);
Related
In my application I generated ids using another database and I need to use this already created ids in mongo aswell. Setting the id before saving the object just returns an object with a generated id by mongo. Is there any configuration for the mongodb to not do this?
SampleObject {
private String id;
}
SampleObject sampleObject = new SampleObject();
sampleObject.setId("f9de2416-1d10-11ec-a8b5-00ff37757c54");
repository.save(sampleObject); // returns the object saved but with a different id (generated by mongo not what we set)
Could you retry with the Annotation of #Id. Based on the document you need to annotate #Id to map to _id.
#Document
public class SampleObject {
#Id
private ObjectId id;
}
I have a document with an array field with
"chapters": [
{ "id" : "14031871223912313", ...}
...
]
I would like to query return the id's with Spring Data's MongoTemplate using the following:
class Chapter {
private String id;
public String getId() {
return id;
}
}
This way the id is not populated. I have tried using the different mapping options with #Field described here http://docs.spring.io/spring-data/mongodb/docs/current/reference/html/#mapping.conventions.id-field
What am I doing wrong? I know I can always to back to mongo java driver, but I thought this should work.
Thanks in advance for any help.
Found a solution. It is populated via:
#Field("id")
private String chaperId
In MongoDB id's are _id, and every document in mongo has an _id. From the document you linked to, Spring will map #Field String id to mongo's _id field. You probably want to use a #Field('id') String id field mapping to indicate that the field you want is id not _id.
Is it possible somehow to search in String fields over #DBRef.
I have this #Document:
public class DocumentFileVersion {
#TextIndexed
#DBRef
private OtherObject otherObject
and I will search in String fields of otherObject. Is there any possibility to do that?
DBRef are designed to be queried by id reference only.
So it is not possible. You should rethink your schema structure.
I am trying to use nested Mongodb query but it does not work.
It is similar to Spring data mongodb query for subdocument field
But suggestions mentioned there does not work.
Please find my documents below.
#Document
public class Ticket {
#Id
private String id;
#DBRef
#CascadeSave
private Customer customer;
// getters and setters
}
#Document
public class Customer {
#Id
private String id;
private String firstName;
// getters and setters
}
public interface TicketRepository extends MongoRepository<Ticket, String> {
public List<Ticket> findByCustomerFirstName(String firstName);
}
I tried both findByCustomerFirstName and findByCustomer_FirstName but it does not work. Any suggestions ?
These suggestions are right it should work...
Official docs explains it as you did it:http://docs.spring.io/spring-data/mongodb/docs/current/reference/html/#repositories.query-methods.query-property-expressions
Property expressions can refer only to a direct property of the
managed entity, as shown in the preceding example. At query creation
time you already make sure that the parsed property is a property of
the managed domain class. However, you can also define constraints by
traversing nested properties. Assume a Person has an Address with a
ZipCode. In that case a method name of
List<Person> findByAddressZipCode(ZipCode zipCode);
creates the
property traversal x.address.zipCode
Just one thing, remove #Document from Customer and try it, Mongodb didn't support join queries (I'm not sure if now it does)... so you're document should be Ticket and it must have a embbebed document Customer as a inner object and not in a different document.
Is it possible to search based on some param value of a DBRef object in spring data.
eg. say we have two objects, Car, and Company as shown
Class Car {
#Id
String id;
String model;
#DBRef
Company company;
}
Class Company {
#Id
String id;
String name;
}
Can I write a query to fetch all car's of Hyundai company like this,
Query queryForCars = new Query(Criteria.where("company.name").is("Hyundai")), Car.class)
It works fine for non referenced objects, but for referenced object it's working in my case.
Thanks for your help.
For referenced objects this is how your query should look like:
Query queryForCars = new
Query(Criteria.where("company.$name").is("Hyundai")), Car.class)