代码之家  ›  专栏  ›  技术社区  ›  themihai

如何编码/解码MongoDB光标?

  •  -1
  • themihai  · 技术社区  · 8 年前

    我需要建立一个“页面”列表,因此其中的一部分 cursor . 问题是我找不到编码(字符串)和解码光标的方法。知道吗?这个 Cursor 接口没有“编码”方法(虽然没有文档记录,但有ID),也无法从字符串(或int)创建新的光标。

    type Cursor interface {
    
        // Get the ID of the cursor.
        ID() int64
    
        // Get the next result from the cursor.
        // Returns true if there were no errors and there is a next result.
        Next(context.Context) bool
    
        Decode(interface{}) error
    
        DecodeBytes() (bson.Reader, error)
    
        // Returns the error status of the cursor
        Err() error
    
        // Close the cursor.
        Close(context.Context) error
    }
    

    为什么需要对光标进行编码?

    通过HTML或JSON API向最终客户机提供分页。

    2 回复  |  直到 8 年前
        1
  •  0
  •   Alex Blex    8 年前

    mongo.Cursor

    cursorTimeoutMillis

    Cursor.Decode()

    mongo.NewCursor() mongo.ParseCursor() mongo.DecodeCursor() Collection.Find()

    func (coll *Collection) Find(ctx context.Context, filter interface{},
        opts ...findopt.Find) (Cursor, error)
    
        2
  •  0
  •   themihai    8 年前

    Cursor recommended workaround _id

    function printStudents(startValue, nPerPage) {
      let endValue = null;
      db.students.find( { _id: { $lt: startValue } } )
                 .sort( { _id: -1 } )
                 .limit( nPerPage )
                 .forEach( student => {
                   print( student.name );
                   endValue = student._id;
                 } );
    
      return endValue;
    }
    

    minquery