Query for subdocument

Issue #28 wontfix
Giuseppe Laurenza
created an issue

Hi all, I reopen the issue because it is not resolved for me. This is the original post: I want to substitute pymongo with monary in my code, I have to extract part of a document (a field with all its nested subfields) from a mongodb collection, i try with bson type, but the result of the query is a series of string, instead I need to convert them in dictionaries (one dict for each result). Is it possible with monary? I can't find a way to do that.

A. Jesse Jiryu Davis says: Hi Giuseppe, you can extract a document and a particular nested field with dot notation, like "a.b": from pymongo import MongoClient from monary import Monary

collection = MongoClient().test.test collection.delete_many({}) collection.insert_one({'a': {'b': 1}})

m = Monary() print(m.query("test", "test", {}, ["a.b"], ["int64"])) To get all the fields in a nested document, specify each one as a separate field using dot notation. Monary produces only NumPy arrays, never a dict.

my problem is that I do not know a priori wich are the subfields, an example is obtained using collection.insert_one({'a': {'b': {'c':1, 'd':{'e':10, 'f':{'g':10, 'h':11}}})

i want to extract b and all its nested field without knowing c,d or the other Is it possible with monary ?

Comments (3)

  1. A. Jesse Jiryu Davis

    Hi. No, this is not possible with Monary and it's not a feature on our roadmap. Monary creates NumPy arrays, which means you must tell Monary the exact field names and types before you query. If you want arbitrary data, please continue using PyMongo.

  2. Log in to comment