0

如何在 Java 中进行此查询

db.comments.find({ "hsID.$id" { "$oid" : "4fe71a50e7e9f22ae5fb96bf"}} , {commentList: { $slice : [ 2 , 2]}});

这是我正在做的代码

    BasicDBObject query = new BasicDBObject();
        BasicDBObject allFields = new BasicDBObject();
        ObjectId objectId = new ObjectId(objId);
        query.put("{hsID.$id", objectId);
        Integer[] sliceInt = { startIndex, pageSize };
        query.put( 
                "commentList",
                new BasicDBObject().append("$slice", sliceInt));
 DBCursor resultsCursor = CommentColl.find(allFields,query);

输出是

输出是

query  = { "{hsID.$id" : { "$oid" : "4fe71a50e7e9f22ae5fb96bf"} , "commentList" : { "$slice" : [ 2 , 2]}}

谢谢你的帮助

4

1 回答 1

3

您需要将查询与要返回的字段分开。尝试更多类似的东西:

BasicDBObject query = new BasicDBObject("hsID.$id", new ObjectId(objId));
BasicDBObject fields = new BasicDBObject(
      "commentList",
       new BasicDBObject("$slice", new int[] { startIndex, pageSize }));
DBCursor resultsCursor = CommentColl.find(query, fields);

另请注意,我删除了 hsid.$id 之前的左大括号。

于 2012-09-23T21:30:07.537 回答