我正在使用 org.springframework.data.mongodb.repository.MongoRepository。我写了一些像下面这样的自定义方法,
public interface DocRepository extends MongoRepository<Doc, String> {
Doc findByDocIdAndAssignmentId(final String docId, final String assignemtId);
}
Run Code Online (Sandbox Code Playgroud)
如何编写自定义方法,在满足条件时更新所有条目。
例如,如果分配 ID 为“xyz”,则将文档倾斜字段设置为“abc”?
我正在将 MongoDB 与Spring Boot 2.0.1.RELEASE. 一切似乎都运行良好。我能够使用 MongoRepository 正确执行 CRUD 操作。当我使用 mongodb 查询时
@Query(value = "{address.city:?0}")
public List<Hotel> findByCity(String city);
@Query(value = "{address.country:?0}")
public List<Hotel> findByCountry(String country);
Run Code Online (Sandbox Code Playgroud)
当我尝试使用 url 访问数据时localhost:8090/hotels/address/city/Rome,我收到以下错误响应
{
"timestamp": "2018-05-04T04:51:43.549+0000",
"status": 500,
"error": "Internal Server Error",
"message": "Invalid JSON input. Position: 9. Character: '.'.",
"path": "/hotels/address/city/rome"
}
Run Code Online (Sandbox Code Playgroud)
和以下登录控制台:
Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.bson.json.JsonParseException: Invalid JSON input. Position: 9. Character: '.'.] with …Run Code Online (Sandbox Code Playgroud) 我们正在开发一个从 mongoDB 获取数据的项目。我们创建了存储库类,如下所示
@Repository
public interface CustomerRepository extends MongoRepository<Customer,String>{
List<Customer> customers = findByCustomerId(final String customerId);
}
Run Code Online (Sandbox Code Playgroud)
我们希望添加跳过/偏移和限制参数作为 findByCustomerId 方法的一部分。其中 limit 用于定义返回的记录数,skip/offset 定义我们需要获取记录后的记录数。
请帮助我们如何使用 MongoRepository 以最佳方式实现这一点。
我在一个文件中写入了一个聚合,该聚合是从后端的文件MyRepository.kt调用的。MongoDataRetriever.kt
MyRepository.kt文件:
import org.springframework.data.mongodb.repository.Aggregation\nimport org.springframework.data.mongodb.repository.MongoRepository\n\n @Aggregation(pipeline = [\n "{ \\$match: { 'objName' : { \\$exists: true } } }",\n "{ \\$sort: { 'addedDate': -1 } }"\n ])\n fun getLatestObjectsWithLatestData(): List<MyDocument>\nRun Code Online (Sandbox Code Playgroud)\n和MongoDataRetriever.kt文件:
override fun getLatestObjects(): List<MyObj> {\n return myRepository.getLatestObjectsWithLatestData().map { it.toMyObj() }\n }\nRun Code Online (Sandbox Code Playgroud)\n上述aggregation失败并出现错误:
message:\xc2\xa0"Command failed with error 16819 (Location16819): 'Sort exceeded memory limit of 104857600 bytes, but did not opt in to external sorting. Aborting operation. …
我想使用MongoRepository接口,但我有一个BeanCreationException异常.ComponentScan和MappingBasePackage字符串已正确填充.
这是我的代码:
(Jersey类)@Component @Path("技能")公共类SkillREST {
@Autowired
private UserService userService;
@POST
@Path("/{token}")
@Produces({MediaType.APPLICATION_JSON})
public List<Skill> addSkill(@PathParam("token") String token, Skill skill){
return userService.getAllSkills();
}
}
Run Code Online (Sandbox Code Playgroud)
以下是未正确自动装配存储库的服务:
@Component
public class UserService {
@Autowired
SkillRepository skillRepository;
public List<Skill> getAllSkills(){
return skillRepository.findAll();
}
}
@Repository
public interface SkillRepository extends MongoRepository<Skill, String> {
}
Run Code Online (Sandbox Code Playgroud)
Spring配置类:
@Configuration
@PropertySource("classpath:mongodb.properties")
@EnableMongoRepositories
@ComponentScan("com.headlezz")
public class SpringMongoConfig extends AbstractMongoConfiguration {
@Inject
Environment environment;
@Override
public String getDatabaseName() {
return environment.getProperty("db.name");
}
@Override
@Bean
public Mongo mongo() throws Exception {
return new …Run Code Online (Sandbox Code Playgroud)