0

I have a bson file: xyz.bson full of useful data and I'd like to query/process the data using python. Is there a simple example/tutorial out there I can get started with?

I don't understand this one.

3 Answers 3

2

You could use the mongorestore command to import the data into a mongoDB server and then query it by connecting to that server.

Sign up to request clarification or add additional context in comments.

4 Comments

the documentation says its for json or csv. does it work with bson? docs.mongodb.org/v2.2/reference/mongoimport
My bad. Yes, it must be mongorestore then. That reads .bson files.
looks like that's the one. i'm a little confused on usage. I have a file on my desktop property.bson and I tried >mongostore --db propertyInfo /Desktop/property.bson and got SytaxError: Unexpected identifier. Can you point me to some good examples?
The command is mongorestore, in your example you wrote mongostore, maybe that is the reason. You may also have to specify both a database name and a collection name to import to, using the -d and -c options (-d is equivalent to --db). You also have an absolute pathname in your example, which may or may not be what you intended.
2

If you want to stream the data as though it were a flat JSON file on disk rather than loading it into a mongod, you can use this small python-bson-streaming library:

https://github.com/bauman/python-bson-streaming

from bsonstream import KeyValueBSONInput
from sys import argv
for file in argv[1:]:
    f = open(file, 'rb')
    stream = KeyValueBSONInput(fh=f,  fast_string_prematch="somthing") #remove fast string match if not needed
    for id, dict_data in stream:
        if id:
         ...process dict_data...

1 Comment

That Looks like a very interesting approach for querying The Exportes data
0

You may use sonq to query .bson file directly from bash, or you can import and use the lib in Python.

A few examples:

  • Query a .bson file sonq -f '{"name": "Stark"}' source.bson

  • Convert query results to a newline separated .json file sonq -f '{"name": {"$ne": "Stark"}}' -o target.json source.bson

  • Query a .bson file in python from sonq.operation import query_son record_list = list(query_son('source.bson', filters={"name": {"$in": ["Stark"]}}))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.