1. First, import the package,
<dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-mongodb</artifactId> <version>1.10.7.RELEASE</version> </dependency> <dependency> <groupId>org.mongodb</groupId> <artifactId>mongo-java-driver</artifactId> <version>3.4.3</version> </dependency>
2. Configuration, you can view the official documentation :
mongodb.host=127.0.0.1 mongodb.port=27017 mongodb.databaseName=testliu mongodb.username=liu mongodb.password=123 mongodb.connectionsPerHost=8 mongodb.threadsAllowedToBlockForConnectionMultiplier=4 mongodb.connectTimeout=1000 mongodb.maxWaitTime=1500 mongodb.autoConnectRetry=true mongodb.socketKeepAlive=true mongodb.socketTimeout=1500 mongodb.slaveOk=true mongodb.writeNumber=1 mongodb.riteTimeout=0 mongodb.writeFsync=true
<mongo:db-factory id="mongoDbFactory" host="${mongodb.host}" port="${mongodb.port}" dbname="${mongodb.databaseName}" username="${mongodb.username}" password="${mongodb.password}"/> <bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate"> <constructor-arg name="mongoDbFactory" ref="mongoDbFactory" /> </bean>
3. Write unit tests:
@Before public void testBefore() { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring-mongodb.xml"); mongoTemplate = (MongoTemplate) context.getBean("mongoTemplate"); } @Test public void testAdd() { UserVo user = new UserVo(16,"liu"); mongoTemplate.save(user); }
4. When encountering problems, MongoDB has no authentication by default, so it can be accessed arbitrarily, and an error will be reported when spring connects. At this time, it is only necessary to create a new username + password and assign read and write permissions. My client uses it. mongochef, relatively simple.
org.springframework.dao.DataAccessResourceFailureException: Timed out after 30000 ms while waiting for a server that matches WritableServerSelector. Client view of cluster state is {type=UNKNOWN, servers=[{address=127.0.0.1:27017, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoSecurityException: Exception authenticating MongoCredential{mechanism=null, userName='', source='testliu', password=<hidden>, mechanismProperties={}}}, caused by {com.mongodb.MongoCommandException: Command failed with error 18: 'Authentication failed.' on server 127.0.0.1:27017. The full response is { "ok" : 0.0, "errmsg" : "Authentication failed.", "code" : 18, "codeName" : "AuthenticationFailed" }}}]; nested exception is com.mongodb.MongoTimeoutException:Timed out after 30000 ms while waiting for a server that matches WritableServerSelector. Client view of cluster state is {type=UNKNOWN, servers=[{address=127.0.0.1:27017, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoSecurityException: Exception authenticating MongoCredential{mechanism=null, userName='', source='testliu', password=<hidden>, mechanismProperties={}}}, caused by {com.mongodb.MongoCommandException: Command failed with error 18: 'Authentication failed.' on server 127.0.0.1:27017. The full response is { "ok" : 0.0, "errmsg" : "Authentication failed.", "code" : 18, "codeName" : "AuthenticationFailed" }}}]servers=[{address=127.0.0.1:27017, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoSecurityException: Exception authenticating MongoCredential{mechanism=null, userName='', source='testliu', password=<hidden>, mechanismProperties={}}}, caused by {com.mongodb.MongoCommandException: Command failed with error 18: 'Authentication failed.' on server 127.0.0.1:27017. The full response is { "ok" : 0.0, "errmsg" : "Authentication failed.", "code" : 18, "codeName" : "AuthenticationFailed" }}}]servers=[{address=127.0.0.1:27017, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoSecurityException: Exception authenticating MongoCredential{mechanism=null, userName='', source='testliu', password=<hidden>, mechanismProperties={}}}, caused by {com.mongodb.MongoCommandException: Command failed with error 18: 'Authentication failed.' on server 127.0.0.1:27017. The full response is { "ok" : 0.0, "errmsg" : "Authentication failed.", "code" : 18, "codeName" : "AuthenticationFailed" }}}]caused by {com.mongodb.MongoCommandException: Command failed with error 18: 'Authentication failed.' on server 127.0.0.1:27017. The full response is { "ok" : 0.0, "errmsg" : "Authentication failed.", "code" : 18, "codeName" : "AuthenticationFailed" }}}]caused by {com.mongodb.MongoCommandException: Command failed with error 18: 'Authentication failed.' on server 127.0.0.1:27017. The full response is { "ok" : 0.0, "errmsg" : "Authentication failed.", "code" : 18, "codeName" : "AuthenticationFailed" }}}] at org.springframework.data.mongodb.core.MongoExceptionTranslator.translateExceptionIfPossible (MongoExceptionTranslator.java:77) at org.springframework.data.mongodb.core.MongoTemplate.potentiallyConvertRuntimeException(MongoTemplate.java:2146) at org.springframework.data.mongodb.core.MongoTemplate.execute(MongoTemplate.java:484) at org.springframework.data.mongodb.core.MongoTemplate.saveDBObject(MongoTemplate.java:1104) at org.springframework.data.mongodb.core.MongoTemplate.doSave(MongoTemplate.java:1037) at org.springframework.data.mongodb.core.MongoTemplate.save(MongoTemplate.java:984) at org.springframework.data.mongodb.core.MongoTemplate.save(MongoTemplate.java:972) at com.test.MongoSpringTest.testAdd(MongoSpringTest.java:47) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498)
expand:
1) When querying, Query and Criteria objects are generally used, and Aggregation() is used for aggregation.
Aggregation aggregation = Aggregation.newAggregation( Aggregation.match(criteria), Aggregation.group(Fields.field("userId", "_id.userId"))).sum("num").as("sum").sum("mNum").as("mSum")...)2. When using map-reduce to analyze data, it is divided into several aspects,
Query query = new Query(); query.addCriteria(Criteria.where("userId").is(1)); //mapReduceOptions defines the analyzed data and the stored table location MapReduceOptions mapReduceOptions = new MapReduceOptions().outputTypeMerge().outputCollection(tableName).finalizeFunction("classpath:finalize.js"); mongoTemplate.mapReduce(query, CollectionName.ORDER, "classpath:map.js", "classpath:reduce.js", mapReduceOptions, entityClass);Among them, map defines the way of grouping, and reduce defines how to process data.
Official tutorial (grouped by username, accumulating records for each user): >db.posts.mapReduce( function() { emit(this.user_name,1); }, function(key, values) {return Array.sum(values)}, { query:{status:"active"}, out:"post_total" } ).find()
map.js function(){ emit({ user_name: this.user_name, user_Id: this.user_Id, }, { count: this.count, ip: this.ip, money: this.money, }) } reduce.js function(key, values){ reduced = {totalCount:0,ip:'',totalMoney:0}; values.forEach(function (val) { reduced.ip= val.ip; reduced.totalCount += val.count; reduced.totalMoney += val.money; }); return reduced; }