Spring Data Mongo可以仅更新文档中的脏字段吗? [英] Can Spring Data Mongo update only dirty field in a document?

查看:221
本文介绍了Spring Data Mongo可以仅更新文档中的脏字段吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经开始将spring-data-mongo用于需要保留很多内容的应用程序.

I've started using spring-data-mongo for an application where there are a lot of stuff to be persisted.

我们实际上选择了mongo,因为它的广告是这样.现在沉迷于春季,我们发现使用其中的某些功能(对辛勤工作的春季数据人员来说是很赞的)使我们的生活变得很轻松.

We actually chose mongo because it's advertised so. Now heavily addicted to spring we found our life very easy using some of it's features (kudos for the hard work spring data folks).

但是有一件事:文档的60 fields以上.所以我对speedscalability的问题是,spring-data-mongo能否像Hibernate一样仅更新mongo database中的dirty fields? 此处Arthur Ronald F D Garcia

But there is one thing: a document with over 60 fields. So my question about speed and scalability is that can spring-data-mongo only update the dirty fields in the mongo database just like Hibernate does it? a little like how it was explained here by Arthur Ronald F D Garcia

感谢您阅读

推荐答案

来自MongoDB文档:

From MongoDB documentation:

如果 update 参数仅包含字段和值对,则update_)方法将现有文档替换为 update 参数中的文档,但_id字段除外

If the update argument contains only field and value pairs, the update() method replaces the existing document with the document in the update argument, except for the _id field.

换句话说,MongoDB的更新的工作方式与您描述的完全相同.因此,如果您要将此文档纳入测试范围:

In other words, MongoDB's update works exactly like you described. So if you have this document in test colletion:

{ "_id" : "123", "oldfield" : "oldvalue" }

并在mongo CLI中运行以下命令:

And run the following in mongo CLI:

db.test.update({"_id": "123"}, { newfield : "value" })

新文档将如下所示:

{"_id": "123", "newfield" : "value"}

如果只想修改某些字段,则可以使用$ set运算符,该运算符将为一个或多个字段设置一个值.在使用spring数据时,应使用MongoTemplate类进行此更新:

If you want to modify just some fields, you could use the $set operator that will set a value for one or more fields. As you are using spring data, you should use MongoTemplate class to make this updates:

Query q = new Query(Criteria.where("_id").is("123"));
Update u = Update
            .update("newfield", "value")
            .set("oldfield", "newvalue");

this.template.findAndModify(query, update, Entity.class);

您可以在此处了解有关$ set运算符和mongodb更新的信息: http://docs.mongodb.org/manual/reference/operators/#_S_set http://docs.mongodb.org/manual/applications/update

You could read about $set operator and mongodb update here: http://docs.mongodb.org/manual/reference/operators/#_S_set http://docs.mongodb.org/manual/applications/update

这篇关于Spring Data Mongo可以仅更新文档中的脏字段吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆