Aravind chals
Aravind chals

Reputation: 3

How to add features after applying sort in mongolite in R

sorted <- m$find(sort = '{"forks_count": -1}',
             limit = 10)

How do I select only 3 features such as name, age, and income after applying the sort on the forks_count?

Upvotes: 0

Views: 87

Answers (1)

user20042973
user20042973

Reputation: 5065

In standard MongoDB nomenclature, this is referred to as projection. General information about that is found on this page in their documentation.

Taking a look at the Mongolite User Manual, it seems like that project uses the fields parameter to provide this functionality. Based on that documentation, it looks like you can change your query to something similar to the following to get the results that you want:

sorted <- m$find(sort = '{"forks_count": -1}',
             fields = '{"name" : true, "age" : true, "income" : true}',
             limit = 10)

Note that depending on exactly what format you want your results in, you may also want to suppress the _id value (eg make it false).

Upvotes: 0

Related Questions