@Override void mergeAction(String subKey, DBObject subObject, Object object) { BasicDBList currentList = expressionParser.typecast(command, subObject.get(subKey), BasicDBList.class); if (currentList != null && currentList.size() > 0){ int direction = expressionParser.typecast(command, object, Number.class).intValue(); if(direction > 0){ currentList.remove(currentList.size() - 1); } else { currentList.remove(0); } } } },
DBCursor cursor = collection.find(); while ( cursor.hasNext()) { DBObject dbObject = cursor.next(); BasicDBList dbList = (BasicDBList) (dbObject.get("score")); //then use java Collections.sort() to sort the List (refer to two methods at the bottom) dbList.remove(3); collection.save(dbObject); }
try { while(cursor.hasNext()) { BasicDBObject doc = (BasicDBObject) cursor.next(); BasicDBList scoreList = (BasicDBList) doc.get("scores"); doc.remove("scores"); BasicDBObject hw1 = (BasicDBObject) scoreList.get("2"); double hw1Score = hw1.getDouble("score"); BasicDBObject hw2 = (BasicDBObject) scoreList.get("3"); double hw2Score = hw2.getDouble("score"); if (hw1Score > hw2Score) { scoreList.remove(3); } else { scoreList.remove(2); } doc.put("scores",scoreList); collection.save(doc); System.out.println(doc); } } finally { cursor.close(); } }
+ dbObjectList.remove(dbObject));
private BasicDBList aggregate(RequestContext ctx) { BasicDBList queryObject = new BasicDBList(); if (ctx.resourceParams() != null && ctx.resourceParams().contains("q")) { String queryString = ctx.resourceParams().value("q"); DBObject paramObject = (DBObject) JSON.parse(queryString); if (paramObject instanceof BasicDBList) { queryObject = (BasicDBList) paramObject; } else { queryObject.add(paramObject); } } DBCollection dbCollection = parent().getDBCollection(); try { BasicDBList result = new BasicDBList(); AggregationOutput output = dbCollection.aggregate( (DBObject) queryObject.remove(0), queryObject.toArray(new DBObject[queryObject.size()])); for (DBObject dbObject : output.results()) { result.add(dbObject); } return result; } catch (Exception e) { logger().error("", e); throw new RuntimeException("Aggregation query failed: ", e); } }