Java实现mongo JS脚本写入总结

JS:脚本数据

String codeString = "

db.Role.insert(
{
    "_id": "SYSADMIN5QR5PEWTQJIREFETLUBN58OB",
    "name": "test1234",
    "memo": "",
    "enabled": true,
    "builtIn": true,
    "creator": {
        "_id": "ADMINP1QSGK8SIIPZP7B536VKNS8709C",
        "account": "test",
        "name": "1234",
        "valid": true
    },
    "updater": {
        "_id": "ADMINP1QSGK8SIIPZP7B536VKNS8709C",
        "account": "test",
        "name": "1234",
        "valid": true
    },
    "createdTime": ISODate("2019-08-23T01:05:17.238Z"),
    "modifiedTime": ISODate("2019-12-30T10:11:01.274Z"),
    "valid": true,
    "_class": "com........Role"
});

";

1、方法一: spring data 文档官方方法

 ExecutableMongoScript executableMongoScript = new ExecutableMongoScript(codeString);
 ScriptOperations scriptOps = mongoTemplate.scriptOps();
 scriptOps.execute(mongoScript);

2、方法二:doVal

 Object object1 = mongoTemplate.getDb().doEval(codeString);

3、方法三:bson

 BasicDBObject bson = new BasicDBObject();
 bson.put("$eval", codeString);
 Object object2 =  mongoTemplate.getDb().command(bson);

或者

 Object object3 = mongoTemplate.getCollection("$cmd").findOne(bson);

 

 

你可能感兴趣的:(---15.1,mongoDB)