jeroen / mongolite

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

Get information from GridFS #127

Closed FidelCastillo closed 6 years ago

FidelCastillo commented 6 years ago

hi, I'm tring to use the GridFS functions, and I'm pretty sure that I'm doing something wrong.

This is the pymongo code that I'm tring to replicate in mongolite:

client[ = MongoClient()
db = client['MotorCalculo']
fs = GridFS(db, collection='MotorCalculo')

DF = list()

for grid_out in fs.find({ 'metadata.CodKey':'123'}, no_cursor_timeout = True):
    info = json.loads(grid_out.read().decode())
    DF.append(info['NameCol'])`

and this is the mongolite code that I'm using

conFiles <- mongo('MotorCalculo.files', 'MotorCalculo')
gridFS <- gridfs("MotorCalculo")
gridFS$list(filter = '{"metadata.CodKey":"123"}')`

but I'm not able to read the gridFs out file

I have this

> gridFS$list(filter = '{"metadata.CodKey":"123"}')
[1] date
<0 rows> (or 0-length row.names)

thanks in advance

jeroen commented 6 years ago

That seems correct. Do you see anything if you do not pass the filter argument?

jeroen commented 6 years ago

What exactly does your metadata look like? This seems to work for me:

# some example data
fs <- mongolite::gridfs("test")
fs$write('iris', serialize(iris, NULL), metadata = '{"author": "jeroen"}')
fs$write('mtcars', serialize(mtcars, NULL), metadata = '{"author": "jimmy"}')
fs$write('cars', serialize(cars, NULL), metadata = '{"author": "jimmy"}')

# shows files with author jimmy
fs$find('{"metadata.author": "jimmy"}')

# drop example data
fs$drop()

Are you sure you are using the correct db? What do you get for fs$find() without filters?

FidelCastillo commented 6 years ago

Sorry I've got a few days off of my work. On Monday l'll be there. But I think that I can get the data from the gridFS

FidelCastillo commented 6 years ago

hi again, and sorry for delay... too busy in the last weeks.

this is what I have

library(mongolite) library(data.table) library(jsonlite)

conFiles <- mongo('MC.files', 'MC') conChunk <- mongo('MC.chunks', 'MC') fs <- gridfs("MC")

When I connect using the gridfd function I don't get any information

fs$find('{"metadata.CodMeta" : "123"}') [1] id name size date type

<0 rows> (or 0-length row.names)

But when I try with the mongo function I get the _id ,

data.table(conFiles$find('{"metadata.CodMeta":"123"}', fields = "{\"_id\":1}" )) _id 1: 5a8db149de310e302c1a59be 2: 5a8db149de310e302c1a59c4 3: 5a8db149de310e302c1a59c8 4: 5a8db0c4de310e302c19cda9 5: 5a8db149de310e302c1a59d4


5419: 5a8db0c4de310e302c19cd8b 5420: 5a8db0c4de310e302c19cd8f 5421: 5a8db0c4de310e302c19cd95 5422: 5a8db0c4de310e302c19cd9b 5423: 5a8db0c4de310e302c19cd9f

If I try the the empty find I don't have the data either

fs$find() [1] id name size date type

<0 rows> (or 0-length row.names)

Thank again, I'm sure that I'm pretty close

jeroen commented 6 years ago

Ah you are using a custom prefix? Try this: gridfs("MC", prefix = "MC")

francisliyy commented 6 years ago

Hi, why I can't use "fs <- mongolite::gridfs("test")", it says "Error: 'gridfs' is not an exported object from 'namespace:mongolite'

jeroen commented 6 years ago

You need the development version of the package:

devtools::install_github("jeroen/mongolite")
francisliyy commented 6 years ago

I got this error:

ERROR: configuration failed for package ‘mongolite’

jeroen commented 6 years ago

You need to give more output if you want me to help you.

jeroen commented 6 years ago

The CRAN version now has gridfs support so you no longer need to build from source:

install.packages("mongolite")