我正在寻找限制VoyageMongo查询的返回字段的可能性。
假设我有db.persons字段(personId,firstName,lastName)。
在Mongo我可以通过
查询
db.persons.find( { }, {'personId' : 1} )
在VoyageMongo中,似乎发送到JSON查询的所有字典条目都被整理成$和查询。 MongoQuery中的字段有一个instVar和Accessors,但我不知道如何设置它们。
有没有办法在VoyageMongo中指定返回字段?
此致
最高
答案 0 :(得分:0)
在搜索了一段时间后,我找到的唯一选项是扩展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
还可以在options目录中添加限制和偏移量。 由于我有许多字段的对象,因此仅获取其中几个字段时的性能已从48000毫秒变为229毫秒 我创建了一个包含扩展名的包。