简体   繁体   English

使用Java springdata保存带有上限数组($ slice和$ sort)的mongoDB文档

[英]Saving with Java springdata a mongoDB document with capped array ($slice and $sort)

I'm developing a log register using mongoDB and Java SpringData. 我正在使用mongoDB和Java SpringData开发日志寄存器。

Here MongoDb capped sub-collection talks about mongoDB structure, but I would do with Java. 在这里, MongoDb的子集合加盖了有关mongoDB结构的讨论,但是我会使用Java。 The most important thing it's that I have a document with one or more fields and a capped array. 最重要的是,我有一个包含一个或多个字段和一个上限数组的文档。

Is there some method or way in Java to do this? Java中是否有某种方法或方法可以做到这一点?

My object it's like: 我的对象是这样的:

user = {
   name: String,
   latest_messages: [String] (capped to a 100 elements)
}

in Java: 在Java中:

public class MessageLog {
    private ObjectId id;
    private String name;
    private List<Message> messages;
}

Where: 哪里:

public class Message{
    private String text;
    private String level;
    private Date date;
}

EDIT : 编辑

I'm using Java with Spring source (mongodb driver v2.10.1: http://central.maven.org/maven2/org/mongodb/mongo-java-driver/2.10.1/ ) 我正在将Java与Spring源代码一起使用(mongodb驱动程序v2.10.1: http ://central.maven.org/maven2/org/mongodb/mongo-java-driver/2.10.1/)

I finally get the solution using this code: 我终于使用以下代码获得了解决方案:

// Define the search query
BasicDBObject searchQuery = new BasicDBObject().append("idU", idUser);

// To create the json query to modify
BasicDBObject logDocument = new BasicDBObject();

// Create the object and add it to a list (because the $each require a list)
List<DBObject> list = new ArrayList<DBObject>();
DBObject object = new BasicDBObject().append("text", logMessage.getText());
object.append("level", logMessage.getLevel())
object.append("date", logMessage.getDate());
list.add(object);

// Create the $sort and $slice query at the same time
logDocument.append(
    "$push",
    new BasicDBObject().append("logs", new BasicDBObject().append("$each", list).append("$sort", new BasicDBObject().append("date", 1))
            .append("$slice", -10)));

String json = "{findAndModify:\"collectionLog\", query:" + searchQuery.toString() + ", update: " + logDocument.toString() + ", upsert:true}";

try {
     getMongoTemplate().executeCommand(json);
} catch (Exception e) {
     System.out.println(e);
}

I hope it will be useful to someone else!!! 希望对其他人有用!!!

As you have noticed, MongoDB can't provide this functionality, so it has to be solved on the application layer. 您已经注意到,MongoDB无法提供此功能,因此必须在应用程序层上解决它。

Java by default doesn't support anything which works like a capped collection. Java默认情况下不支持任何类似于上限集合的功能。 But the collection library of the Apache Commons project has the class CircularFiFoBuffer which seems to be what you need. 但是,Apache Commons项目的集合库中的CircularFiFoBuffer似乎正是您所需要的。 You create it with a max size. 您以最大大小创建它。 When you add a new element and it already is at the max size, the oldest element is discarded. 当您添加一个新元素并且已经达到最大大小时,最旧的元素将被丢弃。 Iteration order is from the oldest element to the newest. 迭代顺序是从最早的元素到最新的元素。

A workaround without external libraries could be done with a LinkedList . 没有外部库的解决方法可以通过LinkedList完成。 Add new elements using the addFirst() method, check the size, and when it's larger than the desired maximum size, call removeLast(). 使用addFirst()方法添加新元素,检查大小,当它大于所需的最大大小时,调用removeLast()。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM