【问题标题】:Specify fields to return in VoyageMongo指定要在 VoyageMongo 中返回的字段
【发布时间】:2017-05-05 09:42:18
【问题描述】:

我正在寻找一种可能性来限制 VoyageMongo 查询的返回字段。 假设我有带有字段(personId、firstName、lastName)的 db.persons。 在 Mongo 我可以通过
db.persons.find( { }, {'personId' : 1} )
查询 在 VoyageMongo 中,似乎所有发送到 JSON 查询的字典条目都被整理成一个 $and 查询。 MongoQuery 中有一个 instVar 和 Accessors 字段,但我不知道如何设置它们。 有没有办法在 VoyageMongo 中指定返回字段?

问候

最大

【问题讨论】:

    标签: mongodb pharo


    【解决方案1】:

    搜索了一段时间后,我发现唯一的选择是扩展 Class、VOMongoRepository、VOMongoRepositoryResolver 和 MongoCollection。 我添加了一个带有
    Class>>selectMany: aBlock options: someOptions ^self voyageRepository selectMany: self where: aBlock options: someOptions VOMongoRepository>>selectMany: aClass where: aDictionary options: someOptions | selected | selected := resolver selectMany: aClass where: aDictionary options: someOptions. ^aClass = aClass persistentClass ifTrue: [ selected ] ifFalse: [ selected select: [ :each | each isKindOf: aClass ] ] VOMongoRepositoryResolver>>selectMany: aClass where: aDictionary options: someOptions self execute: [ ^self basicSelectMany: aClass where: aDictionary options: someOptions ] VOMongoRepositoryResolver>>basicSelectMany: aClass where: aDictionary options: someOptions "Selecting instances of aClass should be done in the mongo query, not here" self flag: #todo. ^((self basicRawSelectMany: aClass where: aDictionary options: someOptions) collect: [ :each | self retrieveObjectOf: aClass json: each ] as: repository collectionClass) select: [ :each | each isKindOf: aClass ] VOMongoRepositoryResolver>>basicRawSelectMany: aClass where: aDictionary options: someOptions ^self pool withDatabase: [ :db | (self collectionAt: aClass inDatabase: db) select: aDictionary options: someOptions ] MongoCollection>>select: aDictionary options: someOptions ^ self query: [:query | query where: aDictionary; limit: (someOptions at: 'limit' ifAbsent: nil); offset: (someOptions at: 'offset' ifAbsent: nil); fields: (someOptions at: 'fields' ifAbsent: nil) ]的消息链

    这解决了这个问题。 消息以这种方式发送:

    options := { 'fields' -> { 'personId' -> 1 } asDictionary } asDictionary.
    ^ self selectMany: [ :each |
        (each at: 'name') = 'Max' ]
        options: options
    

    还可以在选项目录中添加限制和偏移量。 由于我有许多字段的对象,因此仅获取其中几个字段时的性能已从 48000 毫秒变为 229 毫秒
    我创建了一个带有扩展名的包。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-07-26
      • 2015-02-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-02-23
      相关资源
      最近更新 更多