2017-05-05 27 views
2

我正在尋找限制VoyageMongo查詢的返回字段的可能性。 假設我有db.persons字段(personId,firstName,lastName)。 在蒙戈我可以查詢
db.persons.find({ }, {'personId' : 1})
在VoyageMongo似乎所有的字典條目發送到JSON查詢被整理成一個$和查詢。 MongoQuery中有一個instVar和Accessors字段,但我不知道如何設置它們。 有沒有辦法指定VoyageMongo中的返回字段?指定要在VoyageMongo中返回的字段

問候

最大

回答

0

搜索了一會兒,我發現的唯一選擇是擴展類,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毫秒
我已經創建了一個擴展包。

相關問題