java用spring管理mongodb 怎么修改mongodb中list中的数据

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java用spring管理mongodb 怎么修改mongodb中list中的数据相关的知识,希望对你有一定的参考价值。

MongoClient client = new MongoClient("192.168.0.110", 27017);
DBCollection dbCollection = client.getDB("test").getCollection("update_array");
dbCollection.drop();
dbCollection = client.getDB("test").getCollection("update_array");
BasicDBObject dbObject = new BasicDBObject();
dbObject.append("id", 583739819102582565L).append("kids", Arrays.asList("1", "2", "3"));
dbCollection.insert(dbObject);
System.out.println(dbCollection.findOne());
BasicDBObject dbObject2 = new BasicDBObject(dbObject);
dbObject2.append("kids", Arrays.asList("1", "2", "3", "4"));
dbCollection.update(dbObject, dbObject2);
System.out.println(dbCollection.findOne());


测试结果:

"_id" : "$oid" : "546f37f7d01de241a1e143e8" , "id" : 583739819102582565 , "kids" : [ "1" , "2" , "3"]

"_id" : "$oid" : "546f37f7d01de241a1e143e8" , "id" : 583739819102582565 , "kids" : [ "1" , "2" , "3" , "4"]



<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongo-java-driver</artifactId>
<version>2.12.3</version>
</dependency>

参考技术A spring不负责和数据库交互吧

spring boot aop日志管理(MongoDB)

aop拦截的是controller层请求,正常的请求用@Before来拦截,
异常的请求用@AfterThrowing来拦截
1、引用aop jar包

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-aop</artifactId>
        <version>2.0.3.RELEASE</version>
    </dependency>

2、代码实现

@Aspect
@Component
@Slf4j
public class LogAop {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Pointcut("execution(public * com.caody.muyi.controller.*.*(..))")
    public void logAop(){};

    @Before("logAop()")
    public void around(JoinPoint joinPoint){
        log.info("user:cdy");
        log.info("time:"+new Date());
        log.info("CLASS_METHOD : " + joinPoint.getSignature().getDeclaringTypeName() + "." + joinPoint.getSignature
            ().getName());
        log.info("ARGS : " + Arrays.toString(joinPoint.getArgs()));

        OperationLog operationLog = new OperationLog();
        operationLog.setLogname("cdy");
        operationLog.setLogtype("业务日志");
        operationLog.setCreatetime(new Date());
        operationLog.setUserid(1);
        operationLog.setClassname(joinPoint.getSignature().getDeclaringTypeName());
        operationLog.setMethod(joinPoint.getSignature().getName());
        operationLog.setSucceed("成功");
        operationLog.setMessage("");
       mongoTemplate.save(operationLog);
    }

//    @AfterReturning(returning = "object", pointcut = "logAop()")
//    public void after(Object object){
//        System.out.println(object);
//        log.info("RESPONSE : " + object);
//    }

    @AfterThrowing(pointcut = "logAop()", throwing="e")
    public  void  afterThrowing(JoinPoint joinPoint, Throwable e){
        log.info("user:cdy");
        log.info("time:"+new Date());
        log.info("path : " + joinPoint.getSignature().getDeclaringTypeName() + "." + joinPoint.getSignature
            ().getName());
        log.info("param : " + Arrays.toString(joinPoint.getArgs()));
        log.info("异常代码:" + e.getClass().getName());
        log.info("异常信息:" + e.getMessage());

        OperationLog operationLog = new OperationLog();
        operationLog.setLogname("cdy");
        operationLog.setLogtype("异常日志");
        operationLog.setCreatetime(new Date());
        operationLog.setUserid(1);
        operationLog.setClassname(joinPoint.getSignature().getDeclaringTypeName());
        operationLog.setMethod(joinPoint.getSignature().getName());
        operationLog.setSucceed("失败");
        operationLog.setMessage(e.getMessage());
        mongoTemplate.save(operationLog);

    }

}


作者:周六不算加班
链接:https://www.jianshu.com/p/a0f3adced194
来源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

以上是关于java用spring管理mongodb 怎么修改mongodb中list中的数据的主要内容,如果未能解决你的问题,请参考以下文章

怎么在java中获得mongodb分组group的结果中的总数量

spring boot aop日志管理(MongoDB)

JAVA MONGODB 怎么实现ID自增

用Spring data查询mongodb的问题,求解答

spring mongodb push操作

java怎么导出mongodb数据