invalid-sort-specified-for-tailable-cursor

18 views
Skip to first unread message

Prasanna Ganesh

unread,
Sep 16, 2016, 12:57:33 PM9/16/16
to mongodb-user
I want to listen for CRUD changes in my mongodb and want to perform a audit my inserting modified records in a new table. As triggers are not supported in mongodb, i followed the approach of capped collection and tailable cursor ([How to listen for changes to a MongoDB collection?) and able to tail my logs. But when i'm trying to get latest updated record from logs its failing with below error.

Do anyone have idea on how to resolve this issue?

Exception:
Exception in thread "Thread-2" com.mongodb.MongoException: Unable to execute query: error processing query: ns=local.oplog.rs limit=0 skip=0
Tree: $and
Sort: { $natural: -1 }
Proj: {}
 invalid sort specified
for tailable cursor: { $natural: -1 }
    at com
.mongodb.MongoException.parse(MongoException.java:82)
    at com
.mongodb.DBApiLayer$MyCollection.__find(DBApiLayer.java:314)
    at com
.mongodb.DBApiLayer$MyCollection.__find(DBApiLayer.java:295)
    at com
.mongodb.DBCursor._check(DBCursor.java:368)
    at com
.mongodb.DBCursor._hasNext(DBCursor.java:459)
    at com
.mongodb.DBCursor.hasNext(DBCursor.java:484)
    at com
.trainings.core.Test.lambda$0(Test.java:32)
    at java
.lang.Thread.run(Unknown Source)

Program:
public static void main(String[] args) throws UnknownHostException {
   
Mongo mongo = new Mongo("localhost", 27017);
    DB db
= mongo.getDB("local");
   
DBCollection coll = db.getCollection("oplog.rs");


   
DBCursor cur = coll.find().sort(BasicDBObjectBuilder.start("$natural", -1).get())
           
.addOption(Bytes.QUERYOPTION_TAILABLE | Bytes.QUERYOPTION_AWAITDATA);


   
Runnable task = () -> {
       
System.out.println("\tWaiting for events");


       
while (cur.hasNext()) {
           
DBObject obj = cur.next();
           
System.out.println(obj);


       
}


   
};
   
new Thread(task).start();
}

Reply all
Reply to author
Forward
0 new messages