- mongodb - 在 MongoDB mapreduce 中,如何展平值对象?
- javascript - 对象传播与 Object.assign
- html - 输入类型 ="submit"Vs 按钮标签它们可以互换吗?
- sql - 使用 MongoDB 而不是 MS SQL Server 的优缺点
我一直在开发一个使用 MongoDB 作为存储形式的 Java 应用程序,但是我遇到了一个问题。当用户在我的应用程序中添加评论时,它会将文档添加到评论集合中,然后对统计数据进行更新插入。但是, 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);
}
}
这是我的评论集合中的结果示例:
{
"_id" : ObjectId( "50ab0566e4b0ea8f74b82d48" ),
"_class" : "com.test.models.comment.UserComment",
"userId" : 4,
"ownerId" : 3,
"comment" : "Test comment",
"type" : "ART",
"entity" : "759446489112216656",
"date" : Date( 1353385318079 )
},
{
"_id" : ObjectId( "50ab0691e4b0775cf7daacad" ),
"_class" : "com.test.models.comment.UserComment",
"userId" : 4,
"ownerId" : 3,
"comment" : "Another test",
"type" : "ART",
"entity" : "759446489112216656",
"date" : Date( 1353385617619 )
}
...还有我单例、孤独的状态...
{
"entity" : "759446489112216656",
"total" : 1
}
请注意 stat 集合中缺少的“_id”和“_class”。我的 mongo 或 tomcat 日志中没有任何错误。有没有人遇到过这个问题或知道交易是什么?感谢您的帮助!
注意:如果我删除 upsert 并正常添加统计信息,一切都会增加(当然,这会为单个实体添加多个统计信息,这是不可取的)
最佳答案
我认为您可以尝试使用 @Document(collection = "colName")
注释您的类(class) - 我今天遇到了同样的问题。 Upserts 的工作有时真的很奇怪:我仍然无法找到始终如一地获取错误的方法。
实际上我所做的是创建了一个解决方法(临时的,只是“让它工作”),我必须检查对象中是否存在“_id”,如果它为空,我使用保存,如果不是 - 我使用更新。是的,这很奇怪,但确实有效。
关于Java MongoTemplate : Upserts Not Generating ObjectId,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13466773/
可写 CTE 被认为是 9.5 之前的 UPSERT 解决方案,如 Insert, on duplicate update in PostgreSQL? 中所述 可以使用以下可写 CTE 惯用法的信息
可写 CTE 被认为是 9.5 之前的 UPSERT 解决方案,如 Insert, on duplicate update in PostgreSQL? 中所述 可以使用以下可写 CTE 惯用法的信息
我正在通过 Python 在我的 MongoDB 上运行更新。我有这一行: self.word_counts[source].update({'date':posttime},{"$inc" : wo
我正在尝试使用 Sequelize 添加将数据创建到我的expressJs 应用程序中的 2 个表中的功能。 Controller : Campaign.upsert({ institutio
我正在尝试用现有的 ngxs 状态运算符替换或插入数组项 (upsert)。我目前正在使用以下 iif 语句。有更简单的方法吗? setState( patch({ contractLis
有没有一种方法可以在发生以下情况的 kdb 中更新插入: 如果键不存在,则插入值 如果key存在,检查当前值是否大于 A) 如果是,则不执行任何操作 B) 如果不是,更新值 类似于: job upse
我正在尝试更新测试键空间和表中的记录。当我插入一条记录时,接受一个列值更改,而另一个不接受。 (注意:尽管没有错误消息,我也无法删除记录) 观察 middle_initial 是如何不更新的,而 ti
我正在寻找一些有关如何优化sql存储过程的建议。 对于第一个查询,我正在执行插入操作,对于第二个查询,我正在进行更新(如果数据存在)。 我想要做的是将两个存储过程合并为一个,查询将检查数据是否存在而不
我有以下两个类(class) 条目 public class Entry { public Guid Id { get; set; } public IEnumerable Data
两年前有人问how to do upserts when you don't know a document's id . (未接受的)答案引用了 feature request 这导致了 _upda
今天这有点令人沮丧。我确定它很简单,我忽略了。请参阅下面的代码(为了示例而简化): def dbUpsert(sqlQuery, sqlValues): # Connect to the da
今天这有点令人沮丧。我确定它很简单,我忽略了。请参阅下面的代码(为了示例而简化): def dbUpsert(sqlQuery, sqlValues): # Connect to the da
我想更新插入(如果不存在则更新插入)我的 sqlite 表中的一些行。 我看到了这个sqlite命令: INSERT OR REPLACE INTO Employee (id, name, role)
我要从Amazon Mechanical Turk中提取一些数据并将其保存在mongodb集合中。 我有多个工作人员重复执行每个任务,因为略有冗余可以帮助我检查工作质量。 每次使用boto AWS p
我正在尝试测试新的 PostgreSQL upsert syntax使用以下测试代码,但出现语法错误: test=> CREATE TABLE test1 ( test(> key1
我正在使用 PostgreSQL v9.6.6。我正在运行以下 SQL: insert into t_vs_config_key (name, description, is_brand_depend
我在为 postgresql 编写强大的 upsert 函数方面得到了很多帮助。我正在尝试将值插入到包含 3 列的表中,其中两列是外键,一列是文本。 我有一个非常相似的函数,效果很好,只是没有像这个函
我在 MySQL 中有一个表,每晚都会通过 cron 任务更新。每一行都被更改,因为我从 XLS 文件获取数据并且必须更新所有值。 首先我的问题是是否值得更新每一行,或者另一方面删除所有行并再次插入。
更新插入在冲突时有效,但 id 的数据类型是串行的,这意味着我想让它自动生成/递增。如果我在不指定 id 的情况下执行插入,则插入工作正常。 我遇到的问题是将两者结合起来。为了让 key 自动递增,我
这个问题在这里已经有了答案: How to UPSERT (MERGE, INSERT ... ON DUPLICATE UPDATE) in PostgreSQL? (7 个答案) 关闭 4 年前
我是一名优秀的程序员,十分优秀!