Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java MongoTemplate:Upserts未生成ObjectId_Java_Mongodb_Spring Mongo - Fatal编程技术网

Java MongoTemplate:Upserts未生成ObjectId

Java MongoTemplate:Upserts未生成ObjectId,java,mongodb,spring-mongo,Java,Mongodb,Spring Mongo,我一直在开发一个使用MongoDB作为存储形式的java应用程序,但是我遇到了一个问题。当用户在我的应用程序中添加评论时,它会将文档添加到评论集合中,然后对统计数据进行upsert。但是,upsert只添加第一次(更新或插入新数据后没有调用)。以下是相关代码: public class CommentDAO implements ICommentDAO { @Autowired @Qualifier(value = "mongoDB") MongoTemplate mo

我一直在开发一个使用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);
    }
}
以下是“我的评论”集合中的结果示例:

{ 
    "_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集合中缺少的“\u id”和“\u class”。我的mongo或tomcat日志中没有任何错误。以前有没有人遇到过这个问题,或者知道交易是什么?谢谢你的帮助

注意:
如果我删除upsert并正常添加stat,那么一切都会很好地添加(当然,这会为单个实体添加多个stats,这是不可取的)

我想你可以尝试用
@Document(collection=“colName”)
-我今天也遇到了同样的问题。Upsert有时工作起来真的很奇怪:我仍然无法找到一致地获得bug的方法


实际上,我所做的是创建了一个变通方法(临时的,只是“让它工作”),在这里我必须检查对象中是否存在“_id”,如果它为null,我使用save,如果不是,我使用update。是的,这很奇怪,但它起作用了。

我遇到了一个问题,我在升级一个已经有id的对象时遇到了问题,所以这个id被保留了下来。但是,没有保存_类。我的解决方法是向类中添加一个成员,以确保_类数据始终存在:

@Field("_class")
protected String _class = Foo.class.getName();

您能否提供您运行的MongoDB版本以及有关统计数据集合的更多信息?通过运行db.statistics.stats()-用集合名称替换“statistics”,可以在mongo shell上打印集合摘要。您是如何从stats集合中输出条目的?不可能在没有ObjectId()的情况下将其插入MongoDB中-那么,在upsert之前,这是Java POV中的样子吗?