gpt4 book ai didi

java - 如何通过java将json数据推送到MONGODB中的数组对象中

转载 作者:行者123 更新时间:2023-12-01 10:51:13 24 4
gpt4 key购买 nike

 "termsrelation" : [ 
{
"rel": "RT",
"terms": [
{
"objid": "55dc25083d2cbcb8b0dc48c8",
"source": null,
"scopeNote": [
{
"sourceType": "source",
"source": "abc"
}
],
"formated_name": "Milkbuns",
"name": "Milkbuns",
"type": null,
"url": "test.com",
"nodeid": "14050"
},
{
"objid": "552cae1940feb9123e3f5fb6",
"source": null,
"scopeNote": [
{
"sourceType": "source",
"source": "xyz"
}
],
"formated_name": "Milkchocolate",
"name": "Milkchocolate",
"type": null,
"url": "test.com",
"nodeid": "193570"
}
]
}
]

我需要插入

{
"sourceType" : "source",
"source" : "def"
}

json 到scopeNote 对象,其中name = Milkchocolate by java,请帮助我

我尝试了这个查询{ "$push" : { "termsrelation.$.terms.$.scopeNote" : { "sourceType" : "source" , "source" : "ddd" }}}带有搜索查询{ "$and" : [ { "id" : "4003"} , { "termsrelation.rel" : "RT"} , { "termsrelation.terms.scopeNote.sourceType" : "source"} , { "termsrelation.terms.scopeNote.source" : "xyz"} , { "termsrelation.terms.name" : "Milk chocolate"}]}但没有成功

最佳答案

第1步。首先获取要插入的元素的位置

第 2 步:您获得了该职位

然后使用此查询

List<BasicDBObject> andQuery2 = new ArrayList<BasicDBObject>();
andQuery2.add(new BasicDBObject("id", id));
andQuery2.add(new BasicDBObject("termsrelation.rel", relation));
andQuery2.add(new BasicDBObject("termsrelation.terms.name", name));

BasicDBObject searchObj2 = new BasicDBObject();
searchObj2.put("$and", andQuery2);

BasicDBObject basicDBObject = new BasicDBObject();
basicDBObject.put("sourceType", type);
basicDBObject.put("source", source);


BasicDBObject updateQuery = new BasicDBObject();
updateQuery.append("$push", new BasicDBObject().append("termsrelation.$.terms."+position+".scopeNote", basicDBObject));


coll.update(searchObj2, updateQuery).getError();

关于java - 如何通过java将json数据推送到MONGODB中的数组对象中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33887489/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com