gpt4 book ai didi

java - 使用 Java 驱动程序在 mongodb 3.2 中更新嵌套对象

转载 作者:行者123 更新时间:2023-11-29 09:28:36 25 4
gpt4 key购买 nike

我正在尝试使用 java 使用 mongodb v3.2 的更新插入功能,因此,不接受不包含 Java 响应的所有解决方案。

我的问题是 upsert 命令覆盖嵌套对象而不是添加新对象,我尝试使用“$addToSet”和“push”,但没有成功我收到一条错误消息,指出存储引擎不支持此命令。

我想更新客户的文档及其内部对象,例如支票和支票的值。客户端文档的全局结构如下。

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)

link of the: Example's source code

My intention is to use only one query to update client's document without using many queries.

I'm not specialist in mongodb, so every advice or critics would be appreciated.

Even if I'm doing this all wrong, feel free to notify me, and please using java for mongo 3.2.

enter image description here

enter image description here

enter image description here

Here is the source code used to generate the last result.

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);
}

}

最佳答案

您可以使用 $push、$each、$slice 来解决您的问题,另见 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] }

关于java - 使用 Java 驱动程序在 mongodb 3.2 中更新嵌套对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35541406/

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