我在mongodb中有一个文档,它是从这个java模型创建的:
class Comment
{
String pollID;
List<CommentDetail> commentDetailList;
}
class CommentDetailList
{
String text;
User user;
}
class User
{
String userID;
String username;
}
Run Code Online (Sandbox Code Playgroud)
所以,我的文档看起来像这样:
{
"pollID":"ABCDEFG",
"commentDetailList":
[
{
"text":"Hello Comment1",
"user":
{
"userID":"001",
"username": "username1"
}
},
{
"text":"Hello Comment2",
"user":
{
"userID":"001",
"username": "username1"
}
},
{
"text":"Hello Comment3",
"user":
{
"userID":"002",
"username": "username2"
}
}
]
}
Run Code Online (Sandbox Code Playgroud)
现在我想使用以下代码更新userID = 001的用户的用户名:
Query query = new Query(Criteria.where("pollID").is("ABCDEFG")
.and("commentDetailList")
.elemMatch(Criteria.where("user.userID").is("001")));
Update update = new Update().set("commentDetailList.$.user.username", username);
WriteResult wr …Run Code Online (Sandbox Code Playgroud) 我已经在一个没有问题的项目中使用 Spring-Data-Mongo 一段时间了。在上一次 POM 更新中,我开始在日志文件中看到以下异常:
警告:2014 年 8 月 4 日 13:55:24 org.springframework.data.mongodb.core.mapping.BasicMongoPersistentProperty - 不允许为 id 属性自定义字段名称!不考虑自定义名称!
它没有提供有关此问题出在哪里的任何线索,因此我们一无所知。随着对应用程序的调用,它会重复多次。我们确实在这里找到了它的起源:https : //github.com/spring-projects/spring-data-mongodb/blob/11417144bd3574c35af06fde3a3c2e56a1dd5890/spring-data-mongodb/src/main/java/org/springframework/data/mongodb /core/mapping/BasicMongoPersistentProperty.java#L85
有任何想法吗?
编辑:为感兴趣的人添加了示例类:
@Document(collection="Account")
public class Account {
....
@Id
private String id = null;
....
}
Run Code Online (Sandbox Code Playgroud) 我最近将一个小项目更新为Java 8.该项目使用spring-data-mongodb.我将所有spring依赖版本升级到最新的稳定版本.spring-data-mongodb正在运行1.5.2.RELEASE.
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb</artifactId>
<version>1.5.2.RELEASE</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)
每当我启动服务时,我都会获得java.lang.Object的MappingException.
这是堆栈跟踪:
15:21:51.177 [main] ERROR o.s.web.servlet.DispatcherServlet - Context initialization failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'pageableIdsRepository': Invocation of init method failed; nested exception is org.springframework.data.mapping.model.MappingException: Could not lookup mapping metadata for domain class java.lang.Object!
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1553) ~[spring-beans-4.0.6.RELEASE.jar:4.0.6.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:539) ~[spring-beans-4.0.6.RELEASE.jar:4.0.6.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:475) ~[spring-beans-4.0.6.RELEASE.jar:4.0.6.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:302) ~[spring-beans-4.0.6.RELEASE.jar:4.0.6.RELEASE]
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228) ~[spring-beans-4.0.6.RELEASE.jar:4.0.6.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:298) ~[spring-beans-4.0.6.RELEASE.jar:4.0.6.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193) ~[spring-beans-4.0.6.RELEASE.jar:4.0.6.RELEASE]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:684) ~[spring-beans-4.0.6.RELEASE.jar:4.0.6.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:760) ~[spring-context-4.0.6.RELEASE.jar:4.0.6.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:482) ~[spring-context-4.0.6.RELEASE.jar:4.0.6.RELEASE]
at org.springframework.web.servlet.FrameworkServlet.configureAndRefreshWebApplicationContext(FrameworkServlet.java:658) …Run Code Online (Sandbox Code Playgroud) 如何在Spring Data-MongoDB框架的聚合方法中设置“ allowDiskUse ”选项?
mongodb mongodb-java spring-data aggregation-framework spring-data-mongodb
问题
查询工作正常但没有限制和跳过,它一次获取所有记录。
请建议我做错了什么。
MongoDB 集合
{
"_id" : ObjectId("559666c4e4b07a176940c94f"),
"postId" : "559542b1e4b0108c9b6f390e",
"user" : {
"userId" : "5596598ce4b07a176940c943",
"displayName" : "User1",
"username" : "user1",
"image" : ""
},
"postFor" : {
"type": "none",
"typeId" : ""
},
"actionType" : "like",
"isActive" : 1,
"createdDate" : ISODate("2015-07-03T10:41:07.575Z"),
"updatedDate" : ISODate("2015-07-03T10:41:07.575Z")
}
Run Code Online (Sandbox Code Playgroud)
Java驱动查询
Aggregation aggregation = newAggregation(
match(Criteria.where("isActive").is(1).and("user.userId").in(feedUsers)),
group("postId")
.last("postId").as("postId")
.last("postFor").as("postFor")
.last("actionType").as("actionType")
.last("isActive").as("isActive")
.last("user").as("user")
.last("createdDate").as("createdDate")
.last("updatedDate").as("updatedDate"),
sort(Sort.Direction.DESC, "createdDate")
);
aggregation.skip( skip );
aggregation.limit( limit );
AggregationResults<UserFeedAggregation> groupResults =
mongoOps.aggregate(aggregation, SocialActionsTrail.class, UserFeedAggregation.class);
return …Run Code Online (Sandbox Code Playgroud) 按照MongoDB的文档,"Queries cannot use both text and Geospatial Indexes"意味着我们不能同时使用$textSearch,并$nearSphere在单个弹簧数据蒙戈库方法.
但我正在寻找一些解决方法,这将允许我使用两者TextCriteria并nearSphere Pint一起使用,我没有其他方式,我真的想让这个工作.
我找到了https://groups.google.com/forum/#!msg/mongodb-user/pzlYGKMYMVQ/O6P5S578Xx0J,表示他能够执行一些解决方法,但我没有得到他如何编写Repository方法来跟踪查询?
find({"add.loc": {$near:{$geometry: {type: "Point",coordinates:
[116.425, -31.09]}, $maxDistance: 500000}},$text:{$search: "hello"}}
Run Code Online (Sandbox Code Playgroud)
我处境最糟糕
对于我的存储库方法,它给出:
Page<User> getAddress_PositionNear(TextCriteria tc,Point gpoint, Distance d, Pageable p);
"errmsg" : "text and geoNear not allowed in same query" , "code" : 2
Run Code Online (Sandbox Code Playgroud) 是否可以在 mongodb 存储库中为 @Query 方法使用命名参数,就像我们可以使用 jpa 存储库一样(http://docs.spring.io/spring-data/jpa/docs/1.4.3.RELEASE /reference/html/jpa.repositories.html第 2.3.5 节)?
例如,我想使用以下代码:
@Query("{'store' : :store, 'app' : :app }")
List<T> findByStoreAndApp(@Param("store") String store, @Param("app") String app);
Run Code Online (Sandbox Code Playgroud)
代替:
@Query("{'store' : ?0, 'app' : ?1 }")
List<T> findByStoreAndApp(String store, String app);
Run Code Online (Sandbox Code Playgroud) 由于mongodb中的每个集合在_id列上都有一个默认索引,因此我想在以下情况下利用它。
我的收藏如下
{
"_id":{
"timestamp" : ISODate("2016-08-24T23:22:20.201Z"),
"departmentname" : "sales",
"city":"NJ"
}
//Other fields in my collection
}
Run Code Online (Sandbox Code Playgroud)
通过这种结构,我可以按以下方式进行查询,
db.getCollection('test').find(
{
"_id" : {
"timestamp" : ISODate("2016-08-21T23:22:20.201Z"),
"departmentname" : "sales",
"city":"NJ"
}
}
)
Run Code Online (Sandbox Code Playgroud)
但是,当我通过一个或多个字段查询以下_id列的一部分时,
db.getCollection('test').find(
{
"_id" : {
"timestamp" : ISODate("2016-08-21T23:22:20.201Z")
}
}
)
Run Code Online (Sandbox Code Playgroud)
(要么)
db.getCollection('test').find(
{
"_id" : {
"departmentname" : "sales"
}
}
)
Run Code Online (Sandbox Code Playgroud)
(要么)
db.getCollection('test').find(
{
"_id" : {
"departmentname" : "sales",
"city":"NJ"
}
}
)
Run Code Online (Sandbox Code Playgroud)
我看不到任何文件退回
当我使用.explain()检查时,我看到它已经使用了Index,但是没有找到任何文档。
另外,我想对timestamp字段进行日期范围查询,并对_id列中的一个或多个字段进行查询,如下所示,
db.getCollection('test').find(
{
"_id.timestamp" : {
"$gte": …Run Code Online (Sandbox Code Playgroud) 我有一个扩展ReactiveMongoRepository的MovieRepository.我想以反应方式保存单个POJO.但ReactiveMongoRepository不为Mono或Publisher提供保存方法.我必须使用block()方法或使用saveAllReactiveMongoRepository中的方法.
public Mono<ServerResponse> create(ServerRequest request) {
Mono<Movie> movieMono = request.bodyToMono(Movie.class);
return movieRepository.save(movieMono.block()) //
.flatMap((movie) -> ServerResponse.ok().body(fromObject(movie)));
}
Run Code Online (Sandbox Code Playgroud)
有没有更好的方法来解决这类问题?我不认为使用块方法是反应式编程的好主意.
spring mongodb reactive-programming spring-data-mongodb project-reactor
我在StackOverFlow上找到了几个与Aggregation中的addFields相关的示例。但是没有人用Java实现。
db.getCollection('myDocument').aggregate([
{$match : {"metaId.ref.uuid" : "d6112808-1ce1-4545-bd52-cf55bc4ed25e"}},
{$lookup: {from: "simple", localField: "someId.ref.uuid", foreignField: "uuid",
as: "simple"}},
{"$unwind": "$simple"},
{"$addFields": { "metaId.ref.name" : "$simple.name" }}
])
Run Code Online (Sandbox Code Playgroud)
我不能正确地用Java实现:-没有正确的程序
LookupOperation lookupOperation =LookupOperation.newLookup().from("simple").localField("execId.ref.uuid").foreignField("uuid").as("simple");
Aggregation myDocAggr = newAggregation(match(Criteria.where("metaId.ref.uuid").is(someUUID)), group("uuid").max("version").as("version"),
lookupOperation,
Aggregates.unwind(""),
Aggregates.addFields(fields));
Document document =new Document();
AggregationResults<String> myDocAggrResults = mongoTemplate.aggregate(myDocAggr , myDocument, myDocument.class);
List<String> mydocumentList = myDocAggrResults .getMappedResults();
Run Code Online (Sandbox Code Playgroud)
无法使用unwind和addFields,这是示例Java代码,但不行。请帮我。提前致谢
java mongodb mongodb-query aggregation-framework spring-data-mongodb