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

标签 java json mongodb mongodb-query

 "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/

相关文章:

javascript - $ 或 Mongo 的一部分在 Meteor 应用程序中吗?

java - 如何在jsp测试属性中附加字符串

linux - linux安装jdk的正确方法是什么

java - 获取当前类文件路径上方两个目录的路径

c# - C#中使用DataContractJsonSerializer反序列化JSON对象

mongodb - 根据聚合结果过滤大型数据集

java - 从 Android 应用程序在 openfire 上创建一个组

javascript - 检查 JSON 文件内数组中的值

javascript - 如何通过socket.io发送JSON

sql-server - 如何配置 Rebus 使其具有基于处理程序类型的主题