How to add features after applying sort in mongolite in R

85 Views Asked by At
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?

1

There are 1 best solutions below

0
user20042973 On

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).