代码之家  ›  专栏  ›  技术社区  ›  Kanagavelu Sugumar

Mongodb:基于存在创建或更新文档的不同部分

  •  0
  • Kanagavelu Sugumar  · 技术社区  · 9 年前

    团队

    当文档不存在时,我必须创建以下文档(find_on_user)。

    {
          "st"          :  "s",       //current status s=sent, d=delivered, u=undelivered, r=resent
          "u"           :  "user",
          "e"           :  [         
                           //HISTORY OF EVENTS "s" - status at the time of each event
                    {"c": "2b3de827-ac38-4023-5950-819947858eac", "s":"s", "@":ISODate("2017-01-29T08:43:00Z")}
          ]
    }
    

    如果存在,对于连续的上述事件,我必须只更新事件数组。请注意,主文档中的最新状态(“st”)也会根据添加到数组中的新事件进行更新。

    {
          "st"          :  "r",       //current status s=sent, d=delivered, u=undelivered, r=resent
          "u"           :  "user",
          "e"           :  [         
                           //HISTORY OF EVENTS "s" - status at the time of each event
                    {"c": "2b3de827-ac38-4023-5950-819947858eac", "s":"s", "@":ISODate("2017-01-29T08:53:00Z")},
                    {"c": "2b3de827-ac38-4023-5950-819947858eac", "s":"u", "@":ISODate("2017-01-29T09:43:00Z")},
                    {"c": "2b3de827-ac38-4023-5950-819947858eac", "s":"r", "@":ISODate("2017-01-29T10:43:00Z")}
          ]
    }
    

    我想我想要的是加上$push

    db.collection.update(query, update, {upsert: true})
    
    DBObject listItem = new BasicDBObject("e", new BasicDBObject("c","2b3de827-ac38-4023-5950-819947858eac").append("s","r").append("@",ISODate("2017-01-29T10:43:00Z"));
    DBObject updateQuery = new BasicDBObject("$push", listItem);
    myCol.update(findQuery, updateQuery);
    

    如何在mongodb spring数据中一起实现这一点?请帮忙。

    问:这和什么很相似 mongodb - create doc if not exist, else push to array

    但这并没有告诉我们如何将外部文档(“st”)与$push一起更新?另外,这个答案是处理字符串Json,这是否可能通过java对象实现?

    从Veeram回答:


    Query query = new Query(Criteria.where("u").is("user"));
    
    DBObject listItem = new BasicDBObject("c","2b3de827-ac38-4023-5950-819947858eac").append("s","r").append("@",new Date());
    
    Update update = Update.update("st","r").push("e", listItem);
    
    mongoOperations.upsert(query, update, collection_name);
    

    因为我只有两个参数,比如“u”,所以“st”Veeram在查询时使用了一个,在更新时使用了另一个。如果我有两个以上,但只有在插入时才更新,以后只有状态(“st”)才会更新。

    下面的示例“type”和“account_id”将在插入时添加,稍后的更新将仅在“st”进行。如何解决这个问题?

    {
          "st"          :  "s",       //current status s=sent, d=delivered, u=undelivered, r=resent
          "u"           :  "user",
          "type"        :  "welcome-sms",
          "account_id"  :  "12as-df23-fg-1",
          "e"           :  [         
                           //HISTORY OF EVENTS "s" - status at the time of each event
                    {"c": "2b3de827-ac38-4023-5950-819947858eac", "s":"s", "@":ISODate("2017-01-29T08:43:00Z")}
          ]
    

    我需要像下面那样使用吗?但是type和account_ id是静态的。仅在第一次(插入)时需要更新。好吗?

    Update updateObj = Update.update("st","r")
    updateObj.set("type", "welcome-sms");
    updateObj.set("account_id", "12as-df23-fg-1").push("e", listItem);
    
    1 回复  |  直到 9 年前
        1
  •  1
  •   s7vr    9 年前

    你可以试试这样的东西。

    Query query = new Query(Criteria.where("u").is("user"));
    
    DBObject listItem = new BasicDBObject("c","2b3de827-ac38-4023-5950-819947858eac").append("s","r").append("@",new Date());
    
    Update updateObj = Update.update("st","r")
    
    updateObj.setOnInsert("type", "welcome-sms");
    updateObj.setOnInsert("account_id", "12as-df23-fg-1").push("e", listItem);
    
    mongoOperations.upsert(query, updateObj, collection_name);