亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

db.collection.find({}, {your_key:1, _id:0})

db.collection.find({}, {your_key:1, _id:0})

Go
蝴蝶不菲 2023-01-03 14:05:19
我想list_atttributes從我的 mongo 文檔中獲取整個字段:db.config.find({},{list_attributes:1, _id:0});[{    list_attributes: {    '0': { field: 'LASTNAME', field_key: 'lastname', dataType: 'text' },    '1': { field: 'FIRSTNAME', field_key: 'firstname', dataType: 'text' },    '2': { field: 'SMS', dataType: 'text' },    '3': {        field: 'DOUBLE_OPT-IN',        dataType: 'category',        order: 1,        catAttrib: { '1': 'Yes', '2': 'No' }    },    '4': { field: 'OPT_IN', dataType: 'boolean', order: 2 },    '5': { field: 'TEST_NUMBER', dataType: 'float', order: 3 },    '6': { field: 'TEST_DATE', dataType: 'date', order: 4 }    }}]我試著這樣寫:filter := options.Find().SetProjection(bson.M{"list_attributes": 1})// Pass the filter to Find() to return a MongoDB cursorcursor, err := col.Find(ctx, filter)if err != nil {    log.Fatal("col.Find ERROR:", err)}但是游標在這里返回了 0 個結果。如何bson.M為同一投影創建過濾器?我正在使用官方的 mongodb 驅動器。
查看完整描述

1 回答

?
侃侃爾雅

TA貢獻1801條經驗 獲得超16個贊

這樣的事情應該工作


import (

    "go.mongodb.org/mongo-driver/bson"

    "go.mongodb.org/mongo-driver/mongo"

    "go.mongodb.org/mongo-driver/mongo/options"

)


func find(ctx context.Context) error {

    // ...

    cursor, err := collection.Find(ctx, bson.M{}, options.Find().SetProjection(bson.M{"list_attributes": 1}))

    if err != nil {

        return err

    }

    // ...

    return nil

}


查看完整回答
反對 回復 2023-01-03
  • 1 回答
  • 0 關注
  • 78 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號