spring-data-mongodb

Random documents from MongoDB using spring-data

萝らか妹 提交于 2019-12-01 04:49:12
问题 I'm able to do it by using this mongodb native query: db.books.aggregate( [ { $sample: { size: 15 } } ] ) But how to do it in spring-data-mongodb ? I found no similar operation in Aggregation class of Spring Aggregation Framework 回答1: Update: Starting with v2.0 of Spring Data you can do this: SampleOperation matchStage = Aggregation.sample(5); Aggregation aggregation = Aggregation.newAggregation(sampleStage); AggregationResults<OutType> output = mongoTemplate.aggregate(aggregation,

Spring Data MongoDB Lookup with Pipeline Aggregation

天涯浪子 提交于 2019-12-01 04:41:22
问题 How would I convert the following MongoDB query into a query to be used by my Java Spring application? I can't find a way to use pipeline with the provided lookup method. Here is the query I am attempting to convert. I also want to note that I didn't use $unwind as I wanted the deliveryZipCodeTimings to stay as a grouped collection in the return object. db.getCollection('fulfillmentChannel').aggregate([ { $match: { "dayOfWeek": "SOME_VARIABLE_STRING_1" } }, { $lookup: { from:

PersistenceConstructor argument variable name doesn't match instance variable name

时间秒杀一切 提交于 2019-12-01 03:58:41
问题 I'm trying to persist the following object with spring-data-mongodb version 1.1.1.RELEASE : @Document public static class TestObject { private final int m_property; @PersistenceConstructor public TestObject(int a_property) { m_property = a_property; } public int property() { return m_property; } } I get a MappingException when I try to read the object back from the database (see full stacktrace below) The naming convention my group uses requires argument variable names to be prefaced by a_

Spring Data MongoDB: BigInteger to ObjectId conversion

≡放荡痞女 提交于 2019-12-01 03:54:07
问题 I have a problem with update query using Spring Data MongoDB. I retrieve some object's _id as BigInteger value. Then I want to make following query: Query query = new Query(Criteria.where("_id").is(id)); Update update = new Update(); update.set("version",version); mongoOperations.updateFirst(query, update, Audit.class); Query part fails to match any documents since id value passed to is() somehow must be converted to ObjectId. I can't find any documentation on this kind of conversion. Will

How to use optimistic locking with Spring Data MongoDB?

人盡茶涼 提交于 2019-12-01 03:43:57
I'm going through Spring Data MongoDB - Reference Documentation and I'm finding the examples to be a bit too simplistic. In particular I'm trying to understand how to handle stale data in concurrent environments. For instance, imagine I have the following entity: public class Person { private final String username; private final String firstname; private final String lastname; [...] } Now if I use CrudRepository to save/update/remove my entities, then imagine a scenario in which two threads retrieve the same entity, one of them removes it, and the other one updates its lastname field. If the

How to map document with dynamic keys to a Spring MongoDb entity class

有些话、适合烂在心里 提交于 2019-12-01 03:33:20
问题 I have a document that can have dynamic key names: { "_id" : ObjectId("51a29f6413dc992c24e0283e"), "envinfo" : { "appName" : "MyJavaApp", "environment" : { "cpuCount" : 12, "heapMaxBytes" : 5724766208, "osVersion" : "6.2", "arch" : "amd64", "javaVendor" : "Sun Microsystems Inc.", "pid" : 44996, "javaVersion" : "1.6.0_38", "heapInitialBytes" : 402507520, } Here envinfo 's keys are not known in advance. What is the best way to create an entity class in Spring Data Mongodb which will map this

No converter found capable of converting from type java.lang.String to type @org.springframework.data.repository.query.Param

我与影子孤独终老i 提交于 2019-12-01 01:41:45
I'm building a MongoDb rest application with spring boot 1.1.5. I have created a repository: @RepositoryRestResource(collectionResourceRel = "SourceProductV1Repository", path = "SourceProductV1Repository") public interface SourceProductV1Repository extends MongoRepository<SourceProductV1Model, String> { public List<SourceProductV1Model> findByUser (@Param("user") MongoUser user); } I Defined a converter to convert String to MongoUser (the converter is correctly registered in the spring context): @Component public class String2MongoUserConverter implements Converter<String, MongoUser>{ private

How to configure a MongoDB cluster which supports sessions?

最后都变了- 提交于 2019-12-01 01:08:03
问题 I want to explore the new transaction feature of MongoDB and use Spring Data MongoDB. However, I get the exception message " Sessions are not supported by the MongoDB cluster to which this client is connected ". Any hint regarding the config of MongoDB 3.7.9 is appreciated. The stacktrace starts with: com.mongodb.MongoClientException : Sessions are not supported by the MongoDB cluster to which this client is connected at com.mongodb.MongoClient.startSession(MongoClient.java:555) ~[mongodb

SD MongoDB polymorphism in subdocument

…衆ロ難τιáo~ 提交于 2019-12-01 00:10:10
I just started developing some app in Java with spring-data-mongodb and came across some issue that I haven't been able to solve: Have a couple of document beans like this: @Document(collection="myBeanBar") public class BarImpl implements Bar { String id; Foo foo; // More fields and methods ... } @Docuemnt public class FooImpl implements Foo { String id; String someField; // some more fields and methods ... } And I have a repository class with a method that simply invokes a find similar to this: public List<? extends Bar> findByFooField(final String fieldValue) { Query query = Query.query

通过spring-data-mongodb 操作mongodb

北慕城南 提交于 2019-11-30 21:09:33
一、前言 其实,以前项目组也使用过mongodb,只是那时候是作为主数据库来使用,所以在框架方面会更加复杂,比如使用jpa,如果只是把mongodb作为附数据库使用的话,个人认为通过spring-data-mongodb的mongoTemple来操作数据库即可。 二、spring 整合mongodb 配置文件如下:(用户名 密码缺失) <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:mongo="http://www.springframework.org/schema/data/mongo" xsi:schemaLocation="http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www