我一直在研究一个使用MongoDB作为存储形式的Java应用程序,但是我遇到了一个问题.当用户在我的应用程序中添加注释时,它会将文档添加到注释集合中,然后对统计数据执行upsert.但是,upsert仅在第一次添加(更新后没有调用或插入新数据).这是相关的代码:
public class CommentDAO implements ICommentDAO {
@Autowired
@Qualifier(value = "mongoDB")
MongoTemplate mongoTemplate;
public UserComment addComment(UserComment userComment) {
updateStats(userComment, 1L);
mongoTemplate.save(userComment);
return userComment;
}
public void updateStats(UserComment userComment, Long offset) {
Update update = new Update();
update.inc("total", offset);
Query query = query(where("entity").is(userComment.getEntity()));
WriteResult wr = mongoTemplate.upsert(query, update, CommentStat.class);
}
}
Run Code Online (Sandbox Code Playgroud)
以下是我的评论集合中的结果示例:
{
"_id" : ObjectId( "50ab0566e4b0ea8f74b82d48" ),
"_class" : "com.test.models.comment.UserComment",
"userId" : 4,
"ownerId" : 3,
"comment" : "Test comment",
"type" : "ART",
"entity" : "759446489112216656",
"date" : Date( 1353385318079 …Run Code Online (Sandbox Code Playgroud) 我一直在尝试注册我自己的写自定义转换器来更改默认ID值.但它实际上从未打过电话.这是我的自定义转换器
public class EventKeyConverter implements Converter<Event,DBObject> {
@Override
public DBObject convert(Event object) {
DBObject dbObject = DBObjectTransformer.toDBObject(object);
dbObject.put("_id", KeyGenerator.getRandomKey());
return dbObject;
}
}
Run Code Online (Sandbox Code Playgroud)
这里是我注册客户转换器的地方
@Override
@Bean
public CustomConversions customConversions() {
List<Converter<?, ?>> converters = new ArrayList<Converter<?, ?>>();
converters.add(new EventKeyConverter());
return new CustomConversions(converters);
}
@Override
@Bean
public MappingMongoConverter mappingMongoConverter() throws Exception {
MappingMongoConverter converter = new MappingMongoConverter(
eventsMongoDbFactory(), mongoMappingContext());
converter.setCustomConversions(customConversions());
return converter;
}
@Bean
public MongoTemplate eventsMongoTemplate() throws Exception {
final MongoTemplate template = new MongoTemplate(
eventsMongoDbFactory(), mappingMongoConverter());
template.setWriteResultChecking(WriteResultChecking.EXCEPTION);
return template; …Run Code Online (Sandbox Code Playgroud) 我正在尝试用用户输入创建动态查询和操作我的代码是我创建的标准列表如:
List<Criteria> criterias = new ArrayList<Criteria>();
Run Code Online (Sandbox Code Playgroud)
并添加了此列表的标准.并成功添加.现在我想在每个标准之间制作和操作.
Criteria criteria = new Criteria().andOperator(criterias.get(0), criterias.get(1));
Run Code Online (Sandbox Code Playgroud)
它工作正常但我的输入没有修复,所以我想它应该动态添加,我试过
for(int i=0;i<criterias.size();i++)
Criteria criteria = new Criteria().andOperator(criterias.get(i));
Run Code Online (Sandbox Code Playgroud)
我失踪的地方?
是否有可能在mongodb中使用双向dbref(就像我们在关系数据库中可以拥有的那样,双向一对多关系).如果可能的话,如何在Mongodb中使用Spring-mongodb来表示它.我面临的具体情况如下
假设我们想建立一个论坛.论坛可以有多个论坛主题.每个主题都可以发布帖子,但帖子应标记为一个论坛主题.论坛 - >主题和主题 - >帖子之间有一对多的关系,也有帖子关系 - >论坛主题.鉴于这种情况如何使用Spring-mongodb来处理.
我是一个新的Mongodb,我在使用java spring查询时遇到了问题.
我想在Spring数据中使用这个shell
db.NewFeed.aggregate([
{
$match : {username : "user001"}
},
{
$lookup:
{
from: "NewfeedContent",
localField: "content.contentId",
foreignField: "_id",
as: "NewfeedContent"
}
}
])
Run Code Online (Sandbox Code Playgroud)
我在Google上发现但尚无答案.
我有一个mongo聚合组查询:
db.wizard.aggregate(
{
$group: {
_id: "$title",
versions: { $push: {version:"$version", author:"$author", dateAdded:"$dateAdded"}}
}
})
Run Code Online (Sandbox Code Playgroud)
我在Java Spring-Data-MongoDB中需要这个查询,我目前的解决方案如下所示:
Aggregation agg = Aggregation.newAggregation(
Aggregation.group("title").
push("version").as("versions")
);
Run Code Online (Sandbox Code Playgroud)
问题是我不知道如何为push方法添加更多字段(版本,作者,dateAdded).是否可以使用Spring-Data-MongoDB?
我有这样的模型如下
@CompoundIndexes(value = {
@CompoundIndex(name = "catalog_idx", def = "{'code' : 1, 'brand' : 1}", unique = true) })
@Document(collection = Catalog.ENTITY)
public class Catalog extends AbstractModel<String> {
private static final long serialVersionUID = 1L;
public static final String ENTITY = "catalog";
@NotNull(message = "Code is required")
@Field("code")
private String code;
@NotNull(message = "Brand is required")
@DBRef(lazy = true)
@Field("brand")
private Brand brand;
}
Run Code Online (Sandbox Code Playgroud)
当我保存时,mongoTemplate.save(object);我只看到在DB而不是6中创建的2个对象.就在保存我的调试行以保存对象之前.
Catalog [code=StagedCatalog, brand=Brand [code=Brand_3]]
Catalog [code=StagedCatalog, brand=Brand [code=Brand_2]]
Catalog [code=StagedCatalog, brand=Brand [code=Brand_1]]
Catalog …Run Code Online (Sandbox Code Playgroud) 有人可以告诉我如何配置spring数据mongoDB而不创建新集合(如果不存在)但在插入文档时引发异常吗?
我在 mongodb 文档中找到了这个 mongo 命令:
db.sales.aggregate(
[
{
$group : {
_id : { month: { $month: "$date" }, day: { $dayOfMonth: "$date" }, year: { $year: "$date" } },
totalPrice: { $sum: { $multiply: [ "$price", "$quantity" ] } },
averageQuantity: { $avg: "$quantity" },
count: { $sum: 1 }
}
}
]
)
Run Code Online (Sandbox Code Playgroud)
使用 spring 数据的聚合时,很容易通过调用 Aggregation.group(Feild ...) 将一个 Document 属性绑定到 $group 中的 _id
但是对于上述情况,_id 属性被组合在一起,我未能在 Java 中构建它。各位大侠有解决办法吗???我的意思是如何用Java表达上面的js??
非常感谢...
@update ..... $group 的 _id 使用 mongo 函数,如 $month …
java mongodb spring-mongo mongodb-query aggregation-framework
我尝试使用 Spring Data REST 和 MongoDB 实现基于位置的搜索。首先我创建了一个模型。
public class Event {
@Id
private String id;
private String name;
private String description;
private double[] position;
.. getter setter ..
}
Run Code Online (Sandbox Code Playgroud)
其次,我添加了一个存储库。
public interface EventRepository extends MongoRepository<Event, String> {
List<Event> findByName(@Param("name") String name);
List<Event> findByPositionWithin(@Param("circle") Circle c);
List<Event> findByPositionNear(@Param("point") Point p, @Param("distance") Distance d);
}
Run Code Online (Sandbox Code Playgroud)
但是现在我遇到了我不知道如何为findByPositionWithin和findByPositionNear调用/search端点的问题?我找不到任何关于如何将复杂类型传递给方法的参考文献或文档。
findByName 端点events/search/findByName?name=test运行良好。如何传递 circle 参数?我需要编写自定义消息转换吗?
希望有人有任何建议:)