这是一个简单的pojo:
public class Description {
private String code;
private String name;
private String norwegian;
private String english;
}
Run Code Online (Sandbox Code Playgroud)
请参阅以下代码,upsert
通过spring MongoTemplate 应用于MongoDb:
Query query = new Query(Criteria.where("code").is(description.getCode()));
Update update = new Update().set("name", description.getName()).set("norwegian", description.getNorwegian()).set("english", description.getEnglish());
mongoTemplate.upsert(query, update, "descriptions");
Run Code Online (Sandbox Code Playgroud)
生成Update
对象的行Item
手动指定类的每个字段.
但如果我的Item
对象发生了变化,那么我的Dao图层会破裂
那么有没有办法避免这样做,以便我的Item
类中的所有字段自动应用于更新?
例如
Update update = new Update().fromObject(item);
Run Code Online (Sandbox Code Playgroud)
请注意,我的pojo没有扩展DBObject
.
我需要在MongoTemplate中拉一个子文档,但无法弄清楚如何做到这一点.
我保存的文件是:
{
"_id" : "FooUser",
"_class" : "com.domain.User",
"tests" : [
{
"variant" : {
"_id" : "C",
"probability" : "0.5"
},
"experiment" : {
"$ref" : "experiment",
"$id" : "MyExperiment2"
}
},
{
"variant" : {
"_id" : "B",
"probability" : "0.5"
},
"experiment" : {
"$ref" : "experiment",
"$id" : "MyExperiment1"
}
}
]
}
Run Code Online (Sandbox Code Playgroud)
我只需要删除具有MyExperiment1的测试.执行以下命令有效:
db.user.update( {}, {$pull: { "tests":{"experiment.$id":"MyExperiment1"}}}, {multi: true} )
Run Code Online (Sandbox Code Playgroud)
我应该如何使用Spring MongoTemplate编写这个?
我尝试了以下,但不起作用:
this.mongoTemplate.updateMulti(new Query(), new Update().pull("tests", "{\"experiment.$id\":\"MyExperiment1\"}"), "user");
Run Code Online (Sandbox Code Playgroud)
谢谢.
我是Spring Boot和MongoDb的新手.尝试使用Mongo Repositories和Spring Boot的一些示例.但经过一些文件发现,Mongo Template将是一个更好的选择.无法使用Mongo Template示例获得正确的Spring Boot.
有人可以帮我一个例子.
在尝试使用Mongo模板时,是否需要创建用户定义的存储库接口并扩展存储库或CRUD存储库?
我想查询我的集合中的对象,例如给定值必须属于stringArray中的值
stringArray是包含字符串列表的每个Obejct的字段名称
我在mongodb收藏的结构是
Object1
{
field1
field2
stringArray[2]
0 String0
1 String1
}
Object2
{
field1
field2
stringArray[3]
0 String0
1 String1
2 String2
}
Run Code Online (Sandbox Code Playgroud)
}
我的查询是:
Query query = new Query();
query.addCriteria(
Criteria.where(theValueIamlookingFor).in("stringArray")
);
return mongoTemplate.find(query, myObject.class);
Run Code Online (Sandbox Code Playgroud)
到目前为止,它还没有奏效.
有任何想法吗 ?
com.mongodb.CommandFailureException: { "serverUsed" : "localhost:27017" , "createdCollectionAutomatically" : true , "numIndexesBefore" : 1 , "ok" : 0.0 , "errmsg" : "namespace name generated from index name \"NDS.ABCD_pre_import.$importabilityEvaluations.perNameResults.straightImportResults.resultPolContent_NOT_IN_CURRENT_USE.officialPolResultNameContentId\" is too long (127 byte max)" , "code" : 67}
at com.mongodb.CommandResult.getException(CommandResult.java:76)
at com.mongodb.CommandResult.throwOnError(CommandResult.java:131)
at com.mongodb.DBCollectionImpl.createIndex(DBCollectionImpl.java:362)
at com.mongodb.DBCollection.createIndex(DBCollection.java:563)
at org.springframework.data.mongodb.core.index.MongoPersistentEntityIndexCreator.createIndex(MongoPersistentEntityIndexCreator.java:136)
at org.springframework.data.mongodb.core.index.MongoPersistentEntityIndexCreator.checkForAndCreateIndexes(MongoPersistentEntityIndexCreator.java:129)
at org.springframework.data.mongodb.core.index.MongoPersistentEntityIndexCreator.checkForIndexes(MongoPersistentEntityIndexCreator.java:121)
at org.springframework.data.mongodb.core.index.MongoPersistentEntityIndexCreator.onApplicationEvent(MongoPersistentEntityIndexCreator.java:105)
at org.springframework.data.mongodb.core.index.MongoMappingEventPublisher.publishEvent(MongoMappingEventPublisher.java:60)
at org.springframework.data.mapping.context.AbstractMappingContext.addPersistentEntity(AbstractMappingContext.java:306)
at org.springframework.data.mapping.context.AbstractMappingContext.getPersistentEntity(AbstractMappingContext.java:180)
at org.springframework.data.mapping.context.AbstractMappingContext.getPersistentEntity(AbstractMappingContext.java:140)
at org.springframework.data.mapping.context.AbstractMappingContext.getPersistentEntity(AbstractMappingContext.java:67)
at org.springframework.data.mongodb.core.MongoTemplate.determineCollectionName(MongoTemplate.java:1881)
at org.springframework.data.mongodb.core.MongoTemplate.determineEntityCollectionName(MongoTemplate.java:1868)
at org.springframework.data.mongodb.core.MongoTemplate.save(MongoTemplate.java:825)
Run Code Online (Sandbox Code Playgroud) 我正在尝试将 mongoDb 与 Spring 的 mongoTemplate 连接起来。我还尝试将 'spring-data-mongodb' 的版本从 1.7.2.RELEASE 更改为 1.8.2.RELEASE,但即使这样也不起作用。
下面是我在项目中使用的代码。
这是我的 pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.storeApp</groupId>
<artifactId>storeApp</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>Store Application</name>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.1.RELEASE</version>
</parent>
<dependencies>
<!-- <dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.2.4.RELEASE</version>
</dependency> -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb</artifactId>
<version>1.7.2.RELEASE</version>
</dependency>
</dependencies>
<build>
<finalName>storeApp</finalName>
</build>
<repositories>
<repository>
<id>spring-releases</id>
<name>Spring Releases</name>
<url>https://repo.spring.io/libs-release</url>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>spring-releases</id>
<name>Spring Releases</name>
<url>https://repo.spring.io/libs-release</url>
</pluginRepository>
</pluginRepositories>
</project>
Run Code Online (Sandbox Code Playgroud)
我的 SpringMongoConfig 文件
package com.storeApp.config;
import …
Run Code Online (Sandbox Code Playgroud) spring-data-mongodb 从 1.9.0.RELEASE 开始支持批量更新。
BulkOperations ops = template.bulkOps(BulkMode.UNORDERED, Match.class);
for (User user : users) {
Update update = new Update();
...
ops.updateOne(query(where("id").is(user.getId())), update);
}
ops.execute();
Run Code Online (Sandbox Code Playgroud)
mongoTemplate 有一个名为 void save(Object objectToSave) 的函数;我想插入/更新整个记录,但不是某些特定字段。有什么方法或功能可以使 Update 类无效吗?
也许是这样的..?
BulkOperations ops = template.bulkOps(BulkMode.UNORDERED, Match.class);
for (User user : users) {
...
ops.save(query(where("id").is(user.getId())), user);
}
ops.execute();
Run Code Online (Sandbox Code Playgroud) aggregate
MongoTemplate
返回的方法AggregationResults<T>
,其中T
是与mongo集合对应的类.
有时,我们只需要该集合中的单个(例如属性abc
)或几个属性(pqr
和xyz
),具体取决于特定条件.在这些情况下,我们可以将整个集合检索到T
类中,也可以创建一个包含properties(abc
)或(pqr
,xyz
)的新类.
有没有办法将这些单个属性映射到List<String>
两个属性作为键值对HashMap<String, String>
?
我有一个更新的对象/文档列表,我需要立即保存列表中的所有对象。
我在 MongoTemplate 中看到 save() 但它一次只能保存一个文档。有什么方法可以一次保存多个文档,或者我需要调用循环保存?
mongodb mongo-java spring-data-mongodb mongotemplate mongo-java-driver
我要上传文件和检索它们mongodb
的spring boot
应用程序.但我不想使用,GridFSTemplate
因为我的file size
意志不会超过16 MB
.我没有选择,GridFSTemplate
因为链接https://docs.mongodb.com/manual/core/gridfs/#faq-developers-when-to-use-gridfs中提到的要求都不符合我的要求.正在使用Document
保存文件并使用MongoTemplate
一种好的方法检索它们?MyDocument定义看起来像
@Document
public class MyDocument {
@Id
private String id;
private String emailId;
private String docType;
@CreatedDate
private DateTime created;
@LastModifiedDate
private DateTime modified;
private File document;
}
Run Code Online (Sandbox Code Playgroud)
存储文件
MyDocument document = new MyDocument();
document.setEmailId("abc@gmail.com");
document.setDocType("passport");
document.setDocument(file);
mongoTemplate.insert(document);
Run Code Online (Sandbox Code Playgroud)
我想要store file
一些类似的信息email
.后来,我将retrieve
这个file
基于email
参数.请建议这种方法是否合适,或者是否赞赏任何其他更好的解决方案.
mongotemplate ×10
mongodb ×9
java ×4
spring ×3
spring-boot ×2
criteria ×1
file-upload ×1
mongo-java ×1
spring-mvc ×1
upsert ×1