mongo DB打印字段的键

时间:2017-10-23 15:44:58

标签: mongodb printf key

假设我有一个包含许多字段的User集合。我知道用户邮件,例如test@test.com。我正在寻找一种方法来打印Partitioner is responsible for knowing how to divide up the data into the partitions. Spring Batch really only provides one out of the box, the的密钥,即" test@test.com"在mongo打印输出。

email

1 个答案:

答案 0 :(得分:0)

您可以找到您感兴趣的文档,进行项目设计,以便返回 "accounts": [{"uid": 1130000025409057, "iname": "", "sex": null, "ready": "no", "hintq": "", "aliases": [], "enabled": "yes", "maillist": "no", "fname": "", "birth_date": null, "login": "admin@bmwcci.org", "fio": ""}, 属性,然后从返回的文档中获取字段名称。例如:

email

FWIW,这是非常不寻常的,因为如果你知道你对电子邮件属性感兴趣那么为什么你需要编写代码来实现它:)也许如果你提供更多关于为什么<的细节/ strong>你想要做到这一点,另一个解决方案可能会变得明显。

如果您正在寻找我的文档中“找到所有密钥的名称”的通用解决方案,那么这样的事情可能会更好:

db.collection.find(
        // find all documents (you'll probably have your own criteria to add here)
        {}, 
        // project on the email attribute and - to reduce noise - exclude
        // the default _id projection 
        {email: 1, '_id': 0}
    )
    // it would make sense to limit here since you are only interested in an attribute name 
    // and, presumably, all relevant documents would have the same attribute name
    .limit(1)
    // since you only projected on email the returned document is
    // expected to have a single attribute
    .forEach(function(myDoc) { 
        for (var key in myDoc) {
            // print the attribute name
            print("key: " + key);    
        }     
    })