jeroen / mongolite

Fast and Simple MongoDB Client for R
https://jeroen.github.io/mongolite/
287 stars 65 forks source link

How to get all first elements from all documents using find #232

Open lisette-bouwmeester1 opened 2 years ago

lisette-bouwmeester1 commented 2 years ago

Considering this example collection:

students <- mongo("students") students$insert(c( '{ "student" : 1, "grades" : [ 95, 92, 90 ] }', '{ "student" : 2, "grades" : [ 98, 100, 102 ] }', '{ "student" : 3, "grades" : [ 95, 110, 100] }'))

I am trying to get from all documents the first element in grade. I tried the following, but this gives as output NULL

students$find(query = '{}', fields = '{"grades.0" : true}')

Why doesn't this work in the field operator, as it does work in the query part when looking for the student with the first grade 98:

students$find(query = '{"grades.0" : 98}', fields = '{"student" : true}')

I don't understand what I do wrong. Thanks for your help!