how to create a classes from mogodb - mongodb

Can someone give me an example of code for mapping with morphia and mongodb?
This is my class which contains database fields. I don't know how to
make the connection between morphia and mongodb.
import com.google.code.morphia.annotations.*;
//#Entity
//define the name of the collection where this entity will be stored
#Entity("tabes")
class MappingMorphia {
#Id
String id;
String FACILITY;
String HOST;
String MESSAGE;
String PID;
String PRIORITY;
String PROGRAM;
int SEQNUM;
String SOURCE;
String SOURCEIP;
String TAGS;
//getters and setters
}

There are examples aplenty at the morphia github site: https://github.com/mongodb/morphia/wiki

Mongodb doesn't understand about classes as is, or at least not the way you are thinking. There is, however, a module called mongoose that lets your model your objects. I think that will suit you.
Here's a link to mongoose's docs
http://mongoosejs.com/docs/documents.html

Related

Select nested property from MongoDB repository with Spring Boot

I like the parsing of method names in the MongoRepository so I don't have to write queries. But I was wondering if there is a way to use this pattern to only select a certain (nested) field.
My Document looks like:
#Document(collection = "elements")
public class ElementEntity {
#Id
private String id;
private String type;
private MetaData metaData;
private String json;
}
public class MetaData {
private String title;
private String description;
private final List<String> keywords = new ArrayList<>();
}
I can search ElementEntities by keyword with this:
List<ElementEntity> findByMetaDataKeywords(String keyword);
I now want to get a list of possible keywords, but I don't find any documentation on how or if it is even possible with the method name pattern. I was hoping something like this might work, but it doesn't:
List<String> getDistinctMetaDataKeywordsAsc();
Is there a way to achieve this with just an interface method, or do I need to write a (SQL?) query?
EDIT based on a comment in one of the answers:
Assuming I have two documents in my elements collection:
has the keywords: "disclaimer" and "legal"
has the keywords: "footnote" and "legal"
I want to have a method that returns me a List with the three distinct keywords in alphabetic order: "disclaimer", "footnote", "legal"
try this
List<ElementEntity> findByMetaData_Keywords(List<String> keywords);

Spring Data - Mongo DB - #TextIndexed over #DBRef

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.

Nested query in mongodb using spring data

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.

search by param values of referenced objects in mongodb

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)

How can I query to find mongo entities whose list of sub-entities contain a field matching a string?

I have a collection of entities that look like this:
public class ClientEntity {
#Id
private String id;
#Indexed(unique = true)
private String clientId;
private String name;
#DBRef
private List<ClientMachineEntity> machines;
...
}
...where ClientMachineEntity looks like:
public class ClientMachineEntity {
#Id
private String id;
#Indexed(unique = true)
private String clientMachineId;
private String hostName;
...
}
I have a working search that finds ClientEntities by matching against "clientId" and "name":
public List<ClientEntity> searchByIdAndName(String id, String name) {
Criteria idCriteria = Criteria.where("clientId").regex(id, "i");
Criteria nameCriteria = Criteria.where("name").regex(name, "i");
Query query = new Query(new Criteria().orOperator(idCriteria, nameCriteria));
...
}
So my question is, how can I expand this search so that it also matches against "clientMachineId" in the list of sub-entities? I tried adding the following criteria:
Criteria machineCriteria = Criteria.where("machines.clientMachineId").regex(id, "i");
...but that doesn't work, presumably because machines is a LIST of entities, not just a single sub-entity.
UPDATE: It seems like what I'm looking for is the .elemMatch() functionality, but when I try that:
Criteria machineCriteria = Criteria.where("machines").elemMatch(Criteria.where("clientMachineId").regex(id, "i"));
...I get the following error:
org.springframework.data.mapping.model.MappingException: No mapping metadata found for class com.mongodb.BasicDBObject
You can't query by fields in subentities linked with DBRef. If ClientMachineEntity would be embedded in ClientMachine - then you could use dot notation or $elemMatch depending on needs.
In your particular example - couldn't field ClientMachineEntity.clientMachineId be saved as _id and used as a primary key? Then you could get the results you need - take a look at: How to query mongodb with DBRef
My suggestion for development with Spring Data MongoDB is - first learn how to (and if it's possible) do it in plain Javascript with MongoDB console, then learn how to do the same with Spring Data MongoDB.