用java对mongodb中的子文档进行排序。

时间:2021-11-10 13:11:59

uI have an array of subdocument in the document of mongodb.
I want to sort those subdocuments. I found it is possible to sort while updating the array.
link: http://docs.mongodb.org/manual/reference/operator/update/sort/

在mongodb的文档中,uI有一个子文档数组。我想对这些子文档进行排序。我发现在更新数组时可以进行排序。链接:http://docs.mongodb.org/manual/reference/operator/update/sort/

I want to do this in java. Here's part of my code:

我想用java做这个。以下是我的部分代码:

BasicDBObject each = new BasicDBObject("$each", input);
BasicDBObject operations = each.append("$slice", "-10").append("$sort", new BasicDBObject("order",1));
push = new BasicDBObject("$push", new BasicDBObject("datas", operations));
collection.update(query,  push);

But it turns out that it didn't turn "operations" to operators but directly push them into documents and made "$each", "$slice" and "$sort" as fields. Where did I do wrong?

但事实证明,它并没有将“操作”转化为操作,而是直接将其转化为文档,并将“每一美元”、“每一美元”和“每一美元”作为字段。我哪里做错了?

1 个解决方案

#1


4  

I have implemented the java version of the query shown in the link you mentioned above.

我已经实现了您上面提到的链接中显示的查询的java版本。

Query is :

查询的方法是:

db.students.update( { name: "joe" },
                    { $push: { quizzes: { $each: [ { id: 3, score: 8 },
                                                   { id: 4, score: 7 },
                                                   { id: 5, score: 6 } ],
                                          $sort: { score: 1 },
                                          $slice: -5
                                        }
                             }
                    }
                  )

Using Java Driver you can implement it as follows :

使用Java驱动程序可以实现如下:

DBCollection coll = db.getCollection("students");

DBObject query = new BasicDBObject("name", "joe");

DBObject dbObj1 = new BasicDBObject();
dbObj1.put("id", 3);
dbObj1.put("score", 8);

DBObject dbObj2 = new BasicDBObject();
dbObj2.put("id", 4);
dbObj2.put("score", 7);

DBObject dbObj3 = new BasicDBObject();
dbObj3.put("id", 5);
dbObj3.put("score", 6);

BasicDBList eachList = new BasicDBList();
eachList.add(dbObj1);
eachList.add(dbObj2);
eachList.add(dbObj3);

BasicDBObject quizzesObj = new BasicDBObject();
quizzesObj.put("$each", eachList);
quizzesObj.put("$sort", new BasicDBObject("score", 1));
quizzesObj.put("$slice", -5);

coll.update(query, new BasicDBObject("$push", new BasicDBObject("quizzes", quizzesObj)));

#1


4  

I have implemented the java version of the query shown in the link you mentioned above.

我已经实现了您上面提到的链接中显示的查询的java版本。

Query is :

查询的方法是:

db.students.update( { name: "joe" },
                    { $push: { quizzes: { $each: [ { id: 3, score: 8 },
                                                   { id: 4, score: 7 },
                                                   { id: 5, score: 6 } ],
                                          $sort: { score: 1 },
                                          $slice: -5
                                        }
                             }
                    }
                  )

Using Java Driver you can implement it as follows :

使用Java驱动程序可以实现如下:

DBCollection coll = db.getCollection("students");

DBObject query = new BasicDBObject("name", "joe");

DBObject dbObj1 = new BasicDBObject();
dbObj1.put("id", 3);
dbObj1.put("score", 8);

DBObject dbObj2 = new BasicDBObject();
dbObj2.put("id", 4);
dbObj2.put("score", 7);

DBObject dbObj3 = new BasicDBObject();
dbObj3.put("id", 5);
dbObj3.put("score", 6);

BasicDBList eachList = new BasicDBList();
eachList.add(dbObj1);
eachList.add(dbObj2);
eachList.add(dbObj3);

BasicDBObject quizzesObj = new BasicDBObject();
quizzesObj.put("$each", eachList);
quizzesObj.put("$sort", new BasicDBObject("score", 1));
quizzesObj.put("$slice", -5);

coll.update(query, new BasicDBObject("$push", new BasicDBObject("quizzes", quizzesObj)));