gpt4 book ai didi

java - 如何使用 java 删除 MongoDB 上的特定字段?

转载 作者:行者123 更新时间:2023-11-30 06:00:11 25 4
gpt4 key购买 nike

我想在显示 JSON 中隐藏 id_jadwal。但现在 id_jadwal 继续显示在我的 JSON 上 我想在显示 JSON 中隐藏 id_jadwal。我在我的框架和 Jackson 中使用 MongoDB 和 java jx-rs

[
{
"name": "Style",
"startTimestamp": 1507611720,
"endTimestamp": null,
"place": "gh",
"description": "gh",
"status": "DONE",
"creator": {
"name": "Pulan",
"email": "sd@gmail.com",
"photo": "https://lh6.googleusercontent.com/-Q5Tx0bdvkAU/AAAAAAAAAAI/AAAAAAAAEyU/83rb_x1tyXA/photo.jpg"
},
"invitedUsers": [
{
"name": "Petra",
"email": "sd.class@gmail.com",
"photo": "",
"present": false,
"nearby": false
}
],
"id_jadwal": null,
"_id": "59db02e8212cba197b7e9d2c"
}

}]

我的代码:

BasicDBObject query = new BasicDBObject();
query.put("creator.email", email);
query.put("status", status.toString());
MongoCursor<Document> cursor = collection.find(query).iterator();
ObjectMapper objectMapper = new ObjectMapper();

while (cursor.hasNext()) {
String jsonUser = cursor.next().toJson();

Event event = objectMapper.readValue(jsonUser, Event.class);
//objectMapper.setVisibility(event.getId_jadwal(), Visibility.NONE);

events.add(event);
events.remove("id_jadwal");

}

最佳答案

您必须在请求级别明确排除您的字段,使用 Projection :

MongoCursor<Document> cursor = collection.find(query).projection(Projections.exclude("id_jadwal").iterator();

关于java - 如何使用 java 删除 MongoDB 上的特定字段?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52400615/

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