mongodb 如何命令转换java语句

本人刚刚开始做后台相关东西,对数据库不是特别了解,请问下如何将以下语句转为在java 框架中使用
CommandResult result = mongoTemplate.executeCommand(xxx)
数据库语句:
db.BrowseCarHistory.update({userCode:"U725845783294804"},{
$currentDate: {
lastModified: true

 },$push: {browseCarHistoryInfos: {$each: [ {
        "sku" : "sku222",
        "productId" : "s00000000`",
        "shopId" : "s000000001",
        "categoryId" : "c000000001",
        "shortImage" : "www.baidu.com",
        "updatetime" : ISODate("2019-01-10T02:47:07.219Z")
    }],$slice: -3}}},{upsert:true})

以下是执行后的结果
{
"_id" : ObjectId("5c332b183ffb98581481ff5e"),
"userCode" : "U725845783294805",
"lastModified" : ISODate("2019-01-07T10:34:53.112Z"),
"browseCarHistoryInfos" : [
{
"sku" : "sku2",
"productId" : "s00000000",
"shopId" : "s000000001",
"categoryId" : "c000000001",
"shortImage" : "www.baidu.com",
"updatetime" : ISODate("2019-01-10T02:47:07.219Z")
},
{
"sku" : "sku222",
"productId" : "s00000000
",
"shopId" : "s000000001",
"categoryId" : "c000000001",
"shortImage" : "www.baidu.com",
"updatetime" : ISODate("2019-01-10T02:47:07.219Z")
}
]
}

跪求大神帮忙,没有分数了,谢谢

1个回答

String JsonSql = JsonUtil.writeValueAsString(info);
DBObject dbUserCode = (DBObject) JSON.parse("{userCode:" + "\"" + usercode + "\"" + " }");
DBObject dbUpdate = (DBObject) JSON.parse(
"{$currentDate: {createtime:true}," +
"$push:{ browseCarHistoryInfos:{$each:[ " + JsonSql + "],\n" +
"\t $slice:-100 } } }"
);

    mongoTemplate.getCollection(collectionName).update(dbUserCode, dbUpdate, true, true);
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐