MongoDB's python sdk "PyMongo" allows for "projections" where only the specified fields are returned from an object. Is it possible to do this in Realm? I'm working on the specifications of an app and haven't been able to find any documentation on projections in Realm.
The answer is No and Yes.
No:
Realm queries always return Realm Objects as complete objects. There is no Realm SDK interface for just returning a field or fields. For example (in Swift)
let results = realm.objects(Person.self)
will return all of the Person objects as complete objects. Those objects can then be read and then manipulated within a write transaction - and any property within an object can be changed. Let's update the name on the first Person object in results:
let firstPerson = results.first!
try! realm.write {
firstPerson.name = "Jay"
}
Note: See Class Projections for another option
Yes:
The SDK offers Application Services. Application services work at a 'lower' or 'more raw' level. The difference is that Application Services work with Documents - much like you would find in PyMongo.
Project Document Fields enables the functionality to return only specific fields within a document which is I believe what's being asked about.
The following omits the Persons _id field but includes the name field
let person: [Document] = ["$project": ["_id": 0, "name": 1]]
0 = exclude the field
1 = include the field
With that being said, working with data via App Services may not be necessary. Realm objects are Lazy-Loading and very memory friendly and their object oriented nature makes building relationships and organizing objects straightforward. See the docs for use cases where App Services may apply.