I am using spring-data-mongodb
to persist my Java objects to MongoDB. Everthing works fine except one specific operation:
@Override
public Collection<MyDocument> findAllByTags(Collection<String> tags) {
FindIterable<MyDocument> results = operations.getCollection(COLLECTION_NAME)
.find(Filters.all(FIELD_TAGS, tags), MyDocument.class);
return StreamSupport.stream(results.spliterator(), false).collect(Collectors.toList());
}
The document class looks like this:
@Getter
@Setter
@NoArgsConstructor
@EqualsAndHashCode(onlyExplicitlyIncluded = true)
@Document
public class MyDocument implements MyEntity {
@Id
@EqualsAndHashCode.Include
@BsonProperty("myId")
private String myId;
@BsonProperty("dateCreated")
private Date dateCreated;
@BsonProperty("otherField")
private String otherField;
@Indexed
@BsonProperty("tags")
private Collection<String> tags;
//more fields
All fields of all objects are returned as expected except for the one annotated with @Id
which is set to null . Does anyone know what is causing this behaviour and how to fix it? Thank you for your time.
I got it working as follows:
@Getter
@Setter
@NoArgsConstructor
@EqualsAndHashCode(onlyExplicitlyIncluded = true)
@Document
public class MyDocument implements MyEntity {
@MongoId(value = FieldType.STRING)
private String myId;
//...
And the query:
@Override
public Collection<MyDocument> findAllByTags(Collection<String> tags) {
return operations.find(query(Criteria.where(FIELD_TAGS).all(tags)), MyDocument.class);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.