【问题标题】:How to push json data into an arrya object in MONGO DB by java如何通过java将json数据推送到MONGODB中的数组对象中
【发布时间】:2015-11-24 07:14:44
【问题描述】:
 "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 to scopeNote object where name = Milkchocolate by java,请帮帮我

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

【问题讨论】:

  • 你试过什么?请显示代码尝试提供一些上下文。 "termsrelation" 是集合名称还是文档中的元素,目前还不清楚。
  • 文档中的元素
  • 我试过这个查询 { "$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" : "牛奶巧克力"}]} 但没用
  • edit您的问题,而不是在 cmets 中发帖

标签: java json mongodb mongodb-query


【解决方案1】:

步骤 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();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-05-21
    • 1970-01-01
    • 2016-03-05
    • 1970-01-01
    • 2023-01-18
    • 2017-09-19
    • 2016-12-06
    • 2021-06-27
    相关资源
    最近更新 更多