Query for subdocument

Create issue
Issue #27 resolved
Giuseppe Laurenza created an issue

Hi all, 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.

Comments (2)

  1. A. Jesse Jiryu Davis

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

  2. Log in to comment