我正在寻找一种可能的方法来限制voyagemongo查询的返回字段。
假设我有带字段(personid,firstname,lastname)的db.persons。
在Mongo我可以通过db.persons.find( { }, {'personId' : 1} )
在voyagemongo中,发送到json查询的所有字典条目似乎都被整理成一个$and查询。mongoquery中有一个instvar和字段访问器,但我不知道如何设置它们。
有没有办法在voyagemongo中指定返回字段?
当做
马克斯
最佳答案
在搜索了一段时间后,我找到的唯一选项是扩展类vomongorepository、vomongorepositoryresolver和mongocollection。
我添加了一个消息链Class>>selectMany: aBlock options: someOptions ^self voyageRepository selectMany: self where: aBlock options: someOptionsVOMongoRepository>>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
还可以在options目录中添加limit和offset。
由于我有许多字段的对象,因此仅获取其中一些字段时的性能已从48000 ms更改为229 ms
我已经创建了一个带有扩展名的包。
关于mongodb - 指定要在VoyageMongo中返回的字段,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/43801636/