2016-02-21 20 views
1

我試圖用使用的java, 所以每一個解決方案不包括Java的反應將不會被接受的MongoDB V3.2的更新插入功能。的Upsert嵌套對象使用Java驅動程序

我的問題是,UPSERT的指令超控嵌套的對象,而不是增加新的,我曾嘗試使用「$ addToSet」和「」,但沒有成功,我得到一個錯誤消息,表明存儲引擎不支持此命令。

我想更新客戶端的文檔以及它們的內部對象,如檢查和檢查的值。 客戶端doc的全局結構如下。的

 
Client 
| 
|__Checks // array of checks , update or insert operation 
    | 
    |__values // array of values, every check has its own values (20 max) 
       // update using index(id) 

鏈接:Example's source code

我的目的是隻用一個查詢,而無需使用多個查詢更新客戶端的文件。

我不是mongodb的專家,所以每一個建議或評論家將不勝感激。

即使我這樣做都是錯,可隨時通知我,並使用的Java爲蒙戈3.2討好。

enter image description here

enter image description here

enter image description here

這裏是用來產生最後結果的源代碼。

package org.egale.core; 

import com.mongodb.MongoClient; 
import com.mongodb.client.MongoCollection; 
import com.mongodb.client.MongoDatabase; 
import com.mongodb.client.model.UpdateOptions; 
import java.util.ArrayList; 
import java.util.List; 
import org.bson.Document; 

/** 
* 
* @author Zied 
*/ 
public class MongoTest { 

    /** 
    * Pojo used to populate data 
    */ 
    static class CheckModel { 
     public String client; 
     public String checkId; 
     public String name; 
     public String command; 
     public String description; 
     public String topic; 
     public int refresh = 60; 
     public int status; 
     public String output; 
    } 

    static MongoClient mongoClient = new MongoClient(); 
    static String dbName = "eagle"; 

    private static List<Document> getCheckValues(CheckModel checkModel, int index) { 

     final List<Document> checkValues = new ArrayList<>(); 
     final Document val = new Document() 
       .append("id", index) 
       .append("output", checkModel.output) 
       .append("status", checkModel.status); 
     checkValues.add(val); // second execution should not ovveride the content of value but a new 
     return checkValues; 
    } 

    private static void insertCheck(MongoDatabase db, CheckModel checkModel) { 
     int idx =++index % 20; 
     final List<Document> checks = new ArrayList<>(); 
     final Document check = new Document() 
       .append("name", checkModel.name) 
       .append("command", checkModel.command) 
       .append("id", checkModel.checkId) 
       .append("description", checkModel.description) 
       .append("topic", checkModel.topic) 
       .append("last_output", checkModel.output) 
       .append("index", index) 
       .append("last_status", checkModel.status) 
       .append("values", getCheckValues(checkModel,idx)) 
       .append("refresh", checkModel.refresh); 
     checks.add(check); 

     Document client = new Document() 
       .append("name", checkModel.client) 
       .append("checks", checks); 
     //.append("$addToSet" , new Document("checks", checks)); // <<- error here '$addToSet' is not recocnized 

     db.getCollection("clients") // execute client insert or update 
       .updateOne(
         new Document().append("_id", checkModel.client), new Document("$set", client), new UpdateOptions().upsert(true) 
       ); 
    } 

    static int index = 0; 

    // Name of the topic from which we will receive messages from = " testt" 
    public static void main(String[] args) { 
     MongoDatabase db = mongoClient.getDatabase(dbName); 

     CheckModel checkModel = new CheckModel(); 
     checkModel.command = "ls -lA"; 
     checkModel.client = "client_001"; 
     checkModel.description = "ls -l command"; 
     checkModel.checkId = "lsl_command"; 
     checkModel.name = "client 001"; 
     checkModel.output = "result of ls -l"; 
     checkModel.status = 0; 
     checkModel.topic = "basic_checks"; 
     checkModel.refresh = 5000; 

     initDB(db); 
     // insert the first check 
     insertCheck(db, checkModel); 
     // insert the second check after some modification 
//  insertCheck(db, modifyData(checkModel)); 

    } 
    // mdofiy data to test the check 
    private static CheckModel modifyData(CheckModel checkModel){ 
     checkModel.status = 1; 
     checkModel.output = "ls commadn not found"; 
     return checkModel; 
    } 
    private static void initDB(MongoDatabase db) { 
     MongoCollection<Document> collection = db.getCollection("configuration"); 
     if (collection.count() == 0) { 
      Document b = new Document() 
        .append("_id", "app_config") 
        .append("historical_data", 20) 
        .append("current_index", 0); 
      collection.insertOne(b); 
     } 

     Document b = new Document().append("none", "none"); 

     MongoCollection<Document> clients = db.getCollection("clients"); 
     clients.insertOne(b); 
     clients.deleteOne(b); 

     MongoCollection<Document> topics = db.getCollection("topics"); 
     topics.insertOne(b); 
     topics.deleteOne(b); 
    } 

} 

回答

-1

您可以使用$推,每個$,$切片解決您的問題,請參見ALSE https://docs.mongodb.org/manual/reference/operator/update/slice/

db.students有以下文件

{ "_id" : 10, "scores" : [ 1, 2, 3 ] } 

db.students.update(
    { _id: 10 }, 
    { 
    $push: { 
     scores: { 
     $each: [ 4 ], 
     $slice: -3 
    } 
    } 
} 
) 

結果是:

{ "_id" : 10, "scores" : [ 2, 3, 4] } 
+0

感謝您的答覆,但是這不是我尋找??! –