简体   繁体   English

如何使用monary从集合中选择所有字段/列?

[英]How do I select all fields/columns from a collection using monary?

I am using Monary 我正在使用Monary

I have a database in Mongo with a lot of rows and columns. 我在Mongo中有一个数据库,其中包含很多行和列。 Currently, the way I am using monary is as follows: 目前,我使用monary的方式如下:

client = Monary()
data = client.query("static_database",             # Database name
                     "properties",                 # Collection name 
                      {},                          # Query
                     ["Name","Address1","Address2"], # Field/col names
                     ["string:72"]*3)              # The types of each 
                                                   # field/col

The collection properties has a lot of field names and I want to take most if not all of those fields into data . 集合properties有很多字段名称,我想将大多数(如果不是全部)这些字段都放入data Typing more than 10 fields into a list seems like a pain. 在列表中输入10多个字段似乎很痛苦。

I also want to use different collections in the future, so a way of getting all column/field names would help a lot. 我也希望将来使用不同的集合,因此获取所有列/字段名称的方法会很有帮助。 I read through the docs and FAQs , yet haven't been able to find a solution. 我通读了文档和常见问题解答 ,但尚未找到解决方案。

I ended up using pyMongo to get all respective column names, using the code provided by this answer and mapReduce documentation . 我最终使用pyMongo来获得所有相应的列名,并使用了此答案和mapReduce 文档提供的代码。

The next problem was type . 下一个问题是type It was pretty annoying to try and find the type of each field (each field could have items of multiple types). 尝试查找每个字段的类型非常烦人(每个字段可以包含多种类型的项)。 I looked at this question . 我看着这个问题 But it seemed like a mess, so I ended up just taking a random entry from mongodb that hopefully contained all the fields. 但这似乎是一团糟,所以我最终只是从mongodb随机抽取了一个条目 ,希望其中包含所有字段。

There should be a better way of getting type names, but I did not implement that. 应该有一个更好的获取类型名称的方法,但是我没有实现它。 The final messy and bad implementation is as follows: 最终的混乱和糟糕的实现如下:

def getColsfromdb(supplier_name):
    client = MongoClient()
    db = client['supplier_static_database']
    map = Code("function(){   for (var key in this) {emit(key,null);} }") 
    reduce = Code("function(key, stuff) {return null;}")
    pT = getattr(db, supplier_name)
    mR = pT.map_reduce(map,reduce,supplier_name + "_keys")

    types_ = [type(v).__name__ for k,v in sorted(pT.find().limit(-1).skip(100).next().items())]

    cols4db = []
    for doc in mR.find():
        cols4db.append(doc["_id"])
    cols4db = sorted(cols4db)

    for i,t in enumerate(types_):
        if "unicode" in t:
            types_[i] = "string:50"
        if "ObjectId" in t:
            types_[i] = "id"
        if "list" in t or "NoneType" in t:
            types_[i] = "string:50"
        if "int" in t:
            types_[i]="int64"
        if "float" in t:
            types_[i]="float64"

    try:
        assert len(types_)==len(cols4db)
    except:
        tmpdiff = len(types_)-len(cols4db)
        if tmpdiff<0:
            for i in range(abs(tmpdiff)):
                types_.append("string:10")
        else:
            del cols4db[-tmpdiff:]

    return cols4db,types_

Initially, I had the except generate a random number every time it failed, but decided to stick with "randomly chosen" 100. 最初,我让except每次失败时都会生成一个随机数,但决定坚持使用“随机选择” 100。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将数据字段从一个集合中的所有文档移到另一集合中的新文档中? - How do I move fields of data from all documents in one collection into new documents of a different collection? 使用 Mongo DB 如何获取不同集合中所有相关字段的总和 - Using Mongo DB how do I get the sum of all related fields in a different collection 如何使用PyMongo从集合中动态选择字段? - How to dynamically select fields from a collection using PyMongo? 如何使用mgo从golang中的mongodb集合中选择所有记录 - How do you select all records from a mongodb collection in golang using mgo 如何使用 djongo 连接器确保不将空字段插入 django 的 mongodb 集合中? - How do I ensure that null fields are not inserted in mongodb collection, from django, using djongo connector? select 如何从 mongodb 中 object 类型的字段的集合中的所有现有字段 - how select all existing fields in a collection from an field of type object in mongodb 如何仅返回流星集合中的唯一集合字段? - How do I return only unique collection fields from a Meteor collection? 使用 mongoDB 时如何列出一个集合中的所有字段? - How to list all fields in one collection when i use mongoDB? 如何从控制台中Mongodb中的表中选择所有行? - how do I select all rows from a table in Mongodb in the console? 如何聚合集合中的嵌套字段? - How do i aggregate nested fields in my collection?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM