bson.M过滤器用于db.collection.find({}, {your_key:1, _id:0})。

huangapple go评论74阅读模式
英文:

bson.M filter for db.collection.find( {}, {your_key:1, _id:0})

问题

我想从我的Mongo文档中获取整个list_attributes字段:

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})

// 将过滤器传递给Find()以返回MongoDB游标
cursor, err := col.Find(ctx, filter)
if err != nil {
    log.Fatal("col.Find ERROR:", err)
}

但是这里的游标返回0个结果。

如何为相同的投影创建一个bson.M过滤器?

我正在使用官方的Go MongoDB驱动程序。

英文:

I want to get the entire list_atttributes field from my mongo document:

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 }
    }
}
]

I tried to write it like this:

filter := options.Find().SetProjection(bson.M{"list_attributes": 1})

// Pass the filter to Find() to return a MongoDB cursor
cursor, err := col.Find(ctx, filter)
if err != nil {
	log.Fatal("col.Find ERROR:", err)
}

But the cursor returns 0 results here.

How do I create a bson.M filter for the same projection?

I am using the official mongodb drive for go.

答案1

得分: 2

像这样应该可以工作

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
}
英文:

Something like this should work

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
}

huangapple
  • 本文由 发表于 2022年7月12日 18:18:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/72950689.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定